Is Java 8 findFirst().isPresent() more efficient than count() > 0?

前端 未结 3 1911
醉酒成梦
醉酒成梦 2021-02-19 10:00

Given I have a stream Stream stream = list.stream().filter(some predicate) where the list is very large, is it more efficient to check whether the stream

3条回答
  •  野性不改
    2021-02-19 10:55

    I would recommend using list.stream().anyMatch(some predicate), which is a terminal operation for exactly this case. It's not only more efficient than stream.count(), but it won't hang on infinite streams.

提交回复
热议问题