简体   繁体   English

如何在流中查找字符串数组的值

[英]How to find values of a string array in a stream

I am trying to get a solution to the following problem.我正在尝试解决以下问题。

How can I find values from "conditions" in "stream"?如何从“流”中的“条件”中找到值? At the moment I can only filter with the "line.contains method".目前我只能使用“line.contains 方法”进行过滤。 But I want that the user can give a number of conditions which would be saved in the Array "conditions".但我希望用户可以给出一些条件,这些条件将保存在数组“条件”中。 I tried to build a for-loop in the stream.filter but I failed.^^ Maybe you know an efficient way.我试图在 stream.filter 中构建一个 for 循环,但我失败了。^^ 也许你知道一种有效的方法。 :) :)

Thanks.谢谢。

private static void streamSelectedFile(String p, String[] conditions) {
    try (
        Stream<String> stream = Files.lines(Paths.get(p), StandardCharsets.ISO_8859_1)) {       
        Stream<String> filteredStream = 
                stream.filter(line -> line.contains("conditionValue"));     
        filteredStream.forEach(elem -> {
            System.out.println(elem + " Path: " + p);
        });
    } catch (IOException e) {
        ...
    }
}   

使用allMatch

stream.filter(line -> Stream.of(conditions).allMatch(line::contains))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM