Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using iterator on a TreeSet

SITUATION: I have a TreeSet of custom Objects and I have also used a custom Comparator. I have created an iterator to use on this TreeSet.

TreeSet<Custom> ts=new TreeSet<Custom>();
Iterator<Custom> itr=ts.iterator();
while(itr.hasNext()){
    Custom c=itr.next();
    //Code to add a new element to the TreeSet ts
}

QUESTION: Well I want to know that if I add a new element to the TreeSet within the while loop, then will that new element get sorted immediately. In other words, if I add a new element within the while loop and it is less than the one which I am currently holding in c, then in the next iteration will I be getting the same element in c as in the last iteration?(since after sorting, the newly added element will occupy a place somewhere before the current element).

like image 829
aps Avatar asked Jun 23 '11 20:06

aps


People also ask

Can we use iterator in TreeSet?

Iterator can be created over the TreeSet objects. Hence this iterator can be used to traverse or loop through the TreeSet.

How does iterator work on TreeSet?

iterator() method is used to return an iterator of the same elements as that of the TreeSet. The elements are returned in random order from what was present in the Tree set. Syntax: Iterator iterate_value = Tree_Set.

Is TreeSet faster than HashSet?

Simply put, HashSet is faster than the TreeSet. HashSet provides constant-time performance for most operations like add(), remove() and contains(), versus the log(n) time offered by the TreeSet.

Can we use comparator with TreeSet in Java?

The comparator() method been present inside java. util. TreeSet shares an important function of setting and returning the comparator that can be used to order the elements in a TreeSet. The method returns a Null value if the set follows the natural ordering pattern of the elements.


2 Answers

If you add an element during your iteration, your next iterator call will likely throw a ConcurrentModificationException. See the fail-fast behavior in TreeSet docs.

To iterate and add elements, you could copy first to another set:

TreeSet<Custom> ts = ...
TreeSet<Custom> tsWithExtra = new TreeSet(ts);

for (Custom c : ts) {
  // possibly add to tsWithExtra
}

// continue, using tsWithExtra

or create a separate collection to be merged with ts after iteration, as Colin suggests.

like image 97
Michael Brewer-Davis Avatar answered Sep 18 '22 23:09

Michael Brewer-Davis


You will get a java.util.ConcurrentModificationException if you add an element into the TreeSet inside while loop.

Set<String> ts=new TreeSet<String>();
ts.addAll(Arrays.asList(new String[]{"abb", "abd", "abg"}));
Iterator<String> itr=ts.iterator();
while(itr.hasNext()){
    String s = itr.next();
    System.out.println("s: " + s);
    if (s.equals("abd"))
        ts.add("abc");
}

Output

Exception in thread "main" java.util.ConcurrentModificationException
like image 36
anubhava Avatar answered Sep 18 '22 23:09

anubhava