
How can we use LinkedHashSet in java collection?

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
public class LinkedHashSetExample{
public static void main(String [] args){
Set ht = new LinkedHashSet();
ht.add("List");
ht.add("Array");
ht.add("Linked");
ht.add("HashSet");
ht.add('R');
ht.add("List");
ht.remove('R');
Iterator it = ht.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
System.out.println("Size :" + ht.size());
}
}
Output:-
List Array Linked HashSet Size :4
Description:- The above example demonstrates you the Set interface. Since Set is an interface, you need to instantiate a concrete implementation of the interface in order to use it. Here Set is implemented by its subclass LinkedHashSet. LinkedHashSet does not accept duplicate values. So here we have check whether the added elements are duplicate or not Then we have removed an element â??Râ?? using remove() method from the LinkedHashSet and iterated the remaining elements of LinkedHashSet r using Iterator and display them.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.