If your ending map has a chance of "duplicate keys", there is a better solution using
toMap(Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper,
BinaryOperator<U> mergeFunction,
Supplier<M> mapSupplier)
You could use it like this:
HashMap<Set<Integer>, Double> map = container.entrySet()
.stream()
.filter(k -> k.getKey().size() == size)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (prev, next) -> next, HashMap::new));
Then as duplicate keys are added, it will use the latest instead of throwing an exception. The last parameter is optional.
If you want to keep duplicate keys into a list, then use Collectors.groupingBy instead.
If your ending map has a chance of "duplicate keys", there is a better solution using
toMap(Function<? super T, ? extends K> keyMapper,
Function<? super T, ? extends U> valueMapper,
BinaryOperator<U> mergeFunction,
Supplier<M> mapSupplier)
You could use it like this:
HashMap<Set<Integer>, Double> map = container.entrySet()
.stream()
.filter(k -> k.getKey().size() == size)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (prev, next) -> next, HashMap::new));
Then as duplicate keys are added, it will use the latest instead of throwing an exception. The last parameter is optional.
If you want to keep duplicate keys into a list, then use Collectors.groupingBy instead.
Seems that Collectors.toMap does not pick up the type arguments of stream.collect in your example and only returns a Map<Object,Object>.
As a workaround you can create the result map yourself and in the last stream step add the filtered entries to the result map:
Map<Set<Integer>, Double> result = new HashMap<>();
container.entrySet()
.stream()
.filter(entry -> entry.getKey().size() == size)
.forEach(entry -> result.put(entry.getKey(), entry.getValue()));
Hashmap with Streams in Java 8 Streams to collect value of Map - Stack Overflow
java - Java8: HashMap to HashMap using Stream / Map-Reduce / Collector - Stack Overflow
How to convert Array to HashMap using Java 8 Stream - Stack Overflow
Java 8 streams - String to HashMap counter of characters
Videos
If you are sure you are going to get at most a single element that passed the filter (which is guaranteed by your filter), you can use findFirst :
CopyOptional<List> o = id1.entrySet()
.stream()
.filter( e -> e.getKey() == 1)
.map(Map.Entry::getValue)
.findFirst();
In the general case, if the filter may match multiple Lists, you can collect them to a List of Lists :
CopyList<List> list = id1.entrySet()
.stream()
.filter(.. some predicate...)
.map(Map.Entry::getValue)
.collect(Collectors.toList());
What you need to do is create a Stream out of the Map's .entrySet():
Copy// Map<K, V> --> Set<Map.Entry<K, V>> --> Stream<Map.Entry<K, V>>
map.entrySet().stream()
From the on, you can .filter() over these entries. For instance:
Copy// Stream<Map.Entry<K, V>> --> Stream<Map.Entry<K, V>>
.filter(entry -> entry.getKey() == 1)
And to obtain the values from it you .map():
Copy// Stream<Map.Entry<K, V>> --> Stream<V>
.map(Map.Entry::getValue)
Finally, you need to collect into a List:
Copy// Stream<V> --> List<V>
.collect(Collectors.toList())
If you have only one entry, use this instead (NOTE: this code assumes that there is a value; otherwise, use .orElse(); see the javadoc of Optional for more details):
Copy// Stream<V> --> Optional<V> --> V
.findFirst().get()
Map<String, String> x;
Map<String, Integer> y =
x.entrySet().stream()
.collect(Collectors.toMap(
e -> e.getKey(),
e -> Integer.parseInt(e.getValue())
));
It's not quite as nice as the list code. You can't construct new Map.Entrys in a map() call so the work is mixed into the collect() call.
Here are some variations on Sotirios Delimanolis' answer, which was pretty good to begin with (+1). Consider the following:
static <X, Y, Z> Map<X, Z> transform(Map<? extends X, ? extends Y> input,
Function<Y, Z> function) {
return input.keySet().stream()
.collect(Collectors.toMap(Function.identity(),
key -> function.apply(input.get(key))));
}
A couple points here. First is the use of wildcards in the generics; this makes the function somewhat more flexible. A wildcard would be necessary if, for example, you wanted the output map to have a key that's a superclass of the input map's key:
Map<String, String> input = new HashMap<String, String>();
input.put("string1", "42");
input.put("string2", "41");
Map<CharSequence, Integer> output = transform(input, Integer::parseInt);
(There is also an example for the map's values, but it's really contrived, and I admit that having the bounded wildcard for Y only helps in edge cases.)
A second point is that instead of running the stream over the input map's entrySet, I ran it over the keySet. This makes the code a little cleaner, I think, at the cost of having to fetch values out of the map instead of from the map entry. Incidentally, I initially had key -> key as the first argument to toMap() and this failed with a type inference error for some reason. Changing it to (X key) -> key worked, as did Function.identity().
Still another variation is as follows:
static <X, Y, Z> Map<X, Z> transform1(Map<? extends X, ? extends Y> input,
Function<Y, Z> function) {
Map<X, Z> result = new HashMap<>();
input.forEach((k, v) -> result.put(k, function.apply(v)));
return result;
}
This uses Map.forEach() instead of streams. This is even simpler, I think, because it dispenses with the collectors, which are somewhat clumsy to use with maps. The reason is that Map.forEach() gives the key and value as separate parameters, whereas the stream has only one value -- and you have to choose whether to use the key or the map entry as that value. On the minus side, this lacks the rich, streamy goodness of the other approaches. :-)
You may use
public static <K, V> Map<K, V> toMap(Object... entries) {
if(entries.length % 2 == 1)
throw new IllegalArgumentException("Invalid entries");
return (Map<K, V>)IntStream.range(0, entries.length/2).map(i -> i*2)
.collect(HashMap::new, (m,i)->m.put(entries[i], entries[i+1]), Map::putAll);
}
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
public static <K, V> Map<K, V> toMap(
Class<K> keyType, Class<V> valueType, Object... entries) {
if(entries.length % 2 == 1)
throw new IllegalArgumentException("Invalid entries");
return IntStream.range(0, entries.length/2).map(i -> i*2)
.collect(HashMap::new,
(m,i)->m.put(keyType.cast(entries[i]), valueType.cast(entries[i+1])),
Map::putAll);
}
This can be compiled without a warning, as the correctness will be checked at runtime. The calling code has to be adapted:
Map<String, Integer> map1 = toMap(String.class, Integer.class, "k1", 1, "k2", 2);
Map<String, String> map2 = toMap(
String.class, String.class, "k1", "v1", "k2", "v2", "k3", "v3");
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:
Map<String, Integer> map1 = Map.of("k1", 1, "k2", 2);
Map<String, String> map2 = Map.of("k1", "v1", "k2", "v2", "k3", "v3");
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:
public static <K,V> Map.Entry<K,V> entry(K k, V v) {
return new AbstractMap.SimpleImmutableEntry<>(k, v);
}
public static <K,V> Map<K,V> ofEntries(Map.Entry<? extends K,? extends V>... entries) {
return Arrays.stream(entries)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
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
public static <K,V> Map<K,V> of() {
return new HashMap<>();// or Collections.emptyMap() to create immutable maps
}
static <K,V> Map<K,V> of(K k1, V v1) {
return ofEntries(entry(k1, v1));
}
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2) {
return ofEntries(entry(k1, v1), entry(k2, v2));
}
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3) {
return ofEntries(entry(k1, v1), entry(k2, v2), entry(k3, v3));
}
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
return ofEntries(entry(k1, v1), entry(k2, v2), entry(k3, v3), entry(k4, v4));
}
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
return ofEntries(entry(k1, v1), entry(k2, v2), entry(k3, v3), entry(k4, v4));
}
(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 just map, rather than calling at “of”, as you are not writing the Map 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 a HashMapin the current version). If you want to have a guaranty that a HashMap instance is returned, you have to use Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1,v2)->{throw new IllegalArgumentException("duplicate key");}, HashMap::new) instead.
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
Pairclass, and play a bit with static import to get almost what you want:
e.g.:
class Pair<K,V> {
final K k;
final V v;
Pair( K ak, V av) {
k=ak;
v=av;
}
static <A,B> Pair<A,B> p(A a, B b) {
return new Pair(a,b);
}
}
public class JavaTest8 {
<K,V> Map<K,V> toMap( Pair<K,V>... pairs ) {
return Arrays.stream(pairs).collect(Collectors.toMap(p->p.k, p->p.v));
}
public static void main(String[] args) {
// Usage
Map<String,Integer> sti = toMap( p("A",1), p("B",2) );
Map<Integer,Boolean> itb = toMap( p(1,true), p(42,false) );
}
}
Given a String, I want to create a hashmap that maps a Character to the number of times it shows up in the string.
I saw in Stack Overflow the following line:
Map<String, Long> map =
Arrays.stream(s1.split("")).
collect(Collectors.groupingBy(c -> c, Collectors.counting()));
And it works great, but it maps a String to the counter.
I tried writing it for Characters, and as far as I understood, there isn't a char stream, and I'll have to use IntStream. So I converted it to an IntStream, and used the same Collectors function.
Map<Integer, Long> map =
s1.chars().
collect(Collectors.groupingBy(c -> c, Collectors.counting()));
The above doesn't work. The error is:
Error:(14, 27) java: method collect in interface java.util.stream.IntStream cannot be applied to given types;
required: java.util.function.Supplier<R>,java.util.function.ObjIntConsumer<R>,java.util.function.BiConsumer<R,R>
found: java.util.stream.Collector<java.lang.Object,capture#1 of ?,java.util.Map<java.lang.Object,java.lang.Long>>
reason: cannot infer type-variable(s) R
(actual and formal argument lists differ in length)
What is the difference between the two?