What is the EK9 equivalent of the Java Collections Framework?
← Collections and Data Structures · Ref: Q132
EK9 provides equivalent collection types to Java's Collections Framework, but with simpler syntax and unified operator patterns.
ARRAYLIST TO LIST
Java: List<String> names = new ArrayList<>(List.of("Alice", "Bob"));
EK9: names <- ["Alice", "Bob"]
No angle brackets, no ArrayList/List distinction, literal syntax.
HASHMAP TO DICT
Java: Map<String, Integer> ages = new HashMap<>(Map.of("Alice", 30));
EK9: ages <- {"Alice": 30}
Literal syntax, no Map.of() or put() calls needed.
PRIORITYQUEUE TO PRIORITYQUEUE
Java: PriorityQueue<String> pq = new PriorityQueue<>(comparator);
EK9: pq <- PriorityQueue("first").withComparator(comparator)
Fluent API with bounded size support via withSize(n).
OPTIONAL TO OPTIONAL
Java: Optional<String> name = Optional.of("Alice");
EK9: name <- Optional("Alice")
Same concept, simpler syntax, integrated with guard variables.
STREAM API TO EK9 STREAMS
Java: list.stream().filter(x -> x > 0).map(x -> x * 2).collect(Collectors.toList());
EK9: cat list | filter by isPositive | map with doubleIt | collect as List of Integer
Named functions instead of lambdas, pipe syntax instead of method chaining.
COLLECTORS TO COLLECT AS
Java: Collectors.groupingBy(), Collectors.summarizingInt(), Collectors.joining()
EK9: | group by func, | collect as Stats (custom), | collect as String
Custom collectors via operator | on records.
KEY DIFFERENCES
EK9 uses 'of' instead of angle brackets for generics.
No null in EK9, tri-state semantics instead.
Operators (+=, -=, +, -) instead of method calls (add, remove, put).
All types closed (no extending List or Dict).
See Q45 for List. See Q46 for Dict. See Q121 for PriorityQueue. See Q122 for collect as custom aggregation.
Example
defines module qa.collections.javacollections defines function isEven() as pure -> num as Integer <- rtn as Boolean: num mod 2 == 0 doubleIt() as pure -> num as Integer <- rtn as Integer: num * 2 defines program JavaCollectionsDemo() stdout <- Stdout() // === ArrayList -> List === // Java: List<String> names = new ArrayList<>(List.of("Alice", "Bob", "Charlie")); names <- ["Alice", "Bob", "Charlie"] names += "Dave" stdout.println(`Names: ${names}`) // === HashMap -> Dict === // Java: Map<String, Integer> ages = new HashMap<>(Map.of("Alice", 30)); ages <- {"Alice": 30, "Bob": 25} age <- ages.getOrDefault("Alice", 0) stdout.println(`Alice age: ${age}`) // === Stream API -> EK9 Streams === // Java: numbers.stream().filter(x -> x % 2 == 0).map(x -> x * 2).collect(Collectors.toList()); numbers <- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] result <- cat numbers | filter by isEven | map with doubleIt | collect as List of Integer stdout.println(`Doubled evens: ${result}`) // === Collectors.summarizingInt -> collect as Integer === // Java: numbers.stream().mapToInt(x -> x).sum(); total <- cat numbers | collect as Integer stdout.println(`Sum: ${total}`) // === Optional === // Java: Optional<String> name = Optional.of("Alice"); optName <- Optional("Alice") if optName? stdout.println(`Got: ${optName}`) // === PriorityQueue === // Java: PriorityQueue<Integer> pq = new PriorityQueue<>(comparator); comparator <- () extends Comparator of Integer as pure function (r:=? t1 <=> t2) pq <- PriorityQueue(5).withComparator(comparator) pq += 3 pq += 8 pq += 1 stdout.println(`PQ: ${pq.list()}`)
Common mistakes
E50060 — EK9 uses the += operator to add items to a List, not the Java-style add() method. See ek9 -h E50060 for details.
Incorrect:
names.add("Dave")
Correct:
names += "Dave"
E50060 — EK9 Dict has no get() method. Use getOrDefault(key, default) for safe access. See ek9 -h E50060 for details.
Incorrect:
age <- ages.get("Alice")
Correct:
age <- ages.getOrDefault("Alice", 0)
E50060 — EK9 does not have Java's Stream API. No .stream(), .filter(), .map(), or .toList() methods. Use pipe syntax: cat source | filter by fn | map with fn | collect as Type. See ek9 -h E50060 for details.
Incorrect:
numbers.stream().filter(isEven).map(doubleIt).toList()
Correct:
cat numbers | filter by isEven | map with doubleIt | collect as List of Integer
E50060 — EK9 does not have toString(). Use string interpolation or the $ operator. See ek9 -h E50060 for details.
Incorrect:
stdout.println(names.toString())
Correct:
stdout.println(`Names: ${names}`)
E50060 — EK9 does not have toString(). Use string interpolation or the $ operator. See ek9 -h E50060 for details.
Incorrect:
stdout.println(result.toString())
Correct:
stdout.println(`Doubled evens: ${result}`)
Other ways to ask this
- How do I translate Java ArrayList and HashMap code to EK9?
- What replaces Java Stream API in EK9?
- How do Java collection patterns map to EK9?
- What replaces Java ArrayList in EK9?
Coming from another language?
Java: ArrayList<T>, HashMap<K,V>, PriorityQueue<T>, Optional<T>, Stream API with Collectors. EK9: List of T, Dict of (K, V), PriorityQueue of T, Optional of T, cat | pipe | collect. Key differences: EK9 literal syntax ([1,2,3] and {k:v}), 'of' instead of <>, operators instead of methods, named functions instead of lambdas, no null.
Keywords: equivalent, translate, Kotlin, collectors, Rust, hashmap, java, data-structure, migration, stream, collection, framework, replace, convert, generic, arraylist, LINQ