Warm‑up: Pick the right Collection (with answer)
Warm‑up (with answer): You receive a list of "orders" as strings. You must choose the BEST Java Collection for each need: 1) Preserve insertion order and allow duplicates 2) Fast membership check (unique) 3) Sorted unique view (ascending) 4) Key->Value mapping (look up by id) 5) FIFO processing Print 5 lines: requirement -> collection + reason (1 short line each).
import java.util.*; // Collections
public class Main { // entry class
// ✅ TODO: Student must implement only this method
static void mapNeedsToCollections() {
// TODO:
// - Print 5 lines mapping requirement -> best collection + reason
// - Examples: ArrayList, HashSet, TreeSet, HashMap, ArrayDeque
throw new UnsupportedOperationException("TODO");
}
public static void main(String[] args) {
mapNeedsToCollections(); // run warm-up
}
}
Warm‑up Answer (Mappings)
Typical mapping: 1) insertion order + duplicates => ArrayList (keeps order, allows duplicates) 2) fast membership + unique => HashSet (O(1) average contains) 3) sorted unique => TreeSet (sorted + unique) 4) key->value => HashMap (fast lookup by key) 5) FIFO => ArrayDeque (queue operations add/remove)
System.out.println("1) Order+duplicates => ArrayList (keeps insertion order, duplicates allowed).");
System.out.println("2) Fast unique contains => HashSet (avg O(1) membership).");
System.out.println("3) Sorted unique => TreeSet (sorted + unique elements).");
System.out.println("4) Key->Value lookup => HashMap (fast key lookup).");
System.out.println("5) FIFO processing => ArrayDeque (queue operations).");