Convert an loop (while and for) to stream

前端 未结 4 1549
北海茫月
北海茫月 2021-01-05 00:33

I have started working with Java 8 and trying to convert some loops and old syntax in my code to lambdas and streams.

So for example, I\'m trying to convert this whi

4条回答
  •  既然无缘
    2021-01-05 00:46

    To answer your question, it's a 1-liner:

    List list = oldList.stream()
        .filter(line -> map.keySet().stream().anyMatch(line::startsWith))
        .map(line -> map.entrySet().stream()
            .filter(entry -> line.startsWith(entry.getKey()))
            .map(entry -> line.replace(entry.getKey(), entry.getValue()))
            .findFirst().get())
        .collect(Collectors.toList());
    

提交回复
热议问题