【问题标题】:Java 8 - triple nested for loopsJava 8 - 三重嵌套的 for 循环
【发布时间】:2020-08-09 01:29:34
【问题描述】:

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

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

【问题讨论】:

  • 你肯定想groupBy,问题是它很复杂(相当混乱)“by”和“mapped to”什么?
  • 让我编辑一下,让它变得简单(希望如此)

标签: java for-loop java-8 nested


【解决方案1】:

这可能是最简洁的方式:

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

【讨论】:

  • @user 不错的解决方案
【解决方案2】:

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

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

【讨论】:

  • 感谢您的回复。拥有另一个对象肯定会更干净,更好。但是,我的返回对象(我使用的 Map 或您使用的 MyObject)将被发送给 Jaxrs 调用者。调用方法对这个Object没有任何想法,因此尝试使用Map。
  • @Tiya 我不是要你返回Map&lt;String, List&lt;MyObject&gt;&gt;。您仍然可以返回Map&lt;String, List&lt;String&gt;&gt;。你试过我的解决方案了吗?
  • @Tiya 查看编辑。如您所见,您的方法的签名根本不需要更改。希望现在更清楚了。
  • 不需要创建对象。 split("\\.",2)
  • @HadiJ 我只是喜欢给事物起名字...String[] 很少告诉你它存储的数据的结构,除了“它是一堆字符串”。这门课以后可能会派上用场,谁知道呢?感谢您提醒split 的第二个参数。
【解决方案3】:

修改数据后,您可以这样做:

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

【讨论】:

    猜你喜欢
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    相关资源
    最近更新 更多