#collections Clear
Tags #php #kotlin #bash #go #sql #rust #typescript #html #java #python #files #utils #strings #http #concurrency #async #json #arrays #security #types #crypto #database #dates #format
Kotlin map / filter / reduce
The three classic functional combinators. Each takes a lambda and returns a new collection (or a single value for reduce). Chain freely — intermediate allocations only matter at very large scale (use `Sequence` then).
Kotlin fold and scan
`fold` accumulates from a seed across all items. `runningFold` / `scan` yields every intermediate accumulator value — running totals, cumulative metrics.
Kotlin groupBy and partition
`groupBy` returns a `Map<Key, List<T>>` keyed by what the lambda returns. `partition` is a special-case `groupBy` for booleans — returns `(matching, nonMatching)` Pair.
Python group_by — Group Items by a Key
A dict-returning groupby that doesn't require sorted input. Like itertools.groupby but materializes the groups so consumers can iterate them multiple times.
Java Sorting with Comparator
`Comparator.comparing`, `thenComparing`, and `reversed()` chain into expressive multi-field sorts. Way cleaner than the old Comparable spaghetti.
Kotlin flatMap and Flattening
`flatMap` maps each item to a collection, then concatenates. Use it when "for each X, produce N Y's, collect all Y's". `flatten()` is the no-mapping version.
Rust HashMap with the entry() API
The `entry()` API is the idiomatic way to "insert if absent" or "update if present" in a single lookup. Avoids double-lookups that the explicit `contains_key` + `insert` dance would need.
Kotlin Immutable vs Mutable Collections
`listOf` / `setOf` / `mapOf` build READ-ONLY views; `mutableListOf` / `mutableSetOf` / `mutableMapOf` are mutable. Default to immutable — the call site is a contract about intent.
Java Immutable Collections — List.of / Map.of
Java 9+ static factory methods return immutable collections with no nulls allowed. Concise, fast, and safer to share across threads or APIs than mutable equivalents.
Kotlin zip and zipWithIndex
`zip` pairs elements positionally; the result is the length of the shorter list. `withIndex()` gives you `(index, value)` pairs without the manual `forEachIndexed`.
Kotlin distinct, distinctBy, intersect, union
Set-style ops on collections. `distinct` removes duplicates; `distinctBy` lets you key the dedupe by a derived value; `union/intersect/subtract` give you set operations.
Kotlin windowed and chunked
`chunked(n)` splits a list into NON-overlapping pieces of size n. `windowed(n)` slides a fixed-size window across — overlapping. Useful for moving averages, n-grams, batch APIs.
Kotlin Sorting Collections
`sortedBy { ... }` returns a sorted copy. `sortedWith(compareBy { ... })` chains tie-breakers cleanly. Avoid `sortBy` (in-place) on read-only lists.
Kotlin associate and associateBy
`associateBy` builds a `Map<K, T>` keyed by a derived value. `associateWith` keys by the items themselves with derived values. `associate` lets you build both key and value from each item.
Kotlin minOf / maxOf / sumOf / averageOf
Numerical reducers with a selector lambda — much cleaner than `.map { ... }.max()` chains. `sumOf` is type-aware (returns Int, Long, Double, BigDecimal as appropriate).