Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Difference Between LinkedList and LinkedHashSet in Java
Next article icon

Difference Between LinkedList and LinkedHashSet in Java

Last Updated : 11 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
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
  1. Inheritance:
  2. How work internally Java LinkedList class uses doubly linked list to store the elements while LinkedHashSet uses LinkedHashMap internally to store it’s elements.
  3. uniqueness: LinkedList class can contain duplicate elements while LinkedHashSet contains unique elements only like HashSet.
  4. Insertion: LinkedList in case of doubly linked list, we can add or remove elements from both side while LinkedHashSet insert at the end.
  5. 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)
  6. 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.
  7. compare the elements: LinkedList use equals() method LinkedHashSet also uses equals() and hashCode() methods to compare the elements.
  8. Null Elements: LinkedList allow any number of null values while LinkedHashSet also allows maximum one null element.
  9. 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  

Next Article
Difference Between LinkedList and LinkedHashSet in Java

R

rajput-ji
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Technical Scripter 2018
  • java-LinkedList
  • java-LinkedHashSet
  • Java-List-Programs
  • Java-Set-Programs
Practice Tags :
  • Java

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences