如何通过拆分包含重复名称的Map <string,list <object =“” >>的键来创建新Map

时间:2019-02-18 14:21:02

标签: java list dictionary java-8

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

示例:

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;
    }

}

2 个答案:

答案 0 :(得分: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())));

答案 1 :(得分:0)

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

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;
  }