LinkedHashSet in JAVA

 The LinkedHashSet Class

The LinkedHashSet class extends HashSet and adds no members of its own. It is a generic class that has this declaration: 

 class LinkedHashSet

Here, E specifies the type of objects that the set will hold. Its constructors parallel those in HashSet.  

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. That is, when cycling through a LinkedHashSet using an iterator, the elements will be returned in the order in which they were inserted. This is also the order in which they are contained in the string returned by toString( ) when called on a LinkedHashSet object.

Example(Source Code):

import java.util.*;
class Main{
    public static void main(String[] args){
        LinkedHashSet<String> hs = new LinkedHashSet<String>();
        hs.add("Beta");
        hs.add("Alpha");
        hs.add("Eta");
        hs.add("Gamma");
        hs.add("Epsilon");
        hs.add("Omega");
        System.out.println(hs);
    }
}

Output: 

[Beta, Alpha, Eta, Gamma, Epsilon, Omega]

Comments

Popular posts from this blog

THREE LEVELS OF DATA INDEPENDENCE

Python-HackerRank Problem List Comprehensions

Python Problem Solving - Lonely Integer