Closed
Description
Java 9 introduced static factory methods on the collection interaces, to create immutable collections. We should consider doing the same for MultiValueMap
.
There are several open questions to answer:
- Should multi-value maps returned by these factory methods be immutable, just like the java collections?
- It seems difficult to create a convenient, unambiguous API that takes keys and values. Consider
MultiValueMap.of("foo", "bar", "baz", "qux")
. Does the resulting map have two entries (foo->bar and baz->qux)? Or does it have one entry, with three values (foo->[bar, baz, qux])? Without reading the javadoc, you would not know, which makes the method problematic. - We could introduce
MultiValueMap<K, V> MultiValueMap::of(Map<K, V>)
, so that you could doMultiValueMap.of(Map.of("foo", "bar")
. - If we also want to support multiple values, we could have
MultiValueMap<K, V> MultiValueMap::of(Map<K, List<V>>)
, which allowsMultiValueMap.of(Map.of("foo", List.of("bar", "baz"))
. - If we want to support both
of
variants (single and multi value), they would have to be named differently because of type erasure. For instance,MultiValueMap<K, V> MultiValueMap::ofSingle(Map<K, V>)
andMultiValueMap<K, V> MultiValueMap::ofMulti(Map<K, List<V>>)