Module 5: Handling Collections of Data
Using Arrays and the Java Collections Framework.
Beyond Single Variables
Welcome to Module 5! So far, we've worked with variables that hold a single value or object. But what if you need to store a list of 100 users, or a directory of phone numbers? Real-world applications require us to work with groups of data. This module introduces you to Java's powerful tools for managing data collections, starting with the basic **Array** and moving on to the more flexible and powerful classes within the **Java Collections Framework**.
1Arrays: Fixed-Size Lists
An **Array** is the most fundamental data structure for storing a collection of elements of the same type. Think of it as a row of numbered boxes, where each box holds a value. The key characteristic of an array is its **fixed size**; once you create an array to hold 5 items, you cannot change it to hold 6.
Declaring and Initializing an Array
You declare an array by specifying the data type followed by square brackets `[]`.
// Declare an array of integers that can hold 5 elements
int[] numbers = new int[5];
// Assign values to elements using their index (starts at 0)
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
// Access and print an element
System.out.println("The first number is: " + numbers[0]); // Outputs 10
// A shorter way to initialize an array
String[] fruits = {"Apple", "Banana", "Orange"};
Looping Through an Array
The `for` loop is perfect for iterating through the elements of an array.
for (int i = 0; i < fruits.length; i++) {
System.out.println(fruits[i]);
}
2The `ArrayList`: A Dynamic Array
Arrays are great, but their fixed size can be a limitation. What if you don't know how many items you'll need to store? The `ArrayList` from the Java Collections Framework solves this problem. It's a resizable array that can grow and shrink as you add or remove elements.
import java.util.ArrayList; // Must be imported
public class Main {
public static void main(String[] args) {
// Create an ArrayList of Strings
ArrayList<String> tasks = new ArrayList<>();
// Add elements to the list
tasks.add("Buy groceries");
tasks.add("Walk the dog");
tasks.add("Learn Java");
// Get an element by index
System.out.println("Second task: " + tasks.get(1));
// Remove an element
tasks.remove("Walk the dog");
// Loop through the list with an enhanced for-loop
for (String task : tasks) {
System.out.println(task);
}
}
}
3The `HashMap`: Key-Value Pairs
What if you want to store data not as a simple list, but as a set of key-value pairs? A `HashMap` is perfect for this. Think of it like a dictionary, where you look up a word (the **key**) to find its definition (the **value**). Keys in a `HashMap` must be unique.
import java.util.HashMap; // Must be imported
public class Main {
public static void main(String[] args) {
// Create a HashMap to store user scores (String key, Integer value)
HashMap<String, Integer> userScores = new HashMap<>();
// Add key-value pairs
userScores.put("Alice", 95);
userScores.put("Bob", 80);
userScores.put("Charlie", 100);
// Get a value by its key
System.out.println("Bob's score is: " + userScores.get("Bob"));
// Loop through the keys
for (String userName : userScores.keySet()) {
System.out.println(userName + " scored " + userScores.get(userName));
}
}
}