Table of contents
1.
Introduction
2.
What is List Interface in Java?
2.1.
Syntax of list in Java
2.2.
Properties of List Interface
2.3.
Code Implementation of list in Java
2.4.
Java
3.
What is Set Interface in Java?
3.1.
Syntax of Set in Java
3.2.
Properties of Set Interface
3.3.
Code Implementation of Set in Java
3.4.
Java
4.
Difference Between List and Set in Java
5.
Frequently Asked Questions
5.1.
Why is set better than list in Java?
5.2.
When to use set instead of list?
5.3.
What is the difference between data set and list?
5.4.
What is the difference between set and list duplicates?
6.
Conclusion
Last Updated: Dec 2, 2024
Easy

Difference between List and Set in Java

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java, both List and Set are interfaces used to store collections of elements, but they serve different purposes. A List allows duplicate elements and maintains insertion order, while a Set does not allow duplicates and may not preserve order. Understanding the key differences between these two can help in choosing the right one based on specific use cases.

Introduction

In this article, we will check the difference between list and set and look at the properties, implementation and performance of each. 
First, we will go through the definition of list and set.

What is List Interface in Java?

list is a collection of elements, where each element has an index that starts from 0. See the image below for a better understanding. In Java, a List is an interface that is part of the Java Collections Framework. It represents an ordered collection of elements, allowing for the storage and manipulation of data. The List interface extends the Collection interface and provides several methods for working with lists of objects.

LIST interface

It is also mutable, so you can change the elements whenever you require. And it can also have duplicate elements with different data types. One important thing to remember is to initialise the list with the square bracket. See the below code for a better understanding.

List = [1, 1, "NINJA", 5.6]


We will move to the Set definition to better understand the difference between list and set.

Syntax of list in Java

List<Type> listName = new ArrayList<>();

Properties of List Interface

  • Lists are a data structure that stores an ordered collection of elements.
  • Each element in a list has an index for addressing.
  • It is mutable, so you can change its elements any time you want.
  • You can add new elements to a list using the append() method or insert() method.
  • You can remove elements from a list using the remove() method or pop() method.
  • Here it is easy to access individual elements in a list using their index.
  • Here we can also concatenate two lists using this ( + ) operator or the extend () method.
  • Here you can sort lists also using the sort() method.

Code Implementation of list in Java

  • Java

Java

import java.util.ArrayList;
import java.util.List;

public class Solution {
public static void main(String[] args) {
      
// New list
List<String> coders = new ArrayList<String>();


// Adding at the end of the list
coders.add("Master Coder");
coders.add("Master Coder");
coders.add("Experienced Coder");

System.out.println("Initial list of coders: " + coders);

// Updating at level
coders.set(1, "Intermediate Coder");


// Removing
coders.remove(2);
System.out.println("Removed coder at index 2");


// Removing and returning the last coder in the list
String lastCoder = coders.remove(coders.size() - 1);
System.out.println("Removed last coder: " + lastCoder);


// Getting the first coder
String firstCoder = coders.get(0);
System.out.println("First coder in the list: " + firstCoder);

// Printing final list
System.out.println("Updated list of coders: " + coders);
   }
}
You can also try this code with Online Java Compiler
Run Code

Output

Initial list of coders: [Master Coder, Master Coder, Experienced Coder]
Removed coder at index 2
Removed last coder: Intermediate Coder
First coder in the list: Master Coder
Updated list of coders: [Master Coder]

What is Set Interface in Java?

set is a collection of unique elements. It is unordered, so there is no indexing as in the list from 0 to n. See image below for a better understanding.

SET interface

It is also mutable, so you can change the elements whenever required. But it can not have duplicate elements. It can have elements with different data types. One important thing to remember here is to initialise a set with the curly bracket. See the below code for a better understanding.

Set = {1, 5, "NINJA", 5.6}

We will move to the properties and implementation of each set and list to better understand the difference between list and set.

Syntax of Set in Java

Set<Type> setName = new HashSet<>();

Properties of Set Interface

  • The Set is also similar to the List, but it is unordered.
  • It is also mutable, so you can change its elements anytime.
  • It is important to note that, as a list, it cannot store duplicate elements.
  • Here, the elements in a set are not assigned an index.
  • It is implemented using curly braces {} or the set() constructor.
  • You can add elements to a set using the add() method or the union() method to combine two sets.
  • You can remove elements from a set using the remove() method or discard() method.

Code Implementation of Set in Java

  • Java

Java

import java.util.HashSet;
import java.util.Set;

public class Solution {
public static void main(String[] args) {

Set<Integer> ninjaLuckyNumbers = new HashSet<>();
ninjaLuckyNumbers.add(7);
ninjaLuckyNumbers.add(42);

System.out.println("Initial set: " + ninjaLuckyNumbers);
// This element is a duplicate and will not be added
ninjaLuckyNumbers.add(42);
ninjaLuckyNumbers.add(17);

// Check if an element is present in the set
System.out.println(ninjaLuckyNumbers.contains(42)); // prints true
System.out.println(ninjaLuckyNumbers.contains(10)); // prints false

// Remove an element from the set
ninjaLuckyNumbers.remove(42);
System.out.println(ninjaLuckyNumbers); // prints [17, 7]

// Clear all elements from the set
ninjaLuckyNumbers.clear();
System.out.println("Updated set: " + ninjaLuckyNumbers);
}
}
You can also try this code with Online Java Compiler
Run Code

Output

Initial set: [42, 7]
true
false
[17, 7]
Updated set: []

Difference Between List and Set in Java

Below is the difference between list and set. In the tabular form, it will be easy for you to understand.

Parameters  ListSet
DefinitionObjects are minimally dependent on each other.Objects are heavily dependent on each other.
Order of Elements Maintains the insertion order of elements.Does not guarantee any specific order of elements.
Index AccessAn index can access elements.No Indexing is available.
MutabilityAllows both mutable and immutable implementations.Allows both mutable and immutable implementations, but mutation may require uniqueness enforcement.
Duplicates Lists allow duplicate elementsSets do not allow duplicate elements.
SyntaxUses square brackets to define [ ].Uses curly braces { }.

Frequently Asked Questions

Why is set better than list in Java?

A Set is better than a List in Java when you need to ensure unique elements and don't require index-based access. Sets eliminate duplicates automatically and can provide more efficient operations for checking presence and removing elements.

When to use set instead of list?

Use a Set instead of a List when you need to ensure all elements are unique and don't require ordering or indexing. Sets are ideal for operations involving uniqueness checks, such as eliminating duplicates or fast membership testing.

What is the difference between data set and list?

A dataset is a collection of related data, while a list is an ordered collection of elements. A dataset is broader, encompassing various data types and structures.

What is the difference between set and list duplicates?

Sets do not allow duplicates; each element must be unique. Lists, however, permit duplicate elements, allowing multiple instances of the same value.

Conclusion

In this article, we learned about the difference between list and set. Understanding the distinctions between Lists and Sets in Java is pivotal for effective data management and algorithm design. Lists, including implementations like ArrayList and LinkedList, maintain the order of elements and allow duplicates, making them suitable for scenarios where the sequence matters. On the other hand, Sets, exemplified by HashSet and TreeSet, prioritize uniqueness and do not guarantee any specific order. 
If you want to learn more, follow these articles below to build fundamentals:

Live masterclass