How to match stream elements but return false if non exists?

后端 未结 6 1731
无人共我
无人共我 2020-12-09 11:58

I have a stream and would like to check if all match a filter. If all match, return true.

But, if the stream is empty, I\'d like to return false

6条回答
  •  一生所求
    2020-12-09 12:52

    If you are OK with losing your characteristics and parallelism, this for example:

     public static boolean validate(Stream stream) {
        Iterator it = stream.iterator();
    
        if (!it.hasNext()) {
            return false;
        }
    
        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, 0), false)
                .peek(System.out::println)
                .allMatch(x -> x.contains("a"));
    
    }
    

提交回复
热议问题