Mastering Java Collections: Lists, Maps, and Sets

Java Collections Framework (JCF) is one of the most powerful features of Java, providing data structures and algorithms that help developers manage and manipulate groups of objects efficiently. This guide dives into three key collection types: Lists, Maps, and Sets, with real-world examples to help you master their usage.

JAVA

1/3/20252 min read

1. Understanding Java Collections Framework

The Java Collections Framework provides a set of classes and interfaces to manage collections of objects. These include:

  • List: Ordered collections that allow duplicate elements.

  • Set: Unordered collections that do not allow duplicate elements.

  • Map: Key-value pairs, where each key is unique.

Each type has its own use cases and advantages, as we’ll explore below.

2. Lists in Java

A List is an ordered collection that can contain duplicate elements. The most commonly used implementations of List are:

  • ArrayList

  • LinkedList

Example: Managing a To-Do List

import java.util.ArrayList; public class ToDoList { public static void main(String[] args) { ArrayList<String> tasks = new ArrayList<>(); // Adding tasks tasks.add("Buy groceries"); tasks.add("Clean the house"); tasks.add("Pay bills"); // Iterating through tasks for (String task : tasks) { System.out.println(task); } // Removing a task tasks.remove("Clean the house"); System.out.println("Updated List: " + tasks); } }

Key Features of Lists:

  • Maintains the insertion order.

  • Allows access by index.

  • Permits duplicate elements.

3. Sets in Java

A Set is a collection that does not allow duplicate elements. Common implementations include:

  • HashSet

  • LinkedHashSet

  • TreeSet

Example: Storing Unique Email Addresses

import java.util.HashSet; public class EmailSet {

public static void main(String[] args) {

HashSet<String> emails = new HashSet<>(); // Adding email addresses emails.add("john@example.com"); emails.add("jane@example.com"); emails.add("john@example.com"); // Duplicate, will not be added // Displaying unique emails for (String email : emails) { System.out.println(email); } } }

Key Features of Sets:

  • Does not maintain insertion order (except LinkedHashSet).

  • Ensures no duplicates.

  • TreeSet maintains a sorted order.

4. Maps in Java

A Map is a collection of key-value pairs, where keys are unique and each key maps to a value. Common implementations include:

  • HashMap

  • LinkedHashMap

  • TreeMap

Example: Storing Product Prices

import java.util.HashMap;

public class ProductPrices {

public static void main(String[] args) {

HashMap<String, Double> prices = new HashMap<>();

// Adding products and their prices

prices.put("Apple", 1.99);

prices.put("Banana", 0.99);

prices.put("Orange", 1.49); // Accessing price by product name

System.out.println("Price of Apple: " + prices.get("Apple"));

// Iterating through the map

for (String product : prices.keySet()) {

System.out.println(product + ": $" + prices.get(product)); }

} }

Key Features of Maps:

  • Stores data in key-value pairs.

  • Keys must be unique.

  • HashMap is unsorted, while TreeMap maintains a natural order.

5. Comparing Lists, Sets, and Maps

6. Choosing the Right Collection

Conclusion

Mastering Java collections is essential for efficient programming. Lists, Sets, and Maps each serve distinct purposes and are indispensable in handling data. Practice using these collections in real-world scenarios to enhance your understanding and programming skills.

For more Java tutorials and insights, visit Jogindra Kumar’s Blog.