Practice Collections in Java

Work with Java Collections Framework including List, Set, Map, and Queue.

Work through 22 exercises with starter code, expected output, test cases, and hints.

How to learn Collections

The Java Collections Framework provides interfaces such as List, Set, Queue, and Map plus implementations with different ordering and performance behavior. Choose the interface from the operation you need, then choose an implementation from ordering, duplication, lookup, and concurrency requirements.

Big-O labels are a useful starting point, not the whole choice. ArrayList is often preferable for iteration and indexed access; HashSet answers membership questions; HashMap associates keys with values; ArrayDeque handles stack and queue operations without the legacy Stack class.

Recommended learning order

  1. List and iteration: Begin with ArrayList, generics, indexed access, enhanced loops, iterators, and the cost of inserting away from the end.
  2. Set membership: Use HashSet to enforce uniqueness and learn how equals and hashCode define whether two elements are considered the same.
  3. Map association: Store key-value pairs, update counts with merge, and iterate through entrySet when both key and value are needed.
  4. Queue and ordering: Use ArrayDeque for FIFO and LIFO behavior, PriorityQueue for priority order, and sorted collections only when their ordering benefit is required.

Tested example: Count words with Map.merge

merge inserts 1 for a new word and adds 1 to the existing count otherwise. LinkedHashMap is chosen so the demonstration prints keys in first-seen order.

import java.util.LinkedHashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        String[] words = {"java", "map", "java", "list", "map", "java"};
        Map<String, Integer> counts = new LinkedHashMap<>();
        for (String word : words) {
            counts.merge(word, 1, Integer::sum);
        }
        System.out.println(counts);
    }
}

Expected output

{java=3, map=2, list=1}

Change and run this example in the Java compiler

Common mistakes

  • Depending on HashMap or HashSet iteration order even though those implementations do not promise one.
  • Mutating a key in a way that changes equals or hashCode after placing it in a hash-based collection.
  • Removing directly from a collection during an enhanced for loop instead of using the iterator or removeIf.
  • Using raw types such as List, which discards compile-time element checks and pushes errors to runtime.

How to practice

Learn one implementation for each core interface, then solve the same counting or deduplication problem with different choices and explain the trade-off. The DSA hub is the next step when collection operations are fluent.

Exercises to do first

Collections exercises

  1. ArrayList Basics Java exercise — Create and use ArrayList
  2. HashSet Basics Java exercise — Use HashSet for unique elements
  3. HashMap Basics Java exercise — Store key-value pairs
  4. List Iteration Methods Java exercise — Different ways to iterate a list
  5. Queue Operations Java exercise — Use Queue for FIFO operations
  6. Stack Operations Java exercise — Use Stack for LIFO operations
  7. Word Frequency Counter Java exercise — Count word frequencies in a sentence
  8. Remove Duplicates from List Java exercise — Remove duplicates while preserving order
  9. List Intersection Java exercise — Find common elements between two lists
  10. Sort Map by Value Java exercise — Sort a map by its values
  11. Group By Property Java exercise — Group elements by a property
  12. Simple LRU Cache Java exercise — Implement a simple LRU cache
  13. Priority Queue with Custom Order Java exercise — Use PriorityQueue with custom comparator
  14. Two Sum with HashMap Java exercise — Find pair with target sum using HashMap
  15. First Unique Element Java exercise — Find first element that appears once
  16. Merge K Sorted Lists Java exercise — Merge multiple sorted lists into one
  17. Stack Using Queues Java exercise — Implement a stack using only queues
  18. Group Anagrams Java exercise — Group words that are anagrams of each other
  19. Sliding Window Maximum Java exercise — Find maximum in each sliding window
  20. Top K Frequent Elements Java exercise — Find the k most frequent elements
  21. Merge Intervals Java exercise — Merge overlapping intervals
  22. Task Scheduler Java exercise — Find minimum time to complete all tasks with cooldown

Browse the complete Java practice path · Open the online Java compiler