7

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?

Naman
  • 27,789
  • 26
  • 218
  • 353
Jamie
  • 99
  • 2
  • 9

2 Answers2

8

There you go:

Map<String,Integer> themap = 
       IntStream.range (0, keys.length).boxed()
                .collect (Collectors.toMap(i->keys[i],
                                           i->values[i],
                                           Integer::sum,
                                           TreeMap::new));

Output:

{a=1, aa=4, b=8, c=3, d=5}

This is quite similar to the snippet you posted, though, for some reason, the snippet you posted contains no reference to the keys and values arrays.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • There is a slight difference in the output of [this](https://stackoverflow.com/a/46948467/1746118) and [this](https://stackoverflow.com/a/46948406/1746118). Just curious to know, does the `groupingBy` sorts the data as well? – Naman Oct 26 '17 at 07:51
  • 2
    @nullpointer `Map` makes no guarantees about iteration order so the output is for all intents and purposes the same. – Michael Oct 26 '17 at 07:53
  • 1
    @nullpointer My answer returns a `TreeMap` (as in the OPs code), so the keys are sorted. You can also modify the `groupingBy` solution to return a `TreeMap` (by default, the current implementation returns a `HashMap`). – Eran Oct 26 '17 at 07:53
  • Thanks Eran for your answer. I was kind of stuck on how we merge 2 separate arrays. Hence why you don't see any reference of keys and values. I have only obtained a single array prior to your solution. – Jamie Oct 26 '17 at 09:22
3

I don't like using streams when referring to indexes, but you can use groupingBy and summingInt to accomplish this:

Map<String, Integer> result = IntStream.range(0, keys.length)
   .boxed()
   .collect(
       Collectors.groupingBy(
           i -> keys[i],
           Collectors.summingInt(i -> values[i])
       )
   );

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.

Michael
  • 41,989
  • 11
  • 82
  • 128
  • I didnt give too much focus towards sorted map. The requirement was simply to merge two arrays with given values as sum for duplicated keys. Have I been too vague in my question? Let me know and I'll edit. Regardless Michael, I appreciate your answer. It was very warming to learn something new :) – Jamie Oct 26 '17 at 09:17
  • 1
    @Jamie Nope, your question was perfectly clear. Happy to help. – Michael Oct 26 '17 at 10:03