Skip to main content

9.2 List, Set, and Map Interfaces

Overview of Collections

The Java Collections Framework is a unified architecture for representing and managing collections in Java. It includes interfaces, implementations, and algorithms that allow developers to manage data efficiently and consistently.

Key Benefits of Collections Framework

  • Performance: Optimized for handling dynamic data structures.
  • Interoperability: Provides a common interface for various data structures.
  • Flexibility: Offers a wide range of data structures to meet different needs.

The main interfaces in the Java Collections Framework are List, Set, and Map, which are specialized for different types of data operations.


List Interface

The List interface represents an ordered collection (also known as a sequence). It allows duplicate elements and provides positional access to elements.

Common List Implementations

  1. ArrayList - A resizable array, which is not synchronized.
  2. LinkedList - Implements a doubly linked list, providing faster insertion and deletion.
  3. Vector - A synchronized, resizable array.

Example: Using an ArrayList

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

public class ListExample {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");

// Accessing elements
System.out.println("First name: " + names.get(0)); // Output: Alice

// Iterating through the list
for (String name : names) {
System.out.println(name);
}
}
}

In this example, we create an ArrayList of strings, add elements to it, and iterate over the list.


Set Interface

The Set interface represents a collection that cannot contain duplicate elements. It models the mathematical set abstraction.

Common Set Implementations

  1. HashSet - A set backed by a hash table, does not maintain order.
  2. LinkedHashSet - Maintains insertion order.
  3. TreeSet - Orders elements based on their values.

Example: Using a HashSet

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

public class SetExample {
public static void main(String[] args) {
Set<String> fruits = new HashSet<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Apple"); // Duplicate, won't be added

// Iterating through the set
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}

In this example, we use a HashSet to store fruit names. Even though we attempt to add "Apple" twice, it only appears once in the output.


Map Interface

The Map interface represents a mapping between a key and a value. Maps do not allow duplicate keys, but each key can map to a single value.

Common Map Implementations

  1. HashMap - A hash table-based implementation, does not maintain order.
  2. LinkedHashMap - Maintains the order of insertion.
  3. TreeMap - Orders keys in ascending order.

Example: Using a HashMap

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

public class MapExample {
public static void main(String[] args) {
Map<Integer, String> employeeMap = new HashMap<>();
employeeMap.put(1, "Alice");
employeeMap.put(2, "Bob");
employeeMap.put(3, "Charlie");

// Accessing values by key
System.out.println("Employee with ID 2: " + employeeMap.get(2)); // Output: Bob

// Iterating through the map
for (Map.Entry<Integer, String> entry : employeeMap.entrySet()) {
System.out.println("ID: " + entry.getKey() + ", Name: " + entry.getValue());
}
}
}

In this example, we use a HashMap to store employee IDs and names. We retrieve a value by its key and iterate through the map entries.


Summary

  • List allows ordered collections with duplicates, commonly implemented by ArrayList.
  • Set enforces unique elements without duplicates, often used with HashSet.
  • Map pairs keys with values, useful for key-based data access, commonly implemented by HashMap.

Each of these interfaces serves different needs in data handling, enabling developers to write efficient and clean code for various applications.