LinkedHashSet in java

Ashok Veer | May 01, 2020 | Be the first to comment!

LinkedHashSet in java

Java LinkedhashSet class is collection class which implements Set interface. HashSet does not maintains insertion order but LinkedHashSet maintains insertion order of object also LinkedHashSet class is non-synchronized.

LinkedHashSet class Declaration


public class LinkedHashSet
    extends HashSet
    implements Set, Cloneable, java.io.Serializable {



Features of  LinkedHashset class

1] LinkedHashSet stores unique element only. Duplicate element not allowed if we enter then recent one get override by previous one.

2] Java LinkedHashSet class store unique element, LinkedHashSet internally uses HashSet and HashSet internally uses HashMap to store objects.

3] Java LinkedHashSet class defines iteration ordering, which is the order in which elements were inserted into the set.

Note: Insertion order is not affected if an element is re-inserted into the set. (An element e is reinserted into a set s if s.add(e) is invoked when s.contains(e) would return true immediately prior to the invocation.)

4] Java LinkedHashSet allows null value.

5] Java LinkedHashSet class is not thread safe, it means it is non- synchronized. If multiple threads access a linked hash set concurrently, and at least one of the threads modifies the set, it must be synchronized externally.

Set s = Collections.synchronizedSet(new LinkedHashSet(...));

LinkedHashSet provides constant-time performance for the basic operations (add, contains and remove), assuming the hash function disperses elements properly among the buckets.


Performance is likely to be just slightly below that of HashSet, due to the added expense of maintaining the linked list.

Constructor and Description

LinkedHashSet hs = new LinkedHashSet<>();
Constructs a new, empty linked hash set with the default initial capacity (16) and load factor (0.75).

LinkedHashSet hs = new LinkedHashSet(Collection c)
Constructs a new linked hash set with the same elements as the specified collection.

LinkedHashSet hs = new LinkedHashSet(int initialCapacity)
Constructs a new, empty linked hash set with the specified initial capacity and the default load factor (0.75).

LinkedHashSet hs = new LinkedHashSet(int initialCapacity, float loadFactor)

Constructs a new, empty linked hash set with the specified initial capacity and load factor.

Java LinkedHashSet Example


package com.vr.techfloaters;

import java.util.LinkedHashSet;

public class LinkedHashSetDemo {
    
     public static void main(String[] args) {
          
           /* Vector of default capacity(size) of 16 and load factor is 0.75f */
           LinkedHashSet hs = new LinkedHashSet<>();
          
           /* Adding elements to a LinkedHashSet*/
           hs.add("abc");
           hs.add(123);
           hs.add(null);
           hs.add(456);
           hs.add("abc");
           hs.add(null);
          
           /* check element and size */
           System.out.println(hs);
           System.out.println("Size is: "+hs.size());
               
         System.out.println("--------------------------");
          
         // Iterating each element using java 8 forEach
         hs.stream().forEach(System.out::println);
        
     }

}

Output
[abc, 123, null, 456]
Size is: 4
--------------------------
abc
123
null
456


As we can see in output duplicate element are first checked by s.contains(e) method and if the element is already present then it is not inserted in LinkedHashSet class.



0 comments:

Post a Comment

 
Copyright © 2019 techfloaters • All Rights Reserved.
Template Design by Ashok Veer ( veersoft solution)