Sunday, November 24, 2019

How To Loop Over Treeset Inward Coffee Alongside Example

In our before articles, nosotros accept learned how to loop over ArrayList inward Java as well as you lot tin role the same logic to loop over a TreeSet. You tin role both, enhanced for loop as well as Iterator to traverse over TreeSet inward Java. Though, worth noting is that Iterator returned yesteryear the iterator() method of TreeSet returns elements inward the sorted gild which is imposed yesteryear the Comparator you lot accept provided to TreeSet at the fourth dimension of instantiation. By default, if you lot don't supply whatsoever custom Comparator than TreeSet sorts elements inward their natural gild e.g. String elements are sorted inward alphabetic gild as well as Integer elements are sorted inward numeric order. When you lot iterate over a TreeSet the iterator follows this order.

You tin too role the enhanced for loop, which also internally uses the Iterator and traverse the TreeSet inward the same order, simply you lot cannot take away an chemical cistron there. On the other hand, Iterator allows you lot to take away elements piece iterating over TreeSet. In this article, you lot volition uncovering the examples of both approaches.


For those who are novel to TreeSet, Influenza A virus subtype H5N1 TreeSet is a sorted collection inward Java. You insert an chemical cistron inward whatsoever gild simply when you lot iterate through the collection, the values are automatically presented inward the sorted order. The sorting is accomplished yesteryear a tree information structure. The electrical current implementation of java.util.TreeSet uses a Red-Black tree. For a detailed description of red-black trees, encounter a practiced algorithm mass e.g. Introduction to Algorithms By Thomas Cormen, Charles Leiserson, Ronald Rivest as well as Clifford Stein. Every fourth dimension an chemical cistron is added to a tree, it is placed into its proper sorting position. Therefore, the iterator ever visits the elements inward sorted order.

 enhanced for loop as well as Iterator to traverse over  How to loop over TreeSet inward Java alongside example



Iterating over TreeSet using Iterator

You tin follow iii steps to start iterating over TreeSet using Iterator, retrieve this is going from outset to final chemical cistron inward the sorted order.

1) acquire the Iterator yesteryear calling iterator() method
2) Use a for or piece loop alongside hasNext()
3) Call the next() method

Iterator<String> itr = treeSetOfScrips.iterator(); while (itr.hasNext()) {    System.out.println(itr.next()); }

You tin too take away the electrical current chemical cistron yesteryear calling the remove() method of Iterator, don't role the remove() method provided yesteryear the java.util.Collection interface because that volition effect inward ConcurrentModificationException every bit shown here.




Looping over TreeSet using enhanced for loop of Java 5
Here is the sample code to loop over TreeSet using advanced for loop, Remember you lot cannot take away elements when you lot  are using enhanced for loop of JDK 1.5 release.

for (String stock : treeSetOfScrips) {    System.out.println(stock); }

There is too a 3rd agency to iterate over TreeSet inward Java simply that is solely available from JDK 8 onwards. You tin role the forEach() method to iterate over whatsoever Collection including TreeSet inward Java 8 because this method comes from java.util.Iterable interface.

Here is a overnice slide of showing how to loop over TreeSet using both iterator as well as enhanced for loop of JDK v inward Java program:

solution)
  • The departure betwixt TreeSet as well as HashSet inward Java? (answer)
  • How to traverse over a List inward Java? (example)
  • 6 departure betwixt LinkedHashSet as well as TreeSet inward Java? (answer)
  • How to iterate over HashSet inward Java? (solution)
  • The best agency to iterate over each entry of HashMap inward Java? (answer)
  • Core Java, Volume one ninth Edition yesteryear Cay S. Horstmann (read)

  • No comments:

    Post a Comment