In Java Collections Framework, List, Set, and Map are the most commonly used collection types for storing and managing data. Each serves a different purpose based on how elements are stored, accessed, and organized. Understanding their differences helps in selecting the most suitable collection for a particular requirement.
- List stores elements in an ordered sequence and allows duplicate values.
- Set stores only unique elements and does not allow duplicates.
- Map stores data in the form of key-value pairs, where each key must be unique.
Set Interface
The Set interface is a part of the java.util package and is used to store a collection of unique elements. It does not allow duplicate values and is commonly used when data uniqueness is required.
- Stores only unique elements.
- Duplicate values are automatically ignored.
- Provides faster searching compared to a List in many implementations.
- Common implementations are HashSet, LinkedHashSet, and TreeSet.
Syntax:
Set<DataType> setName = new HashSet<>();
import java.util.HashSet;
import java.util.Set;
public class SetExample {
public static void main(String[] args) {
// Creating a Set
Set<String> fruits = new HashSet<>();
// Adding elements
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Mango");
fruits.add("Orange");
fruits.add("Apple"); // Duplicate element
// Displaying Set elements
System.out.println("Elements in Set: " + fruits);
// Checking if an element exists
System.out.println("Contains Mango? " + fruits.contains("Mango"));
// Removing an element
fruits.remove("Orange");
// Displaying updated Set
System.out.println("After removing Orange: " + fruits);
// Size of Set
System.out.println("Total Elements: " + fruits.size());
}
}
Output
Elements in Set: [Apple, Mango, Orange, Banana] Contains Mango? true After removing Orange: [Apple, Mango, Banana] Total Elements: 3
List Interface
The List interface is a part of the java.util package and is used to store an ordered collection of elements. It allows duplicate values and preserves the insertion order of elements.
- Maintains the insertion order of elements.
- Allows duplicate values.
- Supports index-based access to elements.
- Common implementations are ArrayList, LinkedList, and Vector.
Syntax:
List<DataType> listName = new ArrayList<>();
import java.util.*;
public class GFG {
public static void main(String args[])
{
// Creating a List
List<String> al = new ArrayList<>();
// Adding elements in the List
al.add("mango");
al.add("orange");
al.add("Grapes");
// Iterating the List
// element using for-each loop
for (String fruit : al)
System.out.println(fruit);
}
}
Output :
mango
orange
Grapes
Map Interface
The Map interface is a part of the java.util package and is used to store data in the form of key-value pairs. Each key must be unique, while values can be duplicated.
- Stores data as key-value pairs.
- Keys must be unique.
- Values can be duplicated.
- Common implementations are HashMap, LinkedHashMap, and TreeMap.
Syntax:
Map<KeyType, ValueType> mapName = new HashMap<>();
import java.util.*;
class MapExample {
public static void main(String args[])
{
// Creating object for Map.
Map<Integer, String> map
= new HashMap<Integer, String>();
// Adding Elements using Map.
map.put(100, "Amit");
map.put(101, "Vijay");
map.put(102, "Rahul");
// Elements can traverse in any order
for (Map.Entry m : map.entrySet()) {
System.out.println(m.getKey() + " "
+ m.getValue());
}
}
}
Output :
100 Amit
101 Vijay
102 Rahul
List vs Set vs Map
| Feature | List | Set | Map |
|---|---|---|---|
| Interface Package | java.util.List | java.util.Set | java.util.Map |
| Data Storage | Stores elements | Stores unique elements | Stores key-value pairs |
| Duplicate Values | Allowed | Not Allowed | Duplicate keys not allowed, values allowed |
| Insertion Order | Maintains insertion order | Depends on implementation | Depends on implementation |
| Index-Based Access | Supported | Not Supported | Not Supported |
| Key-Value Pair | No | No | Yes |
| Null Values | Multiple null values allowed | Usually one null element allowed | One null key (HashMap) and multiple null values allowed |
| Retrieval Method | By index | By element | By key |
| Primary Use | Ordered collection of data | Storing unique data | Associating keys with values |
| Common Implementations | ArrayList, LinkedList LinkedList, Vector | HashSet, LinkedHashSet, TreeSet | HashMap, LinkedHashMap, TreeMap |