- 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
Iterate through elements of HashSet in Java
Create a HashSet and add elements to it −
Set<Integer> hs = new HashSet<Integer>(); hs.add(20); hs.add(39); hs.add(67); hs.add(79); hs.add(81); hs.add(87);
Try the below given code to iterate through the elements −
Iterator i = hs.iterator(); while (i.hasNext()) System.out.println(i.next());
To iterate through the elements of HashSet, try the following code −
Example
import java.util.*; public class Demo { public static void main(String args[]) { Set<Integer> hs = new HashSet<Integer>(); hs.add(20); hs.add(39); hs.add(67); hs.add(79); hs.add(81); hs.add(87); hs.add(88); System.out.println("Elements = "+hs); Iterator i = hs.iterator(); while (i.hasNext()) System.out.println(i.next()); } }
Output
Elements = [81, 67, 20, 39, 87, 88, 79] 81 67 20 39 87 88 79
- Related Articles
- Iterate over the elements of HashSet in Java
- Iterate through elements of Java LinkedHashSet
- Iterate through elements of TreeSet in Java
- Java Program to Iterate through Elements of HashMap
- Iterate through elements of a LinkedList using a ListIterator in Java
- Iterate through ArrayList in Java
- Traverse through a HashSet in Java
- Iterate through the values of Java LinkedHashMap in Java
- Iterate through Decade Tuple in Java
- Iterate through Ennead Tuple in Java
- Iterate through Octet Tuple in Java
- Iterate through LabelValue Tuple in Java
- Iterate through KeyValue Tuple in Java
- Iterate through the values of HashMap in Java
- Iterate through the values of TreeMap in Java

Advertisements