Java LinkedHashSet Examples

Java LinkedHasHSet class is an implementation of a Set interface. It is very similar to HashSet class,
except if offers the insertion iteration order.It internally uses Hash Table and Linked List.LinkedHasHSet
class doesn’t allows duplicate elements.It allows you to insert one and only one null value.

LinkedHasHSet Hierarchy

Java LinkedHashSet class extends HashSet class and implements Set interface. The Java Set interface inherits Collection and Iterable interfaces in hierarchical order.Declaration for HashSet class as below

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

//More Code  
}
java linkedhashset class hierarchy
java LinkedHashSet class hierarchy

LinkedHashSet Features :

  1. Java LinkedHashSet class extends HashSet class.
  2. Java LinkedHashSet class implements Set interface.
  3. Java LinkedHashSet class contains unique elements like HashSet.
  4. One NULL element is allowed in LinkedHashSet.
  5. Java LinkedHashSet class is an ordered collection which is the order in which elements were inserted into the set.
  6. Java LinkedHasHSet class offers constant time performance for the add, remove, contains and size operations , such Like HashSet.
  7. Java LinkedHashSet is non-synchronize so it is not thread-safe. You must explicitly synchronize concurrent modifications to the LinkedHashSet as
    Set s = Collections.synchronizedSet(new LinkedHashSet(…));
  8. LinkedHashSet also implements Searlizable and Cloneable interfaces.
  9. The iterators returned by this class’s iterator method are fail-fast.It means if the set is modified at any time after the iterator is created, in any way except through the iterator’s own remove method, the iterator will throw a ConcurrentModificationException.

Constructors of Java LinkedHashSet class

Java LinkedHashSet Class have four type constructors as bellow.

  1. LinkedHashSet() – Constructs a new, empty linked hash set with the default initial capacity (16) and load factor (0.75).
  2. LinkedHashSet(Collection<? extends E> c) – Constructs a new linked hash set with the same elements as the given collection.
  3. LinkedHashSet(int initialCapacity) – Constructs a new, empty linked hash set with the given initial capacity and the default load factor (0.75).
  4. LinkedHashSet(int initialCapacity, float loadFactor) – Constructs a new, empty linked hash set with the given initial capacity and load factor.

Creating and add element to LinkedHashSet

Now we will create Java LinkedHashSet and add element to it. Let’s see this by code.

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class LinkedHashSetExample {

	public static void main(String[] args) {
		
		/*
		 * Constructs a new, empty linked hash set with the 
		 * default initial capacity (16) and load factor (0.75).
		 */
		Set<String> linkedHashSet = new LinkedHashSet<String>();
		
		/*
		 * Adds the specified element to this set if it is not already present 
		 */
		linkedHashSet.add("Java");
		linkedHashSet.add("php");
		linkedHashSet.add("andoid");
		
		//display element
		System.out.println("LinkedHashSet element ="+ linkedHashSet);
		
		List<String> list = new ArrayList<String>();
		list.add("Java");
		list.add("html");
		list.add("spring");
		
		/*
		 *Constructs a new linked hash set with the same elements as 
		 *the specified collection. The linked hash set is created 
		 *with an initial capacity sufficient to hold the elements in
		 * the specified collection and the default load factor (0.75).
		 */
		Set<String> linkedHashSetFromList = new LinkedHashSet<>(list);
	
		//display element from LinkedHashSet
		System.out.println("LinkedHashSet Element ="+ linkedHashSetFromList);
        
	}
	
}

Output :

LinkedHashSet element =[Java, php, andoid]
LinkedHashSet Element =[Java, html, spring]

Iterating over a LinkedHashSet

We can iterate LinkedHashSet in Java using different ways. Here will discuss these below ways

  • Iterate over a LinkedHashSet using iterator()
  • Iterate over a LinkedHashSet using simple for-each loop.

Let’s see these concept by code

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

public class LinkedHashSetIterating {

	public static void main(String[] args) {

		// Constructs a new, empty set
		Set<String> linkedHashSet = new LinkedHashSet<String>();

		// add element in it
		linkedHashSet.add("java");
		linkedHashSet.add("php");
		linkedHashSet.add("android");

		System.out
		.println("====> Iterate LinkedHashSet using iterator() ====>");
		Iterator<String> linkedHashSetIterator = linkedHashSet.iterator();
		while (linkedHashSetIterator.hasNext()) {
			String element = linkedHashSetIterator.next();
			System.out.println("element is =" + element);
		}

		System.out
	     .println("====> Iterate LinkedHashSet using for for each loop ====>");
		for (String element : linkedHashSet) {
			System.out.println("element is =" + element);
		}

	}
}

Output :

====> Iterate LinkedHashSet using iterator() ====>
element is =java
element is =php
element is =android
====> Iterate LinkedHashSet using for for each loop ====>
element is =java
element is =php
element is =android

How to remove elements from a LinkedHashSet ?

We can use remove method for remove method form LinkedHashSet.

package com.demo;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class LinkedHashSetExample {

	public static void main(String[] args) {
		
		/*
		 * Constructs a new, empty linked hash set with the 
		 * default initial capacity (16) and load factor (0.75).
		 */
		Set<String> linkedHashSet = new LinkedHashSet<String>();
		
		/*
		 * Adds the specified element to this set if it is not already present 
		 */
		linkedHashSet.add("Java");
		linkedHashSet.add("php");
		linkedHashSet.add("andoid");
		linkedHashSet.add("spring");
		linkedHashSet.add("html");
		linkedHashSet.add("javascript");
		
		//display element
		System.out.println("LinkedHashSet element ="+ linkedHashSet);
		
		/*
		 * Removes the specified element from this set if it is present 
		 */
		linkedHashSet.remove("andoid");
		
		//display element 		
		System.out.println("Afer remove lement LinkedHashSet="+ linkedHashSet);
        
	}
	
}

Output :

LinkedHashSet element =[Java, php, andoid, spring, html, javascript]
Afer remove lement LinkedHashSet=[Java, php, spring, html, javascript]

How to Count Element In LinkedHashSet ?

package com.demo;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class LinkedHashSetExample {

	public static void main(String[] args) {
		
		/*
		 * Constructs a new, empty linked hash set with the 
		 * default initial capacity (16) and load factor (0.75).
		 */
		LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>();
		
		/*
		 * Adds the specified element to this set if it is not already present 
		 */
		linkedHashSet.add("Java");
		linkedHashSet.add("php");
		linkedHashSet.add("andoid");
		linkedHashSet.add("spring");
		linkedHashSet.add("html");
		linkedHashSet.add("javascript");
		
		//display element
		System.out.println("LinkedHashSet element ="+ linkedHashSet);
		
		//Returns the number of elements in this set (its cardinality).
		System.out.println("size of LinkedHashSet ="+ linkedHashSet.size());
        
	}
	
}

Output :

LinkedHashSet element =[Java, php, andoid, spring, html, javascript]
size of LinkedHashSet =6

LinkedHashSet Method

  1. add(E e) – This method adds the given element to this set if it is not already present.
  2. clear() – This method removes all of the elements from this set.
  3. clone() – This method returns a shallow copy of this LinkedHashSet instance: the elements themselves are not cloned.
  4. contains(Object o) – This method returns true if this set contains the specified element.
  5. isEmpty() – This method returns true if this set contains no elements.
  6. remove(Object o) – This method removes the given element from this set if it is present.
  7. size() – This method returns the number of elements in this set (its cardinality).
  8. spliterator() – It is creates a late-binding and fail-fast Spliterator over the elements in this set.

Java LinkedHashSet Other Tutorials

Reference :

Leave a Reply

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

− 6 = 3