- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Iterate over a Set
In this article, we will understand how to iterate over a set. A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction.
The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.
Below is a demonstration of the same −
Suppose our input is −
Input set: [Java, Scala, Mysql, Python]
The desired output would be −
Iterating over Set using for-each loop: Java, Scala, Mysql, Python
Algorithm
Step 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Create a hashset of values and initialize elements in it using the ‘add’ method. Step 5 - Display the hashset on the console. Step 6 - Iterate over the elements of the hashset, and fetch each value. Step 7 - Display this on the console. Step 8 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function. For-each
import java.util.Set; import java.util.HashSet; public class Demo { public static void main(String[] args) { System.out.println("The required packages have been imported"); Set<String> input_set = new HashSet<>(); input_set.add("Java"); input_set.add("Scala"); input_set.add("Python"); input_set.add("Mysql"); System.out.println("The set is defined as: " + input_set); System.out.println("\nIterating over Set using for-each loop:"); for(String elements : input_set) { System.out.print(elements); System.out.print(", "); } } }
Output
The required packages have been imported The set is defined as: [Java, Scala, Mysql, Python] Iterating over Set using for-each loop: Java, Scala, Mysql, Python,
Example 2
Here, we encapsulate the operations into functions exhibiting object oriented programming. Iterator
import java.util.Set; import java.util.HashSet; public class Demo { static void print_set(Set<String> input_set){ System.out.println("\nIterating over Set using for-each loop:"); for(String elements : input_set) { System.out.print(elements); System.out.print(", "); } } public static void main(String[] args) { System.out.println("The required packages have been imported"); Set<String> input_set = new HashSet<>(); input_set.add("Java"); input_set.add("Scala"); input_set.add("Python"); input_set.add("Mysql"); System.out.println("The set is defined as: " + input_set); print_set(input_set); } }
Output
The required packages have been imported The set is defined as: [Java, Scala, Mysql, Python] Iterating over Set using for-each loop: Java, Scala, Mysql, Python,
- Related Articles
- Java Program to Iterate over a HashMap
- Java Program to Iterate over enum
- Java Program to Iterate over an ArrayList
- Iterate over a set in Python
- Program to iterate over a List using Java 8 Lambda
- Java Program to Iterate over ArrayList using Lambda Expression
- Program to Iterate over a Stream with Indices in Java 8
- How to iterate over a Java list?
- How to iterate over a list in Java?
- Python program to iterate over multiple lists simultaneously?
- C# program to iterate over a string array with for loop
- How to iterate over a C# dictionary?
- How to iterate over a C# list?
- How to iterate over a C# tuple?
- Iterate over the elements of HashSet in Java

Advertisements