Java List, Set, and Map Interfaces – Complete Guide with Examples - Textnotes

Java List, Set, and Map Interfaces – Complete Guide with Examples


Learn the core Java Collections interfaces: List, Set, and Map, their differences, implementations, and how to use them to store, access, and manipulate data efficiently.

List, Set, and Map Interfaces in Java – Complete Detailed Tutorial

The Java Collections Framework (JCF) provides three main interfaces to handle collections: List, Set, and Map. Each has its own characteristics and usage.

1. List Interface

  1. Ordered collection (maintains insertion order)
  2. Allows duplicates
  3. Common implementations: ArrayList, LinkedList, Vector, Stack
  4. Supports index-based access

Key Methods:

MethodDescription
add(E e)Adds element
get(int index)Retrieves element at index
remove(int index)Removes element at index
size()Returns number of elements
contains(Object o)Checks if element exists

Example – Using List Interface


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

public class Main {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Apple"); // duplicate allowed

System.out.println("Fruits: " + fruits);
System.out.println("Element at index 1: " + fruits.get(1));
fruits.remove("Banana");
System.out.println("After removal: " + fruits);
}
}

Output:


Fruits: [Apple, Banana, Orange, Apple]
Element at index 1: Banana
After removal: [Apple, Orange, Apple]

2. Set Interface

  1. Unordered collection (does not maintain insertion order in HashSet)
  2. No duplicate elements allowed
  3. Common implementations: HashSet, LinkedHashSet (maintains insertion order), TreeSet (sorted)

Key Methods:

MethodDescription
add(E e)Adds element
remove(Object o)Removes element
contains(Object o)Checks if element exists
size()Returns number of elements

Example – Using Set Interface


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

public class Main {
public static void main(String[] args) {
Set<String> fruits = new HashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Apple"); // duplicate ignored

System.out.println("Fruits: " + fruits);
System.out.println("Contains Banana? " + fruits.contains("Banana"));
fruits.remove("Banana");
System.out.println("After removal: " + fruits);
}
}

Output:


Fruits: [Apple, Orange, Banana]
Contains Banana? true
After removal: [Apple, Orange]

Explanation:

  1. Duplicates are ignored
  2. HashSet does not maintain insertion order

3. Map Interface

  1. Stores key-value pairs
  2. Keys are unique, values can be duplicated
  3. Common implementations: HashMap, LinkedHashMap, TreeMap

Key Methods:

MethodDescription
put(K key, V value)Adds a key-value pair
get(Object key)Retrieves value for key
remove(Object key)Removes key-value pair
containsKey(Object key)Checks if key exists
size()Returns number of entries

Example – Using Map Interface


import java.util.HashMap;
import java.util.Map;

public class Main {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "Apple");
map.put(2, "Banana");
map.put(3, "Orange");
map.put(2, "Grapes"); // key 2 updated

System.out.println("Map: " + map);
System.out.println("Value for key 2: " + map.get(2));
map.remove(3);
System.out.println("After removal: " + map);
}
}

Output:


Map: {1=Apple, 2=Grapes, 3=Orange}
Value for key 2: Grapes
After removal: {1=Apple, 2=Grapes}

Explanation:

  1. Keys are unique
  2. Values can be updated
  3. Maintains key-value mapping

4. Key Points

  1. List: ordered, allows duplicates, index-based
  2. Set: unordered, no duplicates, unique elements
  3. Map: key-value pairs, unique keys, allows duplicate values
  4. All interfaces provide common methods like add, remove, contains
  5. Can use Iterator to traverse List and Set; entrySet() for Map

5. Summary

  1. List, Set, and Map are the core interfaces of Collections Framework
  2. Chosen based on requirement: order, uniqueness, or key-value mapping
  3. Implementations provide optimized and ready-to-use data structures