Java stream - purpose of having both anyMatch and noneMatch operations?

自古美人都是妖i 提交于 2019-12-01 06:37:10

Same reason you have a != b, instead of only supporting ! (a == b):

  • Easy of use.
  • Clarity of purpose.

Yes, we totally could. There's at least a moderately reasonable reason for it, though: the ! would go at the very beginning of a stream expression that could be chained many lines long, e.g. you'd have to write

 !collection.stream()
    .map(someMapFunction)
    .filter(someFilterFunction)
    .distinct()
    .sorted(myComparator)
    .map(someOtherMapFunction)
    .filter(someOtherFilterFunction)
    .anyMatch(somePredicate)

...and by the time you've reached the anyMatch when you're reading the code, the negation at the beginning is harder to remember.

(For what it's worth, the JDK generally seems to have a lot fewer redundant methods than other languages I could name.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!