This class extends HashSet, but adds no members of its own.
Java Collections Framework- LinkedHashSet Class
This class extends HashSet, but adds no members of its own. LinkedHashSet maintains a linked list of the entries in the set, in the order in
which they were inserted. This allows insertion-order iteration over the set.
The datastructure LinkedHashSet combines the advantages of a LinkedList and of the HashSet. Internally it keeps the data both as a HashSet and as a
LinkedList, so the restrictions for HashSets still apply. This structure is useful when a list has to be ordered
but also requires many checks if a specified element is in the list.
The hash code is then used as the index at which the data associated with the key is stored. The transformation of the key into its hash code is performed
automatically.
The LinkedHashSet class supports four constructors.
LinkedHashSet() - This constructor constructs a new, empty linked hash set with the default initial capacity (16) and load factor (0.75).
LinkedHashSet(Collection c) - This constructor construct a new linked hash set with the same elements as the specified collection.
LinkedHashSet(int capacity) - This constructor construct a new, empty linked hash set with the specified initial capacity and the default load factor (0.75).
LinkedHashSet(int initialCapacity, float loadFactor) - This constructor construct a new, empty linked hash set with the specified initial capacity and load factor.
Here is an example to demonstrate the implementation of LinkedHashSet class.
Example:- LinkedHashSetDemo.java
package devmanuals.com; import java.util.*; public class LinkedHashSetDemo { public static void main(String args[]) { LinkedHashSet LHSet = new LinkedHashSet(); LHSet.add("C"); LHSet.add("A"); LHSet.add("B"); LHSet.add("E"); LHSet.add("F"); LHSet.add("D"); System.out.println("The LinkedHashSet elements are: " + LHSet); } }Output:-
The LinkedHashSet elements are: [C, A, B, E, F, D] |
Download This Code.
More Examples are here-
[ 0 ] Comments