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
}
LinkedHashSet Features :
- Java LinkedHashSet class extends HashSet class.
- Java LinkedHashSet class implements Set interface.
- Java LinkedHashSet class contains unique elements like HashSet.
- One NULL element is allowed in LinkedHashSet.
- Java LinkedHashSet class is an ordered collection which is the order in which elements were inserted into the set.
- Java LinkedHasHSet class offers constant time performance for the add, remove, contains and size operations , such Like HashSet.
- 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(…)); - LinkedHashSet also implements Searlizable and Cloneable interfaces.
- 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.
- LinkedHashSet() – Constructs a new, empty linked hash set with the default initial capacity (16) and load factor (0.75).
- LinkedHashSet(Collection<? extends E> c) – Constructs a new linked hash set with the same elements as the given collection.
- LinkedHashSet(int initialCapacity) – Constructs a new, empty linked hash set with the given initial capacity and the default load factor (0.75).
- 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
- add(E e) – This method adds the given element to this set if it is not already present.
- clear() – This method removes all of the elements from this set.
- clone() – This method returns a shallow copy of this LinkedHashSet instance: the elements themselves are not cloned.
- contains(Object o) – This method returns true if this set contains the specified element.
- isEmpty() – This method returns true if this set contains no elements.
- remove(Object o) – This method removes the given element from this set if it is present.
- size() – This method returns the number of elements in this set (its cardinality).
- spliterator() – It is creates a late-binding and fail-fast Spliterator over the elements in this set.
Java LinkedHashSet Other Tutorials
- How to Create LinkedHashSet In Java
- How to storing Java Object In LinkedHashSet
- How to Convert a LinkedHashSet to ArrayList
- How to convert array to LinkedHashSet
- How to copy all element of a LinkedHashset to other
- How To Remove Element of LinkedHashset
- How to remove all element from LinkedHashSet
- How to Remove Set From LinkedHashSet
- How To Convert LinkedHashset To Array
- How to find does LinkedHashSet contains elements or not
- How to count number of element in LinkedHashSet
- How to check LinkedHashSet Empty Or not