JaJava · Lesson 5 of 9

Arrays & Collections

Java arrays are fixed-size. For anything dynamic, use the Collections framework — ArrayList, HashMap, HashSet. You'll use these constantly.

Java
import java.util.*;
import java.util.stream.*;

public class Collections {
    public static void main(String[] args) {
        // Array — fixed size
        int[] nums = {5, 2, 8, 1, 9, 3};
        Arrays.sort(nums);  // sorts in place
        System.out.println(Arrays.toString(nums));  // [1, 2, 3, 5, 8, 9]
        System.out.println(nums.length);            // 6

        // ArrayList — dynamic, resizable array
        List<String> fruits = new ArrayList<>();
        fruits.add("apple");
        fruits.add("banana");
        fruits.add("cherry");
        fruits.remove("banana");
        fruits.add(0, "avocado");  // insert at index 0

        System.out.println(fruits.size());          // 3
        System.out.println(fruits.get(0));          // avocado
        System.out.println(fruits.contains("apple")); // true

        Collections.sort(fruits);
        System.out.println(fruits);

        // HashMap
        Map<String, Integer> scores = new HashMap<>();
        scores.put("Alice", 95);
        scores.put("Bob", 87);
        scores.put("Carol", 92);

        System.out.println(scores.get("Alice"));        // 95
        System.out.println(scores.getOrDefault("Dave", 0)); // 0

        for (Map.Entry<String, Integer> entry : scores.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }

        // HashSet — unique values
        Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3, 2, 1));
        System.out.println(set.size());  // 3
    }
}
Java
import java.util.*;
import java.util.stream.*;

public class Streams {
    public static void main(String[] args) {
        // Java Streams — functional-style collection operations (Java 8+)
        List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        // filter, map, collect
        List<Integer> evenSquares = numbers.stream()
            .filter(n -> n % 2 == 0)
            .map(n -> n * n)
            .collect(Collectors.toList());

        System.out.println(evenSquares);  // [4, 16, 36, 64, 100]

        // reduce
        int sum = numbers.stream().reduce(0, Integer::sum);
        System.out.println("Sum: " + sum);  // 55

        // min, max, average
        OptionalInt max = numbers.stream().mapToInt(Integer::intValue).max();
        max.ifPresent(m -> System.out.println("Max: " + m));  // Max: 10

        // String joining
        List<String> words = List.of("hello", "world", "java");
        String joined = words.stream().collect(Collectors.joining(", "));
        System.out.println(joined);  // hello, world, java

        // Count
        long count = numbers.stream().filter(n -> n > 5).count();
        System.out.println("Count > 5: " + count);  // 5
    }
}
◆ Note
Use List.of(), Map.of(), Set.of() for immutable collections (Java 9+). For mutable collections, use new ArrayList<>(), new HashMap<>(), etc. Immutable collections are preferred when you don't need to modify them.