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:
1. Using a for-each loop :
One way to iterate over a Set is to use a for-each loop. This approach is similar to iterating over an array or a List.
Syntax :
Set set = new HashSet<>(); set.add("prepinsta"); set.add("prime"); set.add("prepinstaprime"); for (String s : set) { System.out.println(s); }
2. Using an iterator :
Another way to iterate over a Set is to use an iterator. This approach gives you more control over the iteration, such as the ability to remove elements while iterating.
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); }
3. A third way to iterate over a Set is to use Java 8 Streams.
This approach allows you to use functional programming constructs, such as map and filter, to manipulate the elements in the Set.
Syntax :
Setset = 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
Explanation :
This code creates a Set of strings, adds three elements to it, and then iterates over the Set using a for-each loop. The loop declares a variable s of type String and then iterates over the elements in the Set, assigning each element to the variable s in turn. The code then prints out each element.
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
Explanation :
This code is similar to the previous example, but it uses an iterator instead of a for-each loop. The code first creates an iterator for the Set using the iterator() method, and then loops over the elements using a while loop. The loop uses the hasNext() method to check if there are more elements to iterate over, and the next() method to get the next element in the Set.
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
Explanation :
This code is the shortest and most concise of the three examples, and uses Java 8 Streams to iterate over the Set. The code first creates a Stream from the Set using the stream() method, and then calls the forEach() method on the Stream to print out each element. The System.out::println syntax is a shorthand way of passing a method reference to the forEach() method.
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
Login/Signup to comment