官术网_书友最值得收藏!

Matching

Streams APIs provide multiple terminal methods to match a predicate against the elements of the stream:

  • All element match the predicate: For validating if all the elements match the predicate, the allMatch() method is used. If all the elements match the predicate only then will the result be a Boolean true:
boolean matchesAll =streamSupplier.get().allMatch(x->x.toString().length() > 1);
System.out.println("All the elemetns have lenght greater than 1 ::"+matchesAll);
  • No elements match the predicate: If none of the elements match the predicate condition, the noneMatch() method returns the Boolean value true or else it is false:
boolean noneMatches =streamSupplier.get().noneMatch(x->x.toString().length() > 1);
System.out.println("None of the elemetns have lenght greater than 1 ::"+noneMatches);
  • Any element matches the predicate: This is a special matching function as it short circuits the result, that is, the moment an element matches the predicate, the anyMatch() method returns a true and exits the operation:
boolean anyMatches =streamSupplier.get().peek(x->System.out.println("Element being iterated is :: "+x)).anyMatch(x->x.toString().length() == 2);
System.out.println("The short circuit terminal operation finished with return value :: "+anyMatches);
主站蜘蛛池模板: 蓬安县| 岳西县| 佳木斯市| 慈利县| 嘉善县| 理塘县| 海淀区| 许昌市| 莫力| 云梦县| 柯坪县| 泰顺县| 扶绥县| 比如县| 股票| 宁波市| 罗田县| 渝北区| 福海县| 农安县| 鸡泽县| 漾濞| 天气| 盐池县| 文登市| 淮阳县| 和平县| 清丰县| 和林格尔县| 新昌县| 沁水县| 峨边| 丹棱县| 怀远县| 襄垣县| 天全县| 通榆县| 江孜县| 宁陵县| 开鲁县| 古浪县|