Difference Between List and Set in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

The primary difference between list and set is that a list allows duplicate elements and maintains their order, while a set ensures element uniqueness without any guaranteed order.

Since lists are ordered, position indexing is allowed in them. However, in unordered items like sets, positional indexing is not possible.

What is List Interface?

List interface in Java is a child interface for Collection objects. As mentioned previously, lists are ordered collections of data. Their ordered nature ensures that their insertion order remains intact. They can be indexed and accessed based on their positions.

Syntax

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Example

The output for the following code is:

What is Set Interface?

The set interface in java.util package extends the Collection interface. Sets in java are unordered collections of objects. These restrict duplication of values. Since they are unordered, positional indexing is not allowed in sets.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax

Example

The output for the following code is:

Key Differences Between List and Set

Duplicates:

  • List: Allows duplicate elements.
  • Set: Does not allow duplicate elements.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Order:

  • List: Maintains the order of elements.
  • Set: No guaranteed order (except ordered variants like LinkedHashSet).

Index Access:

  • List: Supports access by index, e.g., get(index).
  • Set: Doesn’t support index-based access.

Examples:

  • List: ArrayList, LinkedList.
  • Set: HashSet, TreeSet.

Difference Between List and Set in Java

List InterfaceSet Interface
Lists allow duplicate valuesSets can't have duplicate values
Lists can be indexed and positionally accessedSets cannot be indexed and cannot be accessed based on their positions
Multiple null elements can be storedOnly one null element can be stored at a time
List implementations are ArrayList, LinkedList, Vector, Stack.Set implementations are HashSet, LinkedHashSet.
The insertion order is maintained by the List.It doesn’t maintain the insertion order of elements.
List is often used when we need to frequently access the elements using their indices.It is used when we need to store distinct elements.
The listiterator() is used to iterate the List elements.The iterator() method is used to iterate Set elements.

When to Use Sets and When to Use Lists?

Lists could be implemented in ArrayList, LinkedList, Vector, and Stack. Lists are best used when a small dataset is at hand

Sets could be implemented in HashSet, LinkedHashSet, etc. Sets are best used when a larger dataset needs to be processed.

Learn More

To learn more about collections in Java, check this out.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Conclusion

  • A List interface is an ordered collection of values in which values may or may not be duplicated
  • In Java, a set is a collection that does not allow duplicate elements and has no guaranteed order for its elements.
  • In Java, when comparing “list vs set” a list supports ordered collections and allows duplicates, whereas a set emphasizes uniqueness without any specific order.
  • Lists allow duplicate values while Sets don't.
  • Lists allow positional indexing. The same is not allowed in sets.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more