Understanding Set vs. List in Java

Naveen Metta
3 min readApr 16, 2024

--

credit goes to the owner : https://techdifferences.com/difference-between-list-and-set-in-java.html
source : techdifferences.com

In Java, collections play a crucial role in storing and manipulating data. Two commonly used collection interfaces are Set and List. While both represent a group of elements, they have distinct characteristics and usage scenarios. In this article, we’ll delve into the differences between Set and List, explore their implementations, and provide comprehensive code examples to illustrate their usage.

Set in Java:

A Set is a collection that does not allow duplicate elements. It models the mathematical set abstraction and provides operations such as union, intersection, and difference. In Java, the Set interface is implemented by classes like HashSet, TreeSet, and LinkedHashSet.

HashSet Example:

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

public class Main {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("apple");
set.add("banana");
set.add("apple"); // Duplicate element, ignored

System.out.println(set); // Output: [banana, apple]
}
}

In this example, HashSet ensures that only unique elements are stored. The duplicate occurrence of “apple” is ignored.

TreeSet Example:

import java.util.Set;
import java.util.TreeSet;

public class Main {
public static void main(String[] args) {
Set<Integer> set = new TreeSet<>();
set.add(3);
set.add(1);
set.add(2);

System.out.println(set); // Output: [1, 2, 3]
}
}

TreeSet orders elements in natural order or using a comparator if provided. In this example, integers are ordered in ascending order.

LinkedHashSet Example:

import java.util.Set;
import java.util.LinkedHashSet;

public class Main {
public static void main(String[] args) {
Set<String> set = new LinkedHashSet<>();
set.add("apple");
set.add("banana");
set.add("apple"); // Duplicate element, preserved

System.out.println(set); // Output: [apple, banana]
}
}

LinkedHashSet preserves the insertion order of elements while ensuring uniqueness. In this example, the order of elements is maintained, and duplicate occurrence of “apple” is preserved.

List in Java:

A List is an ordered collection that allows duplicate elements. It maintains the insertion order of elements and provides operations for accessing elements by index, searching, and modifying elements. In Java, the List interface is implemented by classes like ArrayList, LinkedList, and Vector.

ArrayList Example:

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

public class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("apple"); // Duplicate element

System.out.println(list); // Output: [apple, banana, apple]
}
}

ArrayList allows duplicate elements and preserves the insertion order. In this example, “apple” is added twice.

LinkedList Example:

import java.util.List;
import java.util.LinkedList;

public class Main {
public static void main(String[] args) {
List<Integer> list = new LinkedList<>();
list.add(3);
list.add(1);
list.add(2);

System.out.println(list); // Output: [3, 1, 2]
}
}

LinkedList maintains elements in the order they are added. In this example, integers are printed in the order they were inserted.

Set vs. List:

Now, let’s compare Set and List based on key characteristics:

  1. Duplicates:
  • Set: Does not allow duplicate elements.
  • List: Allows duplicate elements.
  1. Ordering:
  • Set: Does not maintain any specific order of elements.
  • List: Preserves the insertion order of elements.
  1. Access by Index:
  • Set: Does not provide direct access by index as elements are not ordered.
  • List: Allows accessing elements by index using methods like get(index).
  1. Performance:
  • Set: Optimized for operations like add, remove, and contains, with constant time complexity for most operations in HashSet.
  • List: Optimized for accessing elements by index, with constant time complexity for get and set operations in ArrayList.
  1. Usage:
  • Set: Suitable for scenarios requiring uniqueness of elements, such as storing a collection of unique identifiers or eliminating duplicates from a collection.
  • List: Suitable for scenarios requiring ordered collection of elements, such as maintaining a sequence of items or implementing a stack or queue.

Conclusion:

In Java, choosing between Set and List depends on the specific requirements of your application. Use Set when uniqueness of elements is crucial and order doesn’t matter, while List is preferable when you need to maintain the order of elements and allow duplicates. Understanding the characteristics and differences between Set and List empowers developers to select the appropriate collection type for their data manipulation needs.

In summary, Set ensures uniqueness, List preserves order, and both provide essential functionalities for managing collections in Java applications. Whether you’re building data structures, processing data, or implementing algorithms, Set and List are indispensable tools in the Java programmer’s arsenal.

--

--

Naveen Metta

I'm a Full Stack Developer with 2.5 years of experience. feel free to reach out for any help : mettanaveen701@gmail.com