Pages

Linked Hashset in java

What is Linked Hashset in java?

Linked HashSet::

--> It is child class of Hashset.
--> It is exactly same as hashset(including constructors and methods) but insertion order is preserved.
--> Duplicates are not allowed.
--> It's structure is combination of linkedlist and hashtable.

Demo program on Linked HashSet:

import java.util.LinkedHashSet;

public class setDemo {
    public static void main(String[] args) {
        LinkedHashSet hs = new LinkedHashSet();
        hs.add(1);
        hs.add("1");
        hs.add(null);
        hs.add(3);
        System.out.println(hs.add(3));
        System.out.println(hs);
    }
}
Output:
Linked Hashset in java

  • If we execute above program will get constant output as insertion order is preserved in LinkedHashSet.
  • In general we can use linked hashset to develop cache based applications where duplicated are not allowed and insertion order preserved.
Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.