Java 8-三重嵌套for循环

Java 8-三重嵌套for循环,java,for-loop,java-8,nested,Java,For Loop,Java 8,Nested,我已经用Java编写了下面的代码,并试图将其转换为Java8。 对于输入(createTempList方法创建的列表)和输出(键为字符串的第一个小数点,值为字符串的其他小数点的映射),代码按预期工作 我建议您创建另一个类来进行字符串分析: // please give this a better name! // I don't know what the numbers mean, but you should! class MyObject { private String key;

我已经用Java编写了下面的代码,并试图将其转换为Java8。 对于输入(createTempList方法创建的列表)和输出(键为字符串的第一个小数点,值为字符串的其他小数点的映射),代码按预期工作


我建议您创建另一个类来进行字符串分析:

// please give this a better name!
// I don't know what the numbers mean, but you should!
class MyObject {
    private String key;
    private String value;

    public String getKey() {
        return key;
    }

    public String getValue() {
        return value;
    }

    public MyObject(String s) {
        // here I split the string into the first and the rest
        String[] parts = s.split("\\.", 2);
        key = parts[0];
        value = parts[1];
    }
}
然后,对于流,分组方式如下所示:

public static Map<String,List<String>> createMap8(final List<String> tempList) {

    return tempList.stream().map(MyObject::new).collect(
            Collectors.groupingBy(
                    MyObject::getKey, Collectors.mapping(MyObject::getValue, Collectors.toList())
            )
    );
}
publicstaticmap-createMap8(最终列表模板){
返回templast.stream().map(MyObject::new).collect(
收集者分组(
MyObject::getKey,Collectors.mapping(MyObject::getValue,Collectors.toList())
)
);
}

这可能是最简洁的方法:

public static Map<String, List<String>> createMap8(final List <String> tempList) {
    return tempList.stream()
                   .map(s -> s.split("\\.", 2))
                   .collect(groupingBy(p -> p[0], mapping(a -> a[1], toList())));
}
publicstaticmap-createMap8(最终列表模板){
return templast.stream()
.map(s->s.split(“\\.”,2))
.collect(分组方式(p->p[0],映射方式(a->a[1],toList());
}

对于createTempList方法,您可以只使用Arrays.asList

修改数据后,您可以执行以下操作:

Map<String, List<String>> map = tempList.stream()
         .map(t -> t.split("\\.", 2))
         .collect(Collectors.toMap(a -> a[0], v -> new ArrayList<>(singletonList(v[1])),
                                   (l1, l2) -> {l1.addAll(l2); return l1;}
                                  )
                 );
Map Map=templast.stream()
.map(t->t.split(“\\.”,2))
.collect(Collectors.toMap(a->a[0],v->newarraylist(singletonList(v[1])),
(l1,l2)->{l1.addAll(l2);返回l1;}
)
);

你肯定想
groupBy
,问题是它很复杂(相当混乱)“by”和“mapped to”什么?让我来编辑它,让它变得简单(希望如此)谢谢你的回复。拥有另一个物体绝对是更干净更好的。但是,我的返回对象(我使用的映射或您使用的MyObject)将被发送到Jaxrs调用方。调用方法对此对象没有任何概念,因此,尝试使用Map。@Tiya我不是告诉您返回
Map
。您仍然可以返回
映射
。你试过我的解决方案吗?@Tiya看到编辑了。正如您所看到的,您的方法的签名根本不需要更改。希望这能让事情变得更清楚。不需要创建对象<代码>拆分(“\\.”,2)@HadiJ我只是喜欢给事物命名。。。一个
String[]
告诉你的关于它存储的数据结构的信息很少,除了“它是一堆字符串”。这门课以后可能会派上用场,谁知道呢?谢谢你提醒我关于
split
的第二个参数。@user很好的解决方案
public static Map<String,List<String>> createMap8(final List<String> tempList) {

    return tempList.stream().map(MyObject::new).collect(
            Collectors.groupingBy(
                    MyObject::getKey, Collectors.mapping(MyObject::getValue, Collectors.toList())
            )
    );
}
public static Map<String, List<String>> createMap8(final List <String> tempList) {
    return tempList.stream()
                   .map(s -> s.split("\\.", 2))
                   .collect(groupingBy(p -> p[0], mapping(a -> a[1], toList())));
}
Map<String, List<String>> map = tempList.stream()
         .map(t -> t.split("\\.", 2))
         .collect(Collectors.toMap(a -> a[0], v -> new ArrayList<>(singletonList(v[1])),
                                   (l1, l2) -> {l1.addAll(l2); return l1;}
                                  )
                 );