fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.stream.*;
  7. import java.util.List;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. List<Map<String, Map<String, Genuineness>>> taskHandles = new ArrayList<>();
  15. Map<String, Map<String, Genuineness>> map1 = new HashMap<>();
  16. Map<String, Genuineness> map1Sub = new HashMap<>();
  17. map1Sub.put("APP_1", new Genuineness(1, 1,1));
  18. map1.put("USER_1", map1Sub);
  19. taskHandles.add(map1);
  20.  
  21.  
  22.  
  23. Map<String, Map<String, Genuineness>> map2 = new HashMap<>();
  24. Map<String, Genuineness> map2Sub = new HashMap<>();
  25. map2Sub.put("APP_1", new Genuineness(1, 1,1));
  26. map2Sub.put("APP_2", new Genuineness(2, 2,2));
  27. map2.put("USER_2", map2Sub);
  28. taskHandles.add(map2);
  29.  
  30.  
  31. Map<String, Map<String, Genuineness>> map3 = new HashMap<>();
  32. Map<String, Genuineness> map3Sub = new HashMap<>();
  33. map3Sub.put("APP_1", new Genuineness(1, 1,1));
  34.  
  35. map3.put("USER_1", map3Sub);
  36. taskHandles.add(map3);
  37.  
  38.  
  39. Map<String, Map<String, Genuineness>> map4 = new HashMap<>();
  40. Map<String, Genuineness> map4Sub = new HashMap<>();
  41. map4Sub.put("APP_1", new Genuineness(1, 1,1));
  42. map4Sub.put("APP_2", new Genuineness(2, 2,2));
  43. map4.put("USER_2", map4Sub);
  44. taskHandles.add(map4);
  45.  
  46.  
  47. final Map<String, Map<String, Genuineness>> map = taskHandles.stream()
  48. .flatMap(m -> m.entrySet().stream()).collect(
  49. Collectors.toMap(Map.Entry::getKey, e -> e.getValue().entrySet().stream()
  50. .collect(
  51. Collectors.toMap(Map.Entry::getKey,
  52. g -> new Genuineness(g.getValue().getTotal(), g.getValue().getTotalGenuine(), g.getValue().getTotalDevelopment()),
  53. (g1, g2) -> new Genuineness(g1.getTotal() + g2.getTotal(),
  54. g1.getTotalGenuine() + g2.getTotalGenuine(),
  55. g1.getTotalDevelopment() + g2.getTotalGenuine()
  56. )
  57. )
  58. ),(l, r) -> {
  59. l.forEach((k, v) -> r.merge(k, v,
  60. (bi, bii) -> new Genuineness(bi.getTotal() + bii.getTotal(),
  61. bi.getTotalGenuine() + bii.getTotalGenuine(),
  62. bi.getTotalDevelopment() + bii.getTotalGenuine())));
  63. return r;
  64. }
  65.  
  66. )
  67. );
  68.  
  69. System.out.println(map);
  70. }
  71. }
  72.  
  73. class Genuineness {
  74. public Genuineness(int total, int totalGenuine, int totalDevelopment) {
  75. this.total = total;
  76. this.totalGenuine = totalGenuine;
  77. this.totalDevelopment = totalDevelopment;
  78. }
  79.  
  80. int total;
  81.  
  82. public int getTotal() {
  83. return total;
  84. }
  85.  
  86. public void setTotal(int total) {
  87. this.total = total;
  88. }
  89.  
  90. public int getTotalGenuine() {
  91. return totalGenuine;
  92. }
  93.  
  94. public void setTotalGenuine(int totalGenuine) {
  95. this.totalGenuine = totalGenuine;
  96. }
  97.  
  98. public int getTotalDevelopment() {
  99. return totalDevelopment;
  100. }
  101.  
  102. public void setTotalDevelopment(int totalDevelopment) {
  103. this.totalDevelopment = totalDevelopment;
  104. }
  105.  
  106. int totalGenuine;
  107.  
  108. @Override
  109. public String toString() {
  110. return "Genuineness{" +
  111. "total=" + total +
  112. ", totalGenuine=" + totalGenuine +
  113. ", totalDevelopment=" + totalDevelopment +
  114. '}';
  115. }
  116.  
  117. int totalDevelopment;
  118. }
Success #stdin #stdout 0.15s 2184192KB
stdin
Standard input is empty
stdout
{USER_2={APP_1=Genuineness{total=2, totalGenuine=2, totalDevelopment=2}, APP_2=Genuineness{total=4, totalGenuine=4, totalDevelopment=4}}, USER_1={APP_1=Genuineness{total=2, totalGenuine=2, totalDevelopment=2}}}