- 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
Advertisements