- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between LinkedList and LinkedHashSet in Java
LinkedList and LinkedHashSet both are one of the most important classes of Java Collection framework.
Following are the important differences between LinkedList and LinkedHashSet.
Sr. No. | Key | LinkedList | LinkedHashSet |
---|---|---|---|
1 | Implementation | LinkedList is the implementation of list and deque interface. | LinkedHashSet on other hand is the implementation of set interface and it inherits Hashset class. |
2 | Internal implementation | LinkedList internally implements or we can say uses doubly linked list to store the elements. | LinkedHashSet on other hand internally uses LinkedHashMap to store it’s elements. |
3 | Order of elements | As LinkedList internally used doubly linked list so we can add or remove elements from both ends in case of linkedlist | While LinkedHashset has Hashmap internally so elements could only be inserted at the end of LinkedHashset. |
4 | Duplicates | LinkedList could contains multiple number of duplicates in its collection. | On other hand duplicate elements are allowed in LinkedHashSet only like HashSet. |
5 | Index performance | LinkedList Insertion, Removal And Retrieval Operations performance of order O(n) | LinkedHashSet also gives performance of order O(1) for insertion, removal and retrieval operations. |
6 | Null Allowed | Any number of null value can be inserted in LinkedList without any restriction. | On other hand LinkedHashset allows only one null value in its collection,after which no null value is allowed to be added. |
Example of LinkedList vs LinkedHashset
JavaTester.java
import java.util.*; public class JavaTester { public static void main(String args[]){ LinkedList<String> object = new LinkedList<String>(); 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
JavaTester.java
import java.util.LinkedHashSet; public class JavaTester { public static void main(String[] args){ LinkedHashSet<String> linkedset = new LinkedHashSet<String>(); linkedset.add("A"); linkedset.add("B"); linkedset.add("C"); linkedset.add("D"); System.out.println("LinkedHashSet:" + linkedset); System.out.println("Size of LinkedHashSet = " + linkedset.size()); linkedset.add("A"); System.out.println("After adding duplicate element " + linkedset); System.out.println("Size of LinkedHashSet = " + linkedset.size()); linkedset.add(null); linkedset.add(null); System.out.println("After adding null element " + linkedset); System.out.println("Size of LinkedHashSet = " + linkedset.size()); } }
Output
LinkedHashSet:[A, B, C, D] Size of LinkedHashSet = 4 After adding duplicate element [A, B, C, D] Size of LinkedHashSet = 4 After adding null element [A, B, C, D, null] Size of LinkedHashSet = 5
- Related Articles
- What is the difference between ArrayList and LinkedList in Java?
- LinkedHashMap and LinkedHashSet in Java
- Differences between ArrayList and LinkedList in Java
- Get Size of Java LinkedHashSet
- LinkedList in Java
- Remove specified element from Java LinkedHashSet
- Remove all elements from Java LinkedHashSet
- Fetch key-valuepair from Java LinkedHashSet
- Iterate through elements of Java LinkedHashSet
- Clear LinkedList in Java
- Check if a particular element exists in Java LinkedHashSet
- Difference between Java and JavaScript.
- Difference between Go and Java.
- Difference Between C++ and Java
- Difference between Groovy and Java

Advertisements