Beginner Reference Manual • Chapter 7

Storing Lists & Grouping Data (Java Collections Framework)

Master dynamic data storage structures in java.util: Lists (ArrayList, LinkedList), Sets (HashSet, TreeSet), and Maps (HashMap) with Generics.

Plain arrays are fixed in size—if you create an array of 5 items, you can't easily add a 6th item! The Java Collections Framework (`java.util`) gives us smart, flexible data containers that grow and shrink automatically.

7.1. 1. Resizing Lists: ArrayList vs LinkedList

Definition: Lists store ordered collections of items where duplicates are allowed and items can be accessed by index.

Core Working Principles:

🚃 Layman Analogy: Expanding Organizer Tray vs Train Cars

• ArrayList: An expanding plastic tray. Great for grabbing item #3 instantly, but expanding requires getting a larger tray.
• LinkedList: Connected train cars. Easy to attach a new train car to the front or back in 1 second!

// Creating a dynamic ArrayList of Strings
ArrayList<String> shoppingList = new ArrayList<>();
shoppingList.add("Apples");
shoppingList.add("Milk");
shoppingList.add("Bread");

System.out.println("First item: " + shoppingList.get(0)); // Prints Apples
5-Marks Exam Scoring Points
  • ArrayList grows automatically and provides fast O(1) index reads.
  • LinkedList provides fast insertions and deletions at ends.
  • Generics () guarantee type safety at compile time.

7.2. 2. No Duplicates Allowed: HashSet vs TreeSet

Definition: Sets store unique elements only—if you try to add a duplicate item, it is automatically rejected.

Core Working Principles:

Collection Type Ordering Allows Duplicates? Best Feature
ArrayList Insertion Order Yes Fast Random Read Access
HashSet Unordered NO Instant Uniqueness Lookup
TreeSet Sorted Order NO Auto-Sorted Elements
5-Marks Exam Scoring Points
  • Sets enforce element uniqueness (no duplicates).
  • HashSet provides fast unordered uniqueness checks.
  • TreeSet keeps unique elements automatically sorted.

7.3. 3. Instant Lookups: HashMap Key-Value Pairs

Definition: A HashMap stores pairs of data (Key -> Value), allowing you to look up any value instantly using its key.

Core Working Principles:

HashMap<String, String> phonebook = new HashMap<>();
phonebook.put("Alex", "555-0199");
phonebook.put("Jordan", "555-0244");

// Instant lookup by Key
String alexNumber = phonebook.get("Alex");
System.out.println("Alex's Phone: " + alexNumber); // Prints 555-0199
5-Marks Exam Scoring Points
  • HashMap stores data in Key -> Value pairs.
  • Keys must be unique; values can be duplicated.
  • Provides instant O(1) lookups by key.

✍️ Chapter 7 Reference Exercises & Review Problems

Exercise 7.1: When should you choose an `ArrayList` over a regular fixed array?
Step-by-Step Explanation for Beginners:
- Choose a regular array only when you know the fixed size in advance and it will never change (e.g. days of the week = 7).
- Choose an ArrayList whenever the number of items can grow or shrink dynamically (e.g. shopping cart items, user list)!
Exercise 7.2: What happens if you try to add the word 'Apple' twice to a `HashSet`?
Step-by-Step Explanation for Beginners:
The `HashSet` will accept the first 'Apple'. When you try to add the second 'Apple', the `HashSet` checks its hash code, detects that 'Apple' is already present, and simply **ignores** the second add command. The set remains with only 1 unique copy of 'Apple'!