Java 8 stream emitting a stream

前端 未结 2 1327
无人共我
无人共我 2020-12-03 19:41

I have the following file format:

Text1
+ continuation of Text1
+ more continuation of Text1 
Text2
+ continuation of Text2
+ more continuation of Text2
+ ev         


        
2条回答
  •  隐瞒了意图╮
    2020-12-03 20:18

    Assuming that you run this sequentially only and really want to use streams:

     List result = Files.lines(Paths.get("YourPath"))
                .collect(() -> new ArrayList<>(), (list, line) -> {
                    int listSize = list.size();
                    if (line.startsWith("+ ")) {
                        list.set(listSize - 1, list.get(listSize - 1) + line.substring(2));
                    } else {
                        list.add(line);
                    }
                }, (left, right) -> {
                    throw new RuntimeException("Not for parallel processing");
                });
    

提交回复
热议问题