Difference Between LinkedList and LinkedHashSet in Java
Last Updated :
11 Dec, 2018
In this article you will learn difference between LinkedList and LinkedHashSet in java.
Prerequisite: LinkedList :
LinkedHashSet
LinkedList class implements the List and Deque interface and extends from AbstractSequentialList class. LinkedList class uses doubly linked list to store the elements. It provides a linked-list data structure.
Java LinkedHashSet class is a Hash table and Linked list implementation of the set interface. It inherits HashSet class and implements Set interface. LinkedHashSet implementations of Set interface, there are some differences exist between them.
Lets See Difference Between LinkedList and LinkedHashSet in Java
- Inheritance:
- How work internally
Java LinkedList class uses doubly linked list to store the elements while LinkedHashSet uses LinkedHashMap internally to store it’s elements.
- uniqueness:
LinkedList class can contain duplicate elements while LinkedHashSet contains unique elements only like HashSet.
- Insertion:
LinkedList in case of doubly linked list, we can add or remove elements from both side while LinkedHashSet insert at the end.
- Constructor:
LinkedList have two constructor LinkedList() and LinkedList(Collection o) while LinkedHashSet have four constructor HashSet(), HashSet(Collection c), LinkedHashSet(int capacity) and LinkedHashSet(int capacity, float fillRatio)
- Insertion, Removal And Retrieval Operations:
LinkedList Insertion, Removal And Retrieval Operations performance of order O(n) while LinkedHashSet also gives performance of order O(1) for insertion, removal and retrieval operations.
- compare the elements:
LinkedList use equals() method LinkedHashSet also uses equals() and hashCode() methods to compare the elements.
- Null Elements:
LinkedList allow any number of null values while LinkedHashSet also allows maximum one null element.
- Syntax:
LinkedList syntax is:
public class LinkedList extends AbstractSequentialList implements List, Deque, Cloneable, Serializable
LinkedHashSet syntax is:
public class LinkedHashSet extends HashSet implements Set, Cloneable, Serializable
Example of LinkedList:
JAVA
// Java code for Linked List implementation
import java.util.*;
public class Test {
public static void main(String args[])
{
// Creating object of class linked list
LinkedList<String> object = new LinkedList<String>();
// Adding elements to the linked list
// and see carefully element are duplicate, null
object.add("A");
object.add("B");
object.addLast("C");
object.addFirst("D");
object.add(2, "E");
object.add(null);
object.add(null);
System.out.println("Linked list : " + object);
System.out.println("Size of List:" + object.size());
}
}
Output:
Linked list : [D, A, E, B, C, null, null]
Size of List:7
Example of LinkedHashSet:
JAVA
import java.util.LinkedHashSet;
public class Demo {
public static void main(String[] args)
{
LinkedHashSet<String> linkedset = new LinkedHashSet<String>();
// Adding element to LinkedHashSet
linkedset.add("A");
linkedset.add("B");
linkedset.add("C");
linkedset.add("D");
System.out.println("Original LinkedHashSet:" + linkedset);
System.out.println("Size of LinkedHashSet = " + linkedset.size());
// trying to add duplicate
linkedset.add("A");
System.out.println("After adding duplicate element " + linkedset);
System.out.println("Size of LinkedHashSet = " + linkedset.size());
// trying to add null value more than one
linkedset.add(null);
linkedset.add(null);
System.out.println("After adding two null element " + linkedset);
System.out.println("Size of LinkedHashSet = " + linkedset.size());
}
}
Output:
Original LinkedHashSet:[A, B, C, D]
Size of LinkedHashSet = 4
After adding duplicate element [A, B, C, D]
Size of LinkedHashSet = 4
After adding two null element [A, B, C, D, null]
Size of LinkedHashSet = 5
Similar Reads
Difference Between List and Set in Java The List interface allows storing the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values are allowed to store. List preserves the insertion order, it allows positional access and insertion of elements. Declaration: public abstr
2 min read
Difference between List, Set and Map in Java List interface in Java is a sub-interface of the Java collections interface. It contains the index-based methods to insert, update, delete, and search the elements. It can have duplicate elements also. We can also store the null elements in the list. List preserves the insertion order, it allows pos
4 min read
Difference Between TreeSet and SortedSet in Java TreeSet is one of the implementations of the Navigable sub-interface. It is underlying data structure is a red-black tree. The elements are stored in ascending order and more methods are available in TreeSet compare to SortedSet. We can also change the sorting parameter using a Comparator. For examp
3 min read
Difference and similarities between HashSet, LinkedHashSet and TreeSet in Java In this article, we will learn, the difference between HashSet vs LinkedHashSet and TreeSet And similarities between LinkedHashSet and TreeSet. HashSet, LinkedHashSet, and TreeSet all implement the Set interface. So we have tried to list out the differences and similarities between HashSet, LinkedHa
6 min read
Differences between TreeMap, HashMap and LinkedHashMap in Java Prerequisite: HashMap and TreeMap in Java TreeMap, HashMap and LinkedHashMap: What's Similar? All offer a key->value map and a way to iterate through the keys. The most important distinction between these classes is the time guarantees and the ordering of the keys.All three classes HashMap, TreeM
5 min read