In this example we will show how to iterate through elements LinkedHashSet object in Java.
Source Code
package com.beginner.examples;
import java.util.Iterator;
import java.util.LinkedHashSet;
public class IterateThroughElementsOfLinkedHashSet {
public static void main(String[] args) {
//create LinkedHashSet.
LinkedHashSet linkedHashSet = new LinkedHashSet();
linkedHashSet.add("A");
linkedHashSet.add("B");
linkedHashSet.add("C");
linkedHashSet.add("D");
//Use the method iterator to get the Iterator.
Iterator iterator = linkedHashSet.iterator();
System.out.println("LinkedHashSet contains : ");
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
Output:
LinkedHashSet contains :
A
B
C
D
References
Imported packages in Java documentation: