Lets suppose we were given the following two arrays
String[] keys = new String[] {"a", "b", "c", "aa", "d", "b"}
int[] values = new int[] { 1 , 2 , 3 , 4 , 5 , 6 }
And by merging these 2 arrays into HashTable we get the following
// pseudo-code
Map<String, Integer> dictionary = new HashTable<>(
("a" => 1)
("b" => 8) // because "b" appeared in index 1 and 5
("c" => 3)
("aa" => 4)
("d" => 5)
);
How can we do this using java Lambda style?
So far I have the following:
// this loops through the range given (used for index start and end)
// and sums the values of duplicated keys
tree.listMap = IntStream.range(range[0], range[1]).boxed().collect(
Collectors.toMap(i - > dictionary[i], i - > i + 1, Integer::sum, TreeMap::new)
);
However, I'd like to take 2 arrays, merge them by key and value, where value is the sum of all values for duplicated keys. How can we do this?
There you go:
Output:
This is quite similar to the snippet you posted, though, for some reason, the snippet you posted contains no reference to the
keys
andvalues
arrays.I don't like using streams when referring to indexes, but you can use
groupingBy
andsummingInt
to accomplish this:Note that this works on the assumption that keys and values are both of equal length, so you may want to do some additional validation.