【问题标题】:How to create a new Map by splitting the keys of a Map <String, List<Object>> that contain duplicate names如何通过拆分包含重复名称的 Map <String, List<Object>> 的键来创建新 Map
【发布时间】:2019-02-18 14:21:01
【问题描述】:

我有一个&lt;String, List&lt;Object&gt;&gt; 类型的Map,其中Map 的键是与包含X 和Y 坐标的Object 关联的名称(String)。

例子:

Names (String)    Coordinates

Cord1             [[0.1,0.1,0.1],[0.2,0.3,0.4]]
Cord1,Cord2       [[0.1,0.1]    ,[0.4,0.5]]         
Cord1,Cord2,Cord3 [[0.1,0.1]    ,[0.6,0.7]]

我想要实现的是在有逗号,的情况下拆分名称,这样我就可以只有单个名称,这也会影响坐标并避免重复。

我想要实现的示例:

Cord1 [[0.1,0.1,0.1,0.1,0.1,0.1],[0.2,0.3,0.4,0.5,0.6,0.7]]
Cord2 [[0.01,0.01,0.01,0.01]    ,[0.4,0.5,0.6,0.7]]                    
Cord3 [[0.01,0.01]              ,[0.6,0.7]]

有没有办法做到这一点?

编辑:

我对 Java 8 不是很熟悉,这显然是最好的方法,但我正在尝试一些到目前为止还没有奏效的方法:

List<String> list = Splitter.on(',').splitToList(value);
        for (String element : list) {
           //TO-DO
        }

线对象:

public class Cord {
    private double X;
    private double Y;
    private String name;

    public Cord(double x, double y, String name) {
        this.X=x;
        this.Y=y;
        this.name=name;
    }
    @Override
    public String toString() {
        return "["+X+","+Y+"]";
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getX() {
        return X;
    }
    public void setX(double x) {
        X = x;
    }
    public double getY() {
        return Y;
    }
    public void setY(double y) {
        Y = y;
    }

}

【问题讨论】:

  • 到目前为止你有什么尝试?
  • 查看我的编辑。谢谢

标签: java list dictionary java-8


【解决方案1】:

有来自 java-9 的 flatMapping 的流方式:

import static java.util.stream.Collectors.*;

Map<String, List<Object>> collect = map.entrySet().stream()
        .flatMap(entry -> Arrays.stream(entry.getKey().split(","))
                .map(s -> new AbstractMap.SimpleEntry<>(s, entry.getValue())))
        .collect(groupingBy(Map.Entry::getKey, 
                flatMapping(entry -> entry.getValue().stream(), toList())));

如果你不能使用 java-9,它仍然可以通过流 API 来完成,但它会更加罗嗦。可能在这种情况下,您应该考虑使用 for 循环的解决方案。

Map<String, List<Object>> collect1 = map.entrySet().stream()
        .flatMap(entry -> Arrays.stream(entry.getKey().split(","))
                .map(s -> new AbstractMap.SimpleEntry<>(s, entry.getValue())))
        .collect(groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toList())))
        .entrySet().stream()
        .flatMap(entry -> entry.getValue().stream()
                .flatMap(Collection::stream)
                .map(o -> new AbstractMap.SimpleEntry<>(entry.getKey(), o)))
        .collect(groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toList())));

【讨论】:

  • 尝试执行此操作时出现以下错误:无法推断 flatMap(Function super T,? extends Stream extends R>>)
  • @JohnStef 是你的情况吗? stackoverflow.com/questions/42285370/…
  • 我设法解决了这个问题,现在唯一的问题是它无法识别 toList() 方法(对于 main 类型未定义)。
  • @JohnStef 确保您拥有import static java.util.stream.Collectors.toList; 或来自收集器import static java.util.stream.Collectors.*; 的所有方法
  • 这修复了toList 方法,但又引发了另一个错误The method flatMapping((&lt;no type&gt; entry) -&gt; {}, toList()) is undefined for the type Main
【解决方案2】:

这是我从您的问题中理解的解决方案:

Map<String, List<Object>> doTheThing(Map<String, List<Object>> input){
  Map<String, List<Object>> retr = new Map<String, List<Object>>();
  for(String s1:input.keySet()){//for each Name on the original map
    String[] separatedByCommas = s1.split(",");//split the names by ","
    for(String s2:separatedByCommas){//for each separated by "," name
      if(!retr.containsKey(s2)){
        //if the separated by "," name is not on the retr map just put to the separated by "," name the contents of name
        retr.put(s2,input.get(s1));
      }else{
        //if the separated by "," name is on the retr map add to the separated by "," name the contents of name
        retr.put(s2,retr.get(s2).addAll(input.get(s1));//add to whats already in s2
      }
    }
    return retr;
  }

【讨论】:

  • 我添加了 Object 类。你介意向我解释你方法的前两行吗?第二个地图是新地图,第一个是我已经拥有的地图吗?
  • input 是您已经拥有的地图,retr 是您将返回的满足您想要结束的条件的地图
  • 我还添加了一些 cmets 帮助您更好地理解
猜你喜欢
  • 1970-01-01
  • 2021-04-29
  • 1970-01-01
  • 2018-09-22
  • 1970-01-01
  • 1970-01-01
  • 2020-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多