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 List and Set in Java
Next article icon

Difference Between List and Set in Java

Last Updated : 29 Apr, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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 abstract interface List extends Collection

The set interface in the java.util package and extends Collection interface is an unordered collection of objects in which duplicate values cannot be stored. It is an interface that implements the maths set. This interface contains the methods inherited from the Collection interface and adds a feature that restricts to insert the duplicate elements. 

Declaration: The Set interface is declared as:

public interface Set extends Collection

Example:

Input :  Add Elements = [1, 2, 3, 1] Output:  Set = [1, 2, 3]      List = [1, 2, 3, 1]  Input :  Add Elements = [a, b, d, b] Output:  Set = [a, b, d]      List = [a, b, d, b]

Below is the illustration of Set and List :

Java
// Implementation of List and Set in Java import java.io.*; import java.util.*;  class GFG {     public static void main(String[] args)     {         // List declaration         List<Integer> l = new ArrayList<>();         l.add(5);         l.add(6);         l.add(3);         l.add(5);         l.add(4);          // Set declaration         Set<Integer> s = new HashSet<>();         s.add(5);         s.add(6);         s.add(3);         s.add(5);         s.add(4);          // printing list         System.out.println("List = " + l);         // printing Set         System.out.println("Set = " + s);     } } 

Output
List = [5, 6, 3, 5, 4] Set = [3, 4, 5, 6]

Difference between List and Set:

ListSet
1. The List is an indexed sequence.1. The Set is an non-indexed sequence.
2. List allows duplicate elements2. Set doesn't allow duplicate elements.
3. Elements by their position can be accessed.3. Position access to elements is not allowed.
4. Multiple null elements can be stored.4. Null element can store only once.
5. List implementations are ArrayList, LinkedList, Vector, Stack5. Set implementations are HashSet, LinkedHashSet.

Next Article
Difference Between List and Set in Java

T

tejswini2000k
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Difference Between
  • Technical Scripter 2020
  • Java-Collections
  • java-set
  • java-list
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

    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 List and Set in C#
    The list is C# is the same as the list in JAVA. Basically, it is a type of object which can store variables. But in difference with objects, it stores the variables only in a specific order. Following is the syntax from which we can declare variables: Syntax: List<int> numbers = new List<in
    2 min read
    Difference between List and ArrayList in Java
    A Collection is a group of individual objects represented as a single unit. Java provides a Collection Framework which defines several classes and interfaces to represent a group of objects as a single unit This framework consists of the List Interface as well as the ArrayList class. In this article
    4 min read
    Difference Between EnumSet and TreeSet in Java
    EnumSet and TreeSet both are the classes defined inside the collection framework. But there are few differences exists between them. In this article, we have tried to cover all these differences between them. 1. EnumSet: EnumSet is a specialized implementation of the Set interface for enumeration ty
    3 min read
    Difference Between LinkedList and LinkedHashSet in Java
    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
    3 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