LinkedHashSet Interface tutorial in Java Collection

The LinkedHashSet Interface implements the Set Interface and store the elements in a hash table with linked list running through it. The The LinkedHashSet Interface allows unique elements like HashSet as it inherits the HashSet class . The LinkedHashSet creates a empty HashSet which provides default initial capacity as 16 and load factor as 0.75 when constructor LinkedHashSet() is invoked. The LinkedHashSet maintains the elements insertion order and allows NULL Values

The LinkedHashSet Interface class Hierarchy is given below

java.lang.Object
->java.util.AbstractCollection<E>
->->java.util.AbstractSet<E>
->->-> java.util.HashSet<E>
->->->->java.util.LinkedHashSet<E>

The implemented interfaces by LinkedHashSet Interface

Serializable, Cloneable, Iterable<E>, Collection<E>, Set<E>

The LinkedHashSet Interface provides the below given constructors

LinkedHashSet Constructor LinkedHashSet Constructor Description
LinkedHashSet()creates a empty HashSet which provides default initial capacity as 16 and load factor as 0.75
LinkedHashSet(Collection <? Extends E> c)creates a new HashSet which with elements specified in collection
LinkedHashSet(initialCapacity )creates a empty HashSet with provided initial capacity and load factor as 0.75
LinkedHashSet(initialCapacity, loadFactor )creates a empty HashSet with provided initial capacity and provided load factor

Methods for LinkedHashSet Interface

The below given are the methods for LinkedHashSet Interface

LinkedHashSet MethodLinkedHashSet Method Description
Spliterator<E>Creates a late-binding and fail-fast Spliterator over the elements in the set

LinkedHashSet Example in Java Collection

import java.util.*;  
class FruitsLinkedHashSet{  
 public static void main(String args[]){  
 //Create LinkedHashSet object as fruits
        LinkedHashSet<String> b=new LinkedHashSet();  
  // Add elements to fruits   
	   fruits.add("Apple");    
	   fruits.add("Orange");    
	   fruits.add("Banana");   
	   fruits.add("Gauva");  
	   fruits.add("Pears");  
	   Iterator<String> itr =fruits.iterator();  
	   while(itr.hasNext())  
	   {  
				System.out.println(itr.next());  
	   }   
 }  
}  

Leave a Reply

Your email address will not be published. Required fields are marked *

*