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:
- ArrayList (Dynamic Array): Automatically resizes itself as you add items. Extremely fast for reading items by index position.
- LinkedList (Connected Chain): Stores items as nodes linked together by pointers. Extremely fast for inserting or deleting items at the ends.
• 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:
- HashSet: Unordered collection of unique items. Fast $O(1)$ lookups using hash codes.
- TreeSet: Automatically keeps all unique items sorted in ascending order (e.g. A to Z or 1 to 100).
| 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:
- Key-Value Storage: Like a dictionary or phonebook (Key = Name, Value = Phone Number).
- Unique Keys: Every key in a HashMap must be unique.
- Instant $O(1)$ Lookup: Searching by key takes 1 microsecond regardless of list size!
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
- 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)!
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'!