LinkedHashSet
LinkedHashSet available since Java 1.4
LinkedHashSet extends HashSet class.
LinkedHashSet implements following interfaces:
import java.util.*;
class HashSetFlight {
public static void main(String args[]) {
LinkedHashSet<String> lhs = new LinkedHashSet<String>();
lhs.add("A");
lhs.add("A"); // adding duplicate return false
lhs.add("C");
lhs.add("D");
lhs.add(null); // null allowed
System.out.println(lhs);
/// LinkedHashSet maintains insertion order
}
}
Output:
[A, C, D, null]
LinkedHashSet extends HashSet class.
LinkedHashSet implements following interfaces:
- Serializable
- Cloneable
- Iterable<E>
- Collection<E>
- Set<E>
- LinkedHashSet()
- LinkedHashSet(Collection c)
- LinkedHashSet(int initialCapacity)
- LinkedHashSet(int initialCapacity, int loadFactor)
- The underlying data structure used for LinkedHashSetis HashTable + LinkedList.
- Default initial capacity of LinkedHashSetis 16.
- Default loat factor of LinkedHashSetis 0.75.
- LinkedHashSet maintain insertion order.
- LinkedHashSet does not allow duplicate elements.
- Once the total elements in the LinkedHashSet is reach upto the load factor then the capacity of LinkedHashSet increased automatically.
- LinkedHashSet allows null element.
- Iterator returned by LinkedHashSet and HashSet are fail-fast Iterator. It means if Iterator is modified after its creation by any way other than Iterators remove() method, it will throw ConcurrentModificationException with best of effort.
import java.util.*;
class HashSetFlight {
public static void main(String args[]) {
LinkedHashSet<String> lhs = new LinkedHashSet<String>();
lhs.add("A");
lhs.add("A"); // adding duplicate return false
lhs.add("C");
lhs.add("D");
lhs.add(null); // null allowed
System.out.println(lhs);
/// LinkedHashSet maintains insertion order
}
}
Output:
[A, C, D, null]