代码之家  ›  专栏  ›  技术社区  ›  Addie

比较映射和列表时如何将Java 7重构为Java 8[关闭]

  •  0
  • Addie  · 技术社区  · 6 年前

    我想重构以下代码,使其更具可读性。有没有一种方法可以使用streams和Lambda使其更具可读性,或者让代码保持原样有意义吗?

    List<Data> data = ...;
    Map<String, Task> tasks = ...;
    for (Data datum : data) {
        String compKey = datum.getCompKey();
        for (Map.Entry<String, Task> taskEntry : tasks.entrySet()) {
            String taskKey = taskEntry.getKey();
            Task task = taskEntry.getValue();
            if (taskKey != null && task != null) {
                String subKey = Joiner.on(".").useForNull("null").join(Arrays.copyOfRange(taskKey.split("\\."), 0, 3));
                if (compKey.equals(subKey)) {
                    task.setVal1(datum.getVal1());
                    task.setVal2(datum.getVal2());
                    task.setVal3(datum.getVal3());                       
                    break;
                }
            }
        }
    }
    
    2 回复  |  直到 6 年前
        1
  •  3
  •   shmosel    6 年前

    这个怎么样?

    tasks.forEach((key, task) -> {
        if (key != null && task != null) {
            key = String.join(".", Arrays.asList(key.split("\\.")).subList(0, 3));
            data.stream()
                .filter(d -> d.getCompKey().equals(key))
                .findAny()
                .ifPresent(d -> {
                    task.setVal1(d.getVal1());
                    task.setVal2(d.getVal2());
                    task.setVal3(d.getVal3());
                });
        }
    });
    
        2
  •  3
  •   Jacob G.    6 年前

    我不会说这比您的代码更具可读性,但这表明良好的ol’for循环仍然足够:

    List<Data> data = ...;
    Map<String, Task> tasks = ...;
    
    UnaryOperator<String> function = s -> {
        return Joiner.on(".")
                     .useForNull("null")
                     .join(Arrays.copyOfRange(s.split("\\."), 0, 3));
    };
    
    data.forEach(datum -> {
        final String compKey = datum.getCompKey();
    
        tasks.entrySet()
             .stream()
             .filter(e -> e.getKey() != null && e.getValue() != null)
             .filter(e -> compKey.equals(function.apply(e.getKey())))
             .findFirst()
             .map(Map.Entry::getValue)
             .ifPresent(task -> {
                 task.setVal1(datum.getVal1());
                 task.setVal2(datum.getVal2());
                 task.setVal3(datum.getVal3());
             });
    });