I am writing a function to convert array to Map using Java 8 Stream.
Here is what I wanted
public static <K, V> Map<K, V> toMap(Object... entries) {
// Requirements:
// entries must be K1, V1, K2, V2, .... ( even length )
if (entries.length % 2 == 1) {
throw new IllegalArgumentException("Invalid entries");
}
// TODO
Arrays.stream(entries).????
}
Valid usages
Map<String, Integer> map1 = toMap("k1", 1, "k2", 2);
Map<String, String> map2 = toMap("k1", "v1", "k2", "v2", "k3", "v3");
Invalid usages
Map<String, Integer> map1 = toMap("k1", 1, "k2", 2, "k3");
Any helps?
Thanks!
Getting exactly what you want will probably not work for maps whose key type differs from their value type. This is because Java's variable arity declaration (the
Object... entries
part) supports only one type.Some options come to mind:
You could do the checks dynamically and throw an illegal argument exception if the values don't match. But you'll lose the compiler type-checking.
You could define a
Pair
class, and play a bit with static import to get almost what you want:e.g.:
Here is my idea by JDK 8 stream:
If you don't mind use third-party library AbacusUtil, the code could be simplified to:
And I think the most efficient way to do it is by for loop, if you not particularly pursue using Stream API
You can use something like map literals.
For achieving this you can use a factory method:
Finally, you can do something like following:
Output:
I hope that this gives to you the right way.
You may use
but it will give you a (founded) unchecked warning. Your method can’t hold the promise to return a correctly typed
Map<K, V>
for an array of arbitrary objects and, even worse, it will not fail with an exception, but silently return an inconsistent map if you pass in objects of the wrong type.A cleaner, commonly used, solution is
This can be compiled without a warning, as the correctness will be checked at runtime. The calling code has to be adapted:
Besides the need to specify the actual types as class literals, it has the disadvantage of not supporting generic key or value types (as they can’t be expressed as
Class
) and still having no compile-time safety, only a runtime check.It’s worth looking at Java 9. There, you will be able to do:
This will create an immutable map of an unspecified type, rather than a
HashMap
, but the interesting point is the API.There is a method
<K,V> Map.Entry<K,V> entry(K k, V v)
which can be combined with<K,V> Map<K,V> ofEntries(Map.Entry<? extends K,? extends V>... entries)
to create a map of a variable length (varargs are still limited to 255 parameters, though).You can implement a similar thing:
The convenience method(s)
of
are implemented the only way, this can be done with type safety: as overloaded methods with different numbers of arguments, like(Java 9 makes the cut at ten mappings, if you have more, you have to use the
ofEntries(entry(k1, v1), …)
variant).If you follow this pattern, you should keep your
toMap
name or use justmap
, rather than calling at “of
”, as you are not writing theMap
interface.These overloads might not look very elegant, but they solve all problems. You can write the code just as in your question, without specifying
Class
objects, but gain compile-time type safety and even rejection of attempts to call it with an odd number of arguments.You have to make a cut at a certain number of parameters, but, as already noted, even varargs do not support unlimited parameters. And the
ofEntries(entry(…), …)
form isn’t so bad for larger maps.The collector
Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)
returns an unspecified map type, which might even be immutable (though it’s aHashMap
in the current version). If you want to have a guaranty that aHashMap
instance is returned, you have to useCollectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1,v2)->{throw new IllegalArgumentException("duplicate key");}, HashMap::new)
instead.