Java 8 - 三重嵌套 for 循环

标签 java for-loop java-8 nested

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

public static void main(String[] args) {
    List<String> tempList = createTempList();
    createMap8(tempList);
}

public static Map<String,List<String>> createMap8(final List<String> tempList) {
    Map<String,List<String>> vlanFoos = new HashMap<String,List<String>>();
    for(int i=0; i< tempList.size(); i++) {
        String[] idsI = tempList.get(i).split("\\.");
        String vlanI = idsI[0];
        List<String> fooList = new ArrayList<String>();
        for(int j = 0 ; j < tempList.size() ; j ++) {
            String foo = "";
            String[] idsJ = tempList.get(j).split("\\.");
            String vlanJ = idsJ[0];
            if(vlanI.equals(vlanJ)) {
                        for(int k = 1; k < idsJ.length; k++) {
                            foo = foo + idsJ[k];
                            if(idsJ.length - k != 1) {
                                foo = foo + ".";
                            }
                        }
            }
            if(!foo.isEmpty()) {
                fooList.add(foo);
            }
        }
        vlanFoos.put(vlanI, fooList);
    }
    return vlanFoos;
}

输入:

private static List<String> createTempList() {
    List<String> tempList = new ArrayList<String>();
    tempList.add("1.24.75.13.45.91.0");
    tempList.add("1.88.213.110.66.182.127");
    tempList.add("1579.204.45.224.38.12.161");
    tempList.add("1580.204.45.224.38.12.161");
    tempList.add("21.204.45.224.38.12.161");
    tempList.add("39.204.45.224.38.12.161");
    tempList.add("5.12.244.213.2.178.192");
    tempList.add("5.204.45.224.38.12.161");
    tempList.add("5.212.202.109.116.154.217");
    tempList.add("5.212.202.109.116.154.218");
    tempList.add("5.40.153.58.148.24.67");
    tempList.add("5.76.177.205.33.164.80");
    tempList.add("5.84.236.47.13.223.64");
    tempList.add("5.88.213.110.66.182.128");
    return tempList;
}

输出:

 {1=[24.75.13.45.91.0, 88.213.110.66.182.127], 
 1579=[204.45.224.38.12.161], 
 5=[12.244.213.2.178.192, 204.45.224.38.12.161, 212.202.109.116.154.217, 212.202.109.116.154.218, 40.153.58.148.24.67, 76.177.205.33.164.80, 84.236.47.13.223.64, 88.213.110.66.182.128], 
 39=[204.45.224.38.12.161], 
 1580=[204.45.224.38.12.161], 
 21=[204.45.224.38.12.161]}

最佳答案

这可能是最简洁的方法:

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

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

关于Java 8 - 三重嵌套 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61425915/

相关文章:

php - foreach 与一个项目数组

c++ - 捕捉按键 C++

java - 如何实现 Java 8 接口(interface)但在 Java 6 上运行?

java - 修改日期方法

C# 为什么 List.Remove() 中断 'for loop' ?

scala - JDK 1.8 - 不能导入 sbt 项目

Java 逻辑根据收到的代码更新字符串

Java游戏剪刀石头布。无法找出用户控制循环的主要方法来继续播放

java - 创建数组时堆空间不足

java - 使用 Cloud Foundry 的弹性运行时容器进行 Jenkins 构建