Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is List Interface?
2.1.
Properties of List Interface
2.2.
Implementation 
2.3.
Java
2.3.1.
Output
3.
What is Set Interface?
3.1.
Properties of Set Interface
3.2.
Implementation 
3.3.
Java
3.3.1.
Output
4.
Key Differences Between List and Set
5.
Difference Between List and Set in Java
6.
Frequently Asked Questions
6.1.
What is the difference between data set and list?
6.2.
What is the difference between set and list duplicates?
6.3.
How is set different from list in Python?
6.4.
What is the difference between list and set in Flutter?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Difference between List and Set in Java

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

A list allows duplicate elements and maintains their order, whereas a set ensures unique elements without any guaranteed order.

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?

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.

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.

Implementation 

  • 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);
   }
}

Output

Output
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is Set Interface?

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.

Also see, Duck Number in Java 

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.

Implementation 

  • 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);
}
}

Output

Output

Key Differences Between List and Set

In Java, the fundamental distinctions between List and Set revolve around ordering, duplicate handling, and access methods. A List, represented by interfaces such as ArrayList, LinkedList, Vector, and Stack, maintains the insertion order of elements and allows duplicates. Elements in a List can be accessed by their index, supporting both sequential and positional access. 

On the other hand, a Set, implemented by classes like HashSet, LinkedHashSet, and TreeSet, does not guarantee any specific order for its elements and prohibits the inclusion of duplicates. Sets do not provide index-based access, and their iteration order is not guaranteed. Lists are suitable when both order and the possibility of duplicate elements matter, whereas Sets are preferable when uniqueness is crucial, and the specific order of elements is not a consideration. 

Must Read, loop and while loop

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.

  List Set
Ordered Collection. Unordered Collection.
Elements can be repeated. Every element is unique.
An index can access elements. No Indexing is available.
Mutable. Mutable.
Allows Duplicates. Does not allow duplicates.
Uses square brackets to define [ ]. Uses curly braces { }.


If you want to check some Java Collections Interview Questions, click here.

Frequently Asked Questions

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.

How is set different from list in Python?

In Python, a set is an unordered collection of unique elements, while a list is an ordered collection that allows duplicates. Sets use curly braces, and lists use square brackets.

What is the difference between list and set in Flutter?

In Flutter, a list is an ordered collection often used for UI elements, while a set is an unordered collection of unique values. Lists use square brackets, and sets use curly braces.

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:

You may refer to our Guided Path on Code Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning, Ninjas!

Previous article
Difference between Primitive and Non-Primitive Datatypes
Next article
Constant
Live masterclass