What is the difference between them? I know that
A LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all el
HashSet
don't maintain the order of insertion item
LinkedHashSet
maintain the order of insertion item
Example
Set set = ...;// using new HashSet<>() OR new LinkedHashSet<>()
set.add("2");
set.add("1");
set.add("ab");
for(String value : set){
System.out.println(value);
}
HashSet
output
1
ab
2
LinkedHashSet
output
2
1
ab