In Java Collections, List and Set are two fundamental interfaces used to store groups of objects. Both are part of the Collection Framework, but they differ in how they store, organize, and handle elements. Understanding both helps in choosing the right collection based on application requirements.
- List allows duplicate elements and maintains insertion order.
- Set does not allow duplicate elements and ensures unique values only.
- Both are interfaces in java.util and are implemented by different collection classes like ArrayList, LinkedHashSet, etc.
List Interface
A List in Java is an ordered collection that allows duplicate elements and maintains the insertion order of elements. It is part of the Java Collection Framework and provides positional access to elements.
- Maintains insertion order, so elements are stored in the sequence they are added.
- Allows duplicate elements, meaning the same value can appear multiple times.
- Provides index-based access, enabling retrieval and modification using positions.
- Supports dynamic resizing, so the size can grow or shrink as needed.
- Common implementations include ArrayList, LinkedList, and Vector.
Syntax
List<String> list = new ArrayList<>();
import java.util.*;
public class ListExample {
public static void main(String[] args) {
// Creating List using ArrayList
List<String> list = new ArrayList<>();
// Adding elements
list.add("Java");
list.add("Spring");
list.add("Hibernate");
list.add("Java"); // duplicate allowed
// Displaying list elements
System.out.println("List Elements: " + list);
// Accessing element using index
System.out.println("Element at index 1: " + list.get(1));
// Removing element
list.remove("Hibernate");
System.out.println("After removal: " + list);
}
}
Output
List Elements: [Java, Spring, Hibernate, Java] Element at index 1: Spring After removal: [Java, Spring, Java]
Explanation:
- We create a List using ArrayList, which is a class that implements the List interface.
- Elements are added using add() method, and duplicates are allowed (like "Java").
- We access elements using index with get() method and remove elements using remove() method.
- The list maintains insertion order, so output will appear in the same order elements were added.
Set Interface
A Set in Java is a collection that does not allow duplicate elements and is used when uniqueness of data is required. It is part of the Java Collection Framework and does not maintain insertion order in most implementations.
- Does not allow duplicate elements, ensuring all values are unique.
- Generally does not maintain insertion order (except LinkedHashSet).
- Provides faster operations for search and insertion in many implementations.
- Common implementations include HashSet, LinkedHashSet, and TreeSet.
Syntax
Set<String> set = new HashSet<>();
import java.util.*;
public class SetExample {
public static void main(String[] args) {
// Creating Set using HashSet
Set<String> set = new HashSet<>();
// Adding elements
set.add("Java");
set.add("Spring");
set.add("Hibernate");
set.add("Java"); // duplicate (will be ignored)
// Displaying set elements
System.out.println("Set Elements: " + set);
// Checking element presence
System.out.println("Contains Spring? " + set.contains("Spring"));
// Removing element
set.remove("Hibernate");
System.out.println("After removal: " + set);
}
}
Output
Set Elements: [Java, Hibernate, Spring] Contains Spring? true After removal: [Java, Spring]
Explanation:
- We create a Set using HashSet, which stores only unique elements.
- When we try to add duplicate values like "Java", it is ignored automatically.
- We use contains() to check if an element exists in the set.
- We use remove() to delete elements, but no index-based access is available.
Difference between List and Set
| Feature | List | Set |
|---|---|---|
| Definition | Ordered collection that allows duplicate elements | Collection that stores only unique elements |
| Package | java.util | java.util |
| Duplicates | Allows duplicate elements | Does not allow duplicate elements |
| Order Maintenance | Maintains insertion order | Does not maintain order (except LinkedHashSet) |
| Index Support | Supports index-based access (get, set) | Does not support index-based access |
| Access Method | Elements can be accessed using index | No index-based access, uses iterator |
| Performance | Slower for search operations in some cases | Faster for search operations (especially HashSet) |
| Null Values | Allows multiple null values | Allows only one null value (HashSet) |
| Implementation Classes | ArrayList, LinkedList, Vector | HashSet, LinkedHashSet, TreeSet |
| Usage | Used when order matters and duplicates are allowed | Used when uniqueness is required |