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

后端 未结 6 1743
无人共我
无人共我 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:55

    Try peek

    boolean[] flag = new boolean[1];
    return stream.peek(t -> flag[0] = true).allMatch(Whatever::someCheck) && flag[0]
    

提交回复
热议问题