使用Java 8的流(streams)来从另一个嵌套的Map构建一个Map of Map。

huangapple go评论115阅读模式
英文:

Using java 8 streams to build a map of map from another map of map

问题

  1. 我在Java中有一个Map集合
  2. Map<AreaDate, Map<AreaCategory, List<Location>>> dateWiseAreas
  3. 使用Java 8我想构建一个地区映射
  4. Map<Area, Map<AreaCategory, List<Location>>> areas
  5. **我使用了以下逻辑但是出现了重复的键错误提示Area键是重复的**
  6. Map<Area, Map<AreaCategory, List<Location>>> areas = dateWiseAreas.entrySet().stream()
  7. .collect(Collectors.toMap(
  8. k -> k.getKey().area(),
  9. v -> v.getValue()
  10. .entrySet()
  11. .stream()
  12. .collect(Collectors.toMap(
  13. Map.Entry::getKey, Map.Entry::getValue,
  14. (oldValue, newValue) -> oldValue, LinkedHashMap::new))));
  15. Java
  16. enum Area {
  17. EUROPE,
  18. AUSTRALIA,
  19. ASIA;
  20. }
  21. class AreaDate {
  22. Area area;
  23. LocalDate date;
  24. int priority;
  25. }
  26. enum AreaCategory {
  27. NORTH,
  28. SOUTH,
  29. WEST,
  30. EAST;
  31. }
  32. class Location {
  33. int xaxis;
  34. int yaxis;
  35. }
英文:

I have a Map collection in java

  1. Map&lt;AreaDate, Map&lt;AreaCategory, List&lt;Location&gt;&gt; dateWiseAreas

using java 8 I want to build a map of

  1. Map&lt;Area, Map&lt;AreaCategory, List&lt;Location&gt;&gt; areas

I used the below logic and I get a duplicate key error saying Area key is duplicate

  1. Map&lt;Area, Map&lt;AreaCategory, List&lt;Location&gt;&gt; areas = dateWiseAreas.entrySet().stream()
  2. .collect(toMap(k -&gt; k.getKey().area(),
  3. v -&gt; v.getValue()
  4. .entrySet()
  5. .stream()
  6. .collect(Collectors.toMap(Map.Entry::getKey(), Map::Entry::getValue,
  7. (oldValue,newValue) -&gt; oldValue, LinkedHashMap::new))));

Java classes

  1. Area {
  2. EUROPE,
  3. AUSTRALIA,
  4. ASIA;
  5. }
  6. AreaDate {
  7. Area area;
  8. LocalDate date;
  9. int priority;
  10. }
  11. AreaCategory {
  12. NORTH,
  13. SOUTH,
  14. WEST,
  15. EAST;
  16. }
  17. Location {
  18. int xaxis;
  19. int yaxis;
  20. }

答案1

得分: 0

你需要在外部的collect中使用toMap的三个参数版本,以避免在同一个区域在不同日期下出现多次且日期不同的情况下出现冲突的键。

类似这样(未经测试):

  1. Map<Area, Map<AreaCategory, List<Location>>> areas = dateWiseAreas.entrySet().stream()
  2. .collect(toMap(k -> k.getKey().area(),
  3. v -> v.getValue()
  4. .entrySet()
  5. .stream()
  6. .collect(Collectors.toMap(Map.Entry::getKey(), Map.Entry::getValue,
  7. (oldValue, newValue) -> oldValue, LinkedHashMap::new)),
  8. (oldValue, newValue) -> oldValue
  9. ));
英文:

You need to use the 3 argument version of toMap in the outer collect to avoid conflict on keys where the same area is found multiple times with different dates.

Something like (untested)

  1. Map&lt;Area, Map&lt;AreaCategory, List&lt;Location&gt;&gt; areas = dateWiseAreas.entrySet().stream()
  2. .collect(toMap(k -&gt; k.getKey().area(),
  3. v -&gt; v.getValue()
  4. .entrySet()
  5. .stream()
  6. .collect(Collectors.toMap(Map.Entry::getKey(), Map::Entry::getValue,
  7. (oldValue,newValue) -&gt; oldValue, LinkedHashMap::new)),
  8. (oldValue, newvalue) -&gt; oldValue
  9. ));

答案2

得分: 0

你可以使用toMap()的重载版本,该版本允许您传递一个BinaryOperator<V>,其中在您的情况下,VMap<AreaCategory, List<Location>>

  1. Map<Area, Map<AreaCategory, List<Location>>> areas = dateWiseAreas.entrySet().stream()
  2. .collect(Collectors.toMap(
  3. e -> e.getKey().area(),
  4. Map.Entry::getValue, // 似乎没有必要再复制整个内容
  5. (a, b) -> {
  6. // 将Map b合并到Map a
  7. b.forEach((key, values) -> a.merge(key, values, (c, d) -> {
  8. // 将d中的所有位置添加到a
  9. c.addAll(d);
  10. return c;
  11. });
  12. return b;
  13. })
  14. );
英文:

You can use the overload of toMap() which allows you to pass a BinaryOperator&lt;V&gt; where V in your case is Map&lt;AreaCategory, List&lt;Location&gt;&gt;

  1. Map&lt;Area, Map&lt;AreaCategory, List&lt;Location&gt;&gt; areas = dateWiseAreas.entrySet().stream()
  2. .collect(Collectors.toMap(
  3. e -&gt; e.getKey().area(),
  4. Map.Entry::getValue, // there seems to be no need to copy the whole thing again
  5. (a, b) -&gt; {
  6. // merging Map b into Map a
  7. b.forEach((key, values) -&gt; a.merge(key, values, (c, d) -&gt; {
  8. // adding all locations from d to a
  9. c.addAll(d);
  10. return c;
  11. });
  12. return b;
  13. })
  14. );

huangapple
  • 本文由 发表于 2020年10月1日 23:02:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64158091.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定