Arrays, lists and maps

Arrays, ArrayList, HashMap, Set, sorting with Arrays.sort and Comparator.

Runs in Zerethon IDE · Java 25 Official Java documentation ↗
Main.java
import java.util.*;

public class Main {
    public static void main(String[] args) {
        int[] scores = {70, 95, 8, 82};
        Arrays.sort(scores);
        System.out.println(Arrays.toString(scores) + " " + scores.length);

        List<String> names = new ArrayList<>(List.of("linus", "ada", "grace"));
        names.add("alan");
        Collections.sort(names);
        System.out.println(names + " " + names.get(0) + " " + names.size());
        names.sort(Comparator.comparing(String::length).reversed());
        System.out.println(names);

        Map<String, Integer> ages = new HashMap<>();
        ages.put("ada", 36);
        ages.put("alan", 41);
        System.out.println(ages.get("alan") + " " + ages.getOrDefault("linus", -1) + " " + ages.containsKey("ada"));
        for (var e : new TreeMap<>(ages).entrySet()) {
            System.out.println(e.getKey() + "=" + e.getValue());
        }

        Set<Integer> unique = new TreeSet<>(List.of(3, 1, 3, 2));
        System.out.println(unique);
        System.out.println(names.stream().filter(n -> n.length() > 3).map(String::toUpperCase).toList());
    }
}

Output

[8, 70, 82, 95] 4
[ada, alan, grace, linus] ada 4
[grace, linus, alan, ada]
41 -1 true
ada=36
alan=41
[1, 2, 3]
[GRACE, LINUS, ALAN]
  • Arrays have a fixed size (length, no brackets); ArrayList grows (size()).
  • Collections hold objects: List<Integer>, not List<int>.
  • HashMap has no order; TreeMap / TreeSet keep keys sorted, LinkedHashMap keeps insertion order.
  • List.of(...) is unchangeable — wrap it in new ArrayList<>(...) to add or sort.