How do I iterate and modify Java Sets?

JavaSet

Java Problem Overview


Let's say I have a Set of Integers, and I want to increment every Integer in the Set. How would I do this?

Am I allowed to add and remove elements from the set while iterating it?

Would I need to create a new set that I would "copy and modify" the elements into, while I'm iterating the original set?

EDIT: What if the elements of the set are immutable?

Java Solutions


Solution 1 - Java

You can safely remove from a set during iteration with an Iterator object; attempting to modify a set through its API while iterating will break the iterator. the Set class provides an iterator through getIterator().

however, Integer objects are immutable; my strategy would be to iterate through the set and for each Integer i, add i+1 to some new temporary set. When you are finished iterating, remove all the elements from the original set and add all the elements of the new temporary set.

Set<Integer> s; //contains your Integers
...
Set<Integer> temp = new Set<Integer>();
for(Integer i : s)
    temp.add(i+1);
s.clear();
s.addAll(temp);

Solution 2 - Java

You can do what you want if you use an iterator object to go over the elements in your set. You can remove them on the go an it's ok. However removing them while in a for loop (either "standard", of the for each kind) will get you in trouble:

Set<Integer> set = new TreeSet<Integer>();
	set.add(1);
	set.add(2);
	set.add(3);
	
	//good way:
	Iterator<Integer> iterator = set.iterator();
	while(iterator.hasNext()) {
		Integer setElement = iterator.next();
		if(setElement==2) {
			iterator.remove();
		}
	}
	
	//bad way:
	for(Integer setElement:set) {
		if(setElement==2) {
			//might work or might throw exception, Java calls it indefined behaviour:
			set.remove(setElement);
		} 
	}

As per @mrgloom's comment, here are more details as to why the "bad" way described above is, well... bad :

Without getting into too much details about how Java implements this, at a high level, we can say that the "bad" way is bad because it is clearly stipulated as such in the Java docs:

https://docs.oracle.com/javase/8/docs/api/java/util/ConcurrentModificationException.html

stipulate, amongst others, that (emphasis mine):

> "For example, it is not generally permissible for one thread to > modify a Collection while another thread is iterating over it. In > general, the results of the iteration are undefined under these > circumstances. Some Iterator implementations (including those of all > the general purpose collection implementations provided by the JRE) > may choose to throw this exception if this behavior is detected" (...) > > "Note that this exception does not always indicate that an object > has been concurrently modified by a different thread. If a single > thread issues a sequence of method invocations that violates the > contract of an object, the object may throw this exception. For > example, if a thread modifies a collection directly while it is > iterating over the collection with a fail-fast iterator, the iterator > will throw this exception."

To go more into details: an object that can be used in a forEach loop needs to implement the "java.lang.Iterable" interface (javadoc here). This produces an Iterator (via the "Iterator" method found in this interface), which is instantiated on demand, and will contain internally a reference to the Iterable object from which it was created. However, when an Iterable object is used in a forEach loop, the instance of this iterator is hidden to the user (you cannot access it yourself in any way).

This, coupled with the fact that an Iterator is pretty stateful, i.e. in order to do its magic and have coherent responses for its "next" and "hasNext" methods it needs that the backing object is not changed by something else than the iterator itself while it's iterating, makes it so that it will throw an exception as soon as it detects that something changed in the backing object while it is iterating over it.

Java calls this "fail-fast" iteration: i.e. there are some actions, usually those that modify an Iterable instance (while an Iterator is iterating over it). The "fail" part of the "fail-fast" notion refers to the ability of an Iterator to detect when such "fail" actions happen. The "fast" part of the "fail-fast" (and, which in my opinion should be called "best-effort-fast"), will terminate the iteration via ConcurrentModificationException as soon as it can detect that a "fail" action has happen.

Solution 3 - Java

I don't like very much iterator's semantic, please consider this as an option. It's also safer as you publish less of your internal state

private Map<String, String> JSONtoMAP(String jsonString) {

    JSONObject json = new JSONObject(jsonString);
    Map<String, String> outMap = new HashMap<String, String>();

    for (String curKey : (Set<String>) json.keySet()) {
        outMap.put(curKey, json.getString(curKey));
    }

    return outMap;

}

Solution 4 - Java

You could create a mutable wrapper of the primitive int and create a Set of those:

class MutableInteger
{
    private int value;
    public int getValue()
    {
        return value;
    }
    public void setValue(int value)
    {
        this.value = value;
    }
}

class Test
{
    public static void main(String[] args)
    {
        Set<MutableInteger> mySet = new HashSet<MutableInteger>();
        // populate the set
        // ....

        for (MutableInteger integer: mySet)
        {
            integer.setValue(integer.getValue() + 1);
        }
    }
}

Of course if you are using a HashSet you should implement the hash, equals method in your MutableInteger but that's outside the scope of this answer.

Solution 5 - Java

Firstly, I believe that trying to do several things at once is a bad practice in general and I suggest you think over what you are trying to achieve.

It serves as a good theoretical question though and from what I gather the CopyOnWriteArraySet implementation of java.util.Set interface satisfies your rather special requirements.

http://download.oracle.com/javase/1,5.0/docs/api/java/util/concurrent/CopyOnWriteArraySet.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionButtons840View Question on Stackoverflow
Solution 1 - JavaJonathan WeatherheadView Answer on Stackoverflow
Solution 2 - JavaShivan DragonView Answer on Stackoverflow
Solution 3 - JavasnovelliView Answer on Stackoverflow
Solution 4 - JavaSavvas DalkitsisView Answer on Stackoverflow
Solution 5 - JavaMarianPView Answer on Stackoverflow