In this article you will learn difference between LinkedList and LinkedHashSet in java.
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
- How work internally
Java LinkedList class uses doubly linked list to store the elements while LinkedHashSet uses LinkedHashMap internally to store it’s elements.
LinkedList class can contain duplicate elements while LinkedHashSet contains unique elements only like HashSet.
LinkedList in case of doubly linked list, we can add or remove elements from both side while LinkedHashSet insert at the end.
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.
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:
Linked list : [D, A, E, B, C, null, null] Size of List:7
Example of LinkedHashSet:
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
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Difference and similarities between HashSet, LinkedHashSet and TreeSet in Java
- LinkedHashMap and LinkedHashSet in Java
- LinkedHashSet in Java with Examples
- LinkedHashSet contains() method in Java with Examples
- LinkedHashSet clear() method in Java with Examples
- LinkedHashSet removeAll() method in Java with Example
- LinkedHashSet toArray(T) method in Java with Example
- LinkedHashSet toArray() method in Java with Example
- LinkedHashSet equals() method in Java with Example
- LinkedHashSet retainAll() method in Java with Example
- LinkedHashSet toString() method in Java with Example
- LinkedHashSet containsAll() method in Java with Example
- LinkedHashSet hashCode() method in Java with Example
- LinkedHashSet add() method in Java with Examples
- LinkedHashSet contains() method in Java
- LinkedHashSet clone() method in Java
- LinkedHashSet remove() method in Java
- LinkedHashSet size() method in Java
- LinkedHashSet isEmpty() method in Java
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.