Java Program to Iterate Over a Set

Java Program to Iterate Over a Set

What is Set in Java ?

In Java, a Set is a collection that stores a group of unique elements. Unlike a List, which can contain duplicate elements, a Set only stores unique elements. The Java Set interface extends the Collection interface and defines the behavior of a set.
Some of the key characteristics of a Set in Java include:

  • No duplicates:
  • No specific order:
  • Fast search:

Features of Set in Java :

  • No duplicates: A Set cannot contain duplicate elements. If you try to add an element that already exists in the Set, it will not be added.
  • No specific order: The elements in a Set have no specific order. This means that you cannot rely on the order of elements in a Set to remain consistent.
  • Fast search: The Set interface provides efficient operations for checking whether an element is in the Set.

Ways to Iterate over Set in Java:

Syntax :

Set set = new HashSet<>();
set.add("prepinsta");
set.add("prime");
set.add("prepinstaprime");

for (String s : set) {
    System.out.println(s);
}

Syntax :

Set set = new HashSet<>();
set.add("prepintsa");
set.add("prime");
set.add("prepinstaprime");

Iterator iterator = set.iterator();
while (iterator.hasNext()) {
    String s = iterator.next();
    System.out.println(s);
}

Syntax :

Set set = new HashSet<>();
set.add("prepinsat");
set.add("prime");
set.add("prepinstaprime");

set.stream().forEach(System.out::println);

Example 1 : 

Run
import java.util.HashSet;
import java.util.Set;

public class Main {

    public static void main(String[] args) {
        Set< String> set = new HashSet<>();
        set.add("prepinsta");
        set.add("prime");
        set.add("prepinstaprime");

        for (String s : set) {
            System.out.println(s);
        }
    }
}

Output :

prime
prepinsta
prepinstaprime

Example 2 : 

Run
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Main {

    public static void main(String[] args) {
        Set< String> set = new HashSet<>();
        set.add("prepinsta");
        set.add("prime");
        set.add("prepinsatprime");

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

Output :

prime
prepinsta
prepinsatprime

Example 3 : 

Run
import java.util.HashSet;
import java.util.Set;

public class Main {

    public static void main(String[] args) {
        Set< String> set = new HashSet<>();
        set.add("prepinsta");
        set.add("prime");
        set.add("prepinsatprime");

        set.stream().forEach(System.out::println);
    }
}

Output :

prime
prepinsta
prepinsatprime

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription