Java Language – 34 – Collections Framework

Collections and Data Structures – Collections Framework
Introduction

The Java Collections Framework is a fundamental part of the Java programming language, providing a set of interfaces, classes, and algorithms to manage and manipulate collections of objects. Collections are used to store, retrieve, and process data efficiently. In this guide, we’ll explore the key components of the Collections Framework and how they can simplify data handling in Java.

The Collection Interface

At the core of the Collections Framework is the Collection interface. It represents a group of objects and defines common methods for interacting with collections, such as adding, removing, and querying elements. The Collection interface is the foundation for various collection types in Java.

Example:

Let’s create a simple list using the Collection interface:


import java.util.ArrayList;
import java.util.Collection;

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

        // Print the names
        for (String name : names) {
            System.out.println(name);
        }
    }
}
List Interface

The List interface extends the Collection interface and represents an ordered collection that allows duplicate elements. Lists maintain the insertion order, allowing you to access elements by their index. Common implementations of the List interface include ArrayList and LinkedList.

Example:

Here’s an example of using the List interface to maintain a list of items:


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

public class ListExample {
    public static void main(String[] args) {
        List<String> items = new ArrayList<>();
        items.add("Item A");
        items.add("Item B");
        items.add("Item C");

        // Access elements by index
        String firstItem = items.get(0);
        System.out.println("First item: " + firstItem);
    }
}
Set Interface

The Set interface represents a collection that does not allow duplicate elements. Sets are useful when you need to ensure that each element is unique. Common implementations of the Set interface include HashSet and TreeSet.

Example:

Here’s an example using the Set interface to store a unique set of numbers:


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

public class SetExample {
    public static void main(String[] args) {
        Set<Integer> numbers = new HashSet<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(1); // Duplicate element (ignored)

        // Size of the set
        int size = numbers.size();
        System.out.println("Number of unique elements: " + size);
    }
}
Map Interface

The Map interface represents a collection of key-value pairs, where each key is associated with a value. Maps allow you to efficiently retrieve values based on their keys. Common implementations of the Map interface include HashMap and TreeMap.

Example:

Here’s an example using the Map interface to store and retrieve name-age pairs:


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

public class MapExample {
    public static void main(String[] args) {
        Map<String, Integer> nameAgeMap = new HashMap<>();
        nameAgeMap.put("Alice", 30);
        nameAgeMap.put("Bob", 25);

        // Retrieve age by name
        int aliceAge = nameAgeMap.get("Alice");
        System.out.println("Alice's age: " + aliceAge);
    }
}
Conclusion

The Java Collections Framework offers a versatile set of interfaces and classes for managing collections of data efficiently. By choosing the right collection type for your needs, you can simplify data handling and make your Java code more effective and readable.