3 Ways To Loop Over Gear Upwardly Or Hashset Inwards Java? Examples
Since Set interface or HashSet class doesn't render a get() method to recall elements, the exclusively agency to accept out elements from a Set is to iterate over it past times using Iterator, or loop over Set using advanced for loop of Java 5. You tin flame acquire the iterator past times calling the iterator() method of Set interface. This method returns an iterator over the elements inward the sets but they are returned inward no detail order, every bit Set doesn't guarantee whatever order. Though private Set implementations e.g. LinkedHashSet or TreeSet can impose ordering as well as inward such iterator volition render elements on that order. You tin flame exclusively traverse inward 1 management using iterator i.e. from kickoff to terminal elements, in that place is no backward traversing allowed every bit was the instance alongside List interface as well as ListIterator. Similarly, if you lot move advanced for loop, as well as then too you lot tin flame traverse inward exclusively 1 direction.
In this tutorial, you lot volition larn both ways to traverse over Set or HashSet inward Java i.e. past times using both Iterator and enhanced for loop. Btw, Java 8 too introduced a novel to loop over set, that is past times using the forEach() method if you lot are lucky to endure running on Java 8 as well as then you lot tin flame move that every bit well.
Here are the steps to traverse over every bit Set using Iterator inward Java:
This volition impress all the stocks from the setOfStocks into the console.
There is closed to other forEach() method defined inward the Stream cast from novel JDK 8 Stream API, See 10 ways to move forEach inward Java 8 for to a greater extent than examples.
Here is the summary of all 3 ways to iterate over HashSet or whatever Set inward Java:
That's all well-nigh how to traverse over HashSet or Set inward Java. You tin flame move the same technique to loop over other Set implementations similar LinkedHashSet, TreeSet, EnumSet, CopyOnWriteHashSet or ConcurrentSkipListSet inward Java.
All these implementations render the iterator() method which returns the elements inward the guild this cast individually guarantee e.g. LinkedHashSet guarantees insertion guild as well as TreeSet guarantees the sorted guild imposed past times Comparable or Comparator provided spell creating TreeSet instance.
You tin flame direct betwixt Iterator as well as advanced for loop to traverse over Set depending upon whether you lot desire to take the elements during iterator or not. The iterator allows you lot to take elements but enhanced for loop doesn't allow that.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures as well as Algorithms: Deep Dive Using Java
In this tutorial, you lot volition larn both ways to traverse over Set or HashSet inward Java i.e. past times using both Iterator and enhanced for loop. Btw, Java 8 too introduced a novel to loop over set, that is past times using the forEach() method if you lot are lucky to endure running on Java 8 as well as then you lot tin flame move that every bit well.
Iterating over Set using Iterator
Here are the steps to traverse over every bit Set using Iterator inward Java:
- Obtain the iterator past times calling the iterator() method.
- You tin flame move spell or for loop along alongside hasNext(), which render truthful if in that place are to a greater extent than elements inward the Set.
- Call the next() method to obtain the side past times side elements from Set.
Iterator<String> itr = setOfStocks.iterator(); // traversing over HashSet System.out.println("Traversing over Set using Iterator"); while(itr.hasNext()){ System.out.println(itr.next()); }
Loop over HashSet using for loop
There are no detail steps to loop over Set using enhanced for loop, every bit you lot only postulate to move the Set every bit per loop build every bit shown below:for(String stock : setOfStocks){ System.out.println(stock); }
This volition impress all the stocks from the setOfStocks into the console.
Traversing over HashSet using forEach() method
This method is exclusively available from Java 8 onwards. This forEach() method belongs to Iterable interface as well as since Set implements that you lot tin flame move this to traverse over Set every bit shown below:public class HashSetLooperJava8{ public static void main(String args[]) { // Creating as well as initializing an HashSet for iteration Set<String> setOfBooks = new HashSet<>(); setOfBooks.add("Effective Java"); setOfBooks.add("Clean Code"); setOfBooks.add("Refactoring"); setOfBooks.add("Head First Java"); setOfBooks.add("Clean Coder"); // iterating over HashSet using forEach() method inward Java 8 setOfBooks.forEach(System.out::println); } } Output: Clean Code Effective Java Head First Java Clean Coder Refactoring
There is closed to other forEach() method defined inward the Stream cast from novel JDK 8 Stream API, See 10 ways to move forEach inward Java 8 for to a greater extent than examples.
Here is the summary of all 3 ways to iterate over HashSet or whatever Set inward Java:
Java Program to loop over HashSet inward Java
Here is a sample programme to exhibit you lot how you lot tin flame loop over a Set inward Java using both Iterator as well as advanced for loop. Only divergence inward ii ways is that you lot tin flame take elements spell iterating using Iterator's remove() method but you lot cannot take elements spell looping over HashSet using enhanced for loop, fifty-fifty though Set provides a remove() method. Doing thence volition consequence inward ConcurrentModificationException inward Java.import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class HashSetDemo { public static void main(String args[]) { // Creating as well as initializing an HashSet for iteration Set<String> setOfStocks = new HashSet<>(); setOfStocks.add("INFY"); setOfStocks.add("BABA"); setOfStocks.add("GOOG"); setOfStocks.add("MSFT"); setOfStocks.add("AMZN"); System.out.println("Set: " + setOfStocks); // Example 1 - iterating over HashSet using Iterator // Obtaining the Iterator Iterator<String> itr = setOfStocks.iterator(); // traversing over HashSet System.out.println("Traversing over Set using Iterator"); while (itr.hasNext()) { System.out.println(itr.next()); } // Example 2 - looping over HashSet using for loop System.out.println("Looping over HashSet using advanced for loop"); for (String stock : setOfStocks) { System.out.println(stock); } } } Output: Set: [AMZN, INFY, MSFT, GOOG, BABA] Traversing over Set using Iterator AMZN INFY MSFT GOOG BABA Looping over HashSet using advanced for loop AMZN INFY MSFT GOOG BABA
That's all well-nigh how to traverse over HashSet or Set inward Java. You tin flame move the same technique to loop over other Set implementations similar LinkedHashSet, TreeSet, EnumSet, CopyOnWriteHashSet or ConcurrentSkipListSet inward Java.
All these implementations render the iterator() method which returns the elements inward the guild this cast individually guarantee e.g. LinkedHashSet guarantees insertion guild as well as TreeSet guarantees the sorted guild imposed past times Comparable or Comparator provided spell creating TreeSet instance.
You tin flame direct betwixt Iterator as well as advanced for loop to traverse over Set depending upon whether you lot desire to take the elements during iterator or not. The iterator allows you lot to take elements but enhanced for loop doesn't allow that.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures as well as Algorithms: Deep Dive Using Java
0 Response to "3 Ways To Loop Over Gear Upwardly Or Hashset Inwards Java? Examples"
Post a Comment