Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Java - Iterating Set


As we have seen the implementations of a Set are :

  1. HashSet

  2. LinkedHashSet


Let us see the HashSet implementation first,


Say, we have a HashSet with values, Tom, John and Java.


Now, let us see the how to iterate a HashSet.


Iterating a HashSet using for each loop


Example :



import java.util.*;

public class MyApplication {
    public static void main(String[] args) {

        Set x = new HashSet<>();

        x.add("Tom");
        x.add("John");
        x.add("Java");

        for (String i : x) {
            System.out.println(i);
        }
    }
}


Output :



  Java
  Tom
  John

Similarly, in the above code we have created a HashSet.


Set x = new HashSet<>();

And initialised to the variable x.


x.add("Tom");
x.add("John");
x.add("Java");

Below is how they are placed in the HashSet.

Spring_Boot

Note : In HashSet the order is not maintained. i.e. We have inserted 'Tom', 'John' and 'Java'. But if you see the output, we get 'Java', 'Tom' and then 'John'.

In the next line we have used the for each loop to Iterate through the HashSet.


for (String i : x) {
	System.out.println(i);
}

Now, if we see the iterations of for loop,


for (String i : x) {
	System.out.println(i);
}

1st Iteration


In the first Iteration the first value of the HashSet x (i.e. Java) is taken and put into the variable i.

Spring_Boot

And the print statement, prints the value of i.

Output :



  Java

2nd Iteration


Similarly, in the second Iteration the second value of the HashSet x (i.e. Tom) is taken and put into the variable i.

Spring_Boot

And the print statement, prints the value of i.

Output :



  Java
  Tom

3rd Iteration


Similarly, in the third Iteration the third value of the HashSet x (i.e. John) is taken and put into the variable i.

Spring_Boot

And the print statement, prints the value of i.

Output :



  Java
  Tom
  John

Next, let us look at Iterating a LinkedHashSet.


Iterating a LinkedHashSet using for each loop


Example :



import java.util.*;

public class MyApplication {
    public static void main(String[] args) {

        Set x = new LinkedHashSet<>();

        x.add("Tom");
        x.add("John");
        x.add("Java");

        for (String i : x) {
            System.out.println(i);
        }
    }
}


Output :



  Tom
  John
  Java

Similarly, in the above code we have created a LinkedHashSet.


Set x = new LinkedHashSet<>();

And initialised to the variable x.


x.add("Tom");
x.add("John");
x.add("Java");

Below is how they are placed in the HashSet.

Spring_Boot

Note : In LinkedHashSet the order is maintained. i.e. We have inserted 'Tom', 'John' and 'Java'. And if you see the output, we get 'Tom', 'John' and 'Java'.

In the next line we have used the for each loop to Iterate through the LinkedHashSet.


for (String i : x) {
	System.out.println(i);
}

And we get the below output,

Output :



  Tom
  John
  Java