gpt4 book ai didi

java - 累积 Java Stream,然后才处理它

转载 作者:行者123 更新时间:2023-11-30 07:04:58 28 4
gpt4 key购买 nike

我有一个如下所示的文档:

数据.txt

100, "some text"
101, "more text"
102, "even more text"

我使用正则表达式处理它并返回一个新的处理文档,如下所示:

Stream<String> lines = Files.lines(Paths.get(data.txt);
Pattern regex = Pattern.compile("([\\d{1,3}]),(.*)");

List<MyClass> result =
lines.map(regex::matcher)
.filter(Matcher::find)
.map(m -> new MyClass(m.group(1), m.group(2)) //MyClass(int id, String text)
.collect(Collectors.toList());

这将返回已处理的 MyClass 列表。可以并行运行,一切正常。

问题是我现在有这个:

数据2.txt

101, "some text
the text continues in the next line
and maybe in the next"
102, "for a random
number
of lines"
103, "until the new pattern of new id comma appears"

所以,我需要以某种方式加入从流中读取的行,直到出现新的匹配项。 (类似于缓冲区之类的东西?)

我尝试收集字符串,然后收集 MyClass(),但没有成功,因为我实际上无法拆分流。

我想到了Reduce来连接行,但我只会连接行,并且无法减少并生成新的行流。

有什么想法如何用 java 8 流解决这个问题吗?

最佳答案

这是java.util.Scanner的工作。对于即将推出的 Java 9,您可以编写:

List<MyClass> result;
try(Scanner s=new Scanner(Paths.get("data.txt"))) {
result = s.findAll("(\\d{1,3}),\\s*\"([^\"]*)\"")
//MyClass(int id, String text)
.map(m -> new MyClass(Integer.parseInt(m.group(1)), m.group(2)))
.collect(Collectors.toList());
}
result.forEach(System.out::println);

但是由于 Java 8 中不存在生成 findAllStream,因此我们需要一个辅助方法:

private static Stream<MatchResult> matches(Scanner s, String pattern) {
Pattern compiled=Pattern.compile(pattern);
return StreamSupport.stream(
new Spliterators.AbstractSpliterator<MatchResult>(1000,
Spliterator.ORDERED|Spliterator.NONNULL) {
@Override
public boolean tryAdvance(Consumer<? super MatchResult> action) {
if(s.findWithinHorizon(compiled, 0)==null) return false;
action.accept(s.match());
return true;
}
}, false);
}

用这个辅助方法替换findAll,我们得到

List<MyClass> result;
try(Scanner s=new Scanner(Paths.get("data.txt"))) {

result = matches(s, "(\\d{1,3}),\\s*\"([^\"]*)\"")
// MyClass(int id, String text)
.map(m -> new MyClass(Integer.parseInt(m.group(1)), m.group(2)))
.collect(Collectors.toList());
}

关于java - 累积 Java Stream,然后才处理它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40295750/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com