fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import java.util.stream.Stream;
  10.  
  11. /* Name of the class has to be "Main" only if the class is public. */
  12. class Ideone
  13. {
  14. public static void main (String[] args) throws java.lang.Exception
  15. {
  16. Map < String, Double > map1 =
  17. Map.of(
  18. "X1" , 1d ,
  19. "X2" , 1d
  20. );
  21.  
  22. Map < String, Double > map2 =
  23. Map.of(
  24. "X1" , 2d ,
  25. "X2" , 2d ,
  26. "X3" , 7d
  27. );
  28.  
  29. Map < String, Double > map = new HashMap <>();
  30. Stream
  31. .concat( map1.entrySet().stream() , map2.entrySet().stream() )
  32. .forEach(
  33. stringDoubleEntry ->
  34. map.put(
  35. stringDoubleEntry.getKey() , // key
  36. map.getOrDefault( stringDoubleEntry.getKey() , 0d ) + stringDoubleEntry.getValue() ) // value
  37. );
  38.  
  39. System.out.println( "map = " + map );
  40. }
  41. }
Success #stdin #stdout 0.13s 50720KB
stdin
Standard input is empty
stdout
map = {X1=3.0, X2=3.0, X3=7.0}