Java 21 multi-key map collector
In my Java 21+ projects, I often have objects with multiple unique keys maps that I keep in multi-key maps like below. How can I build a multi-key map using the stream collector?
For example:
// id and symbol are guaranteed unique
record Foo(int id, String symbol) {};
then I create a multi-key map:
var fooA = new Foo(1, "A");
var fooB = new Foo(2, "B");
var fooList = List.of(fooA, fooB);
var fooMap = new HashMap
What is the streams collector equivalent? Perhaps something like:
var fooMap = fooList.stream().collect(
Collectors.flatMapping(???...)
);
or with a custom multiKeyMap varargs helper:
var fooMap = fooList.stream().collect(
multiKeyMap(Function.identity(), Foo::id, Foo::symbol)
);