Monday, March 30, 2020

How To Iterate Over Hashset Inwards Coffee - Loop Or Traverse Example

Iterating over HashSet inwards Java
How to iterate over ArrayList inwards Java as well as inwards this tutorial nosotros volition meet How to iterate over HashSet inwards Java. There are 2 ways to iterate, loop or traverse over HashSet inwards Java, commencement using advanced for-each loop added on Java five as well as 2d using Iterator which is a to a greater extent than conventional agency of iterating over HashSet inwards Java. Now questions are When should y'all purpose for loop as well as when Iterator is an appropriate option. Well I ordinarily purpose for loop If I entirely read from HashSet as well as doesn't take whatsoever element, piece Iterator is preferred approach. You should non take elements from HashSet piece iterating over it inwards for loop, Use Iterator to create that.



Iterating over HashSet inwards Java - Example

In this section, nosotros volition meet the code illustration of iterating over HashSet inwards Java. We accept created HashSet as well as added a brace of String on HashSet as well as when nosotros iterate, nosotros volition impress those String i past times i inwards the console.



/**
 *
 * Java programme to Iterate, loop or traverse over HashSet inwards Java
 * This technique tin live on used to traverse over whatsoever other Collection equally well
 * e.g. traversing over ArrayList or traversing over LinkedList inwards Java
 * @author
 */

public class IteratorTest {

 
    public static void main(String args[]) {
     
        //creating HashSet to iterate over
        Set<String> streams = new HashSet<String>();
        streams.add("Programming");
        streams.add("Development");
        streams.add("Debugging");
        streams.add("Coding");
     
        // Iterating over HashSet inwards Java using for-each loop
        for(String str : streams){
            System.out.println(" Looping over HashSet inwards Java chemical share : " + str);
        }
     
        //Iterating over HashSet using Iterator inwards Java
        Iterator<String> itr = streams.iterator();
        while(itr.hasNext()){
            System.out.println(" Iterating over HashSet inwards Java electrical flow object: " + itr.next());
        }
     
    }
}

Output:
 Looping over HashSet inwards Java chemical share : Coding
 Looping over HashSet inwards Java chemical share : Programming
 Looping over HashSet inwards Java chemical share : Debugging
 Looping over HashSet inwards Java chemical share : Development
 Iterating over HashSet inwards Java electrical flow object: Coding
 Iterating over HashSet inwards Java electrical flow object: Programming
 Iterating over HashSet inwards Java electrical flow object: Debugging
 Iterating over HashSet inwards Java electrical flow object: Development

Further Learning
Java In-Depth: Become a Complete Java Engineer
What is divergence betwixt TreeMap as well as TreeSet inwards Java

No comments:

Post a Comment