- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 Java LinkedHashSet
First, create a LinkedHashSet and add elements to it −
LinkedHashSet<Integer> set = new LinkedHashSet<Integer>(); set.add(10); set.add(20); set.add(30); set.add(40); set.add(50); set.add(60);
Now, use the Iterator to iterate through the elements −
Iterator i = set.iterator(); while (i.hasNext()) { System.out.println(i.next()); }
The following is an example to iterate through elements of LinkedHashSet −
Example
import java.util.*; public class Demo { public static void main(String[] args) { LinkedHashSet<Integer> set = new LinkedHashSet<Integer>(); set.add(10); set.add(20); set.add(30); set.add(40); set.add(50); set.add(60); System.out.println("LinkedHashSet..."); System.out.println(set); System.out.println("Iterating..."); Iterator i = set.iterator(); while (i.hasNext()){ System.out.println(i.next()); } } }
Output
LinkedHashSet... [10, 20, 30, 40, 50, 60] Iterating... 10 20 30 40 50 60
- Related Articles
- Iterate through elements of HashSet in Java
- 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
- Remove all elements from Java LinkedHashSet
- Golang Program to Iterate through Elements of Dictionary
- Iterate through Java Unit Tuple
- Iterate through Java Pair Tuple
- Iterate through ArrayList in Java
- Iterate through the values of Java LinkedHashMap in Java
- Copy all elements of Java LinkedHashSet to an Object Array
- Iterate through Decade Tuple in Java
- Iterate through Ennead Tuple in Java
- Iterate through Octet Tuple in Java
- Iterate through LabelValue Tuple in Java

Advertisements