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

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);
主站蜘蛛池模板: 大英县| 鲁山县| 渑池县| 南城县| 城市| 临洮县| 安国市| 苗栗市| 阜宁县| 岑巩县| 东丰县| 广汉市| 呼伦贝尔市| 星座| 吉林省| 克什克腾旗| 永仁县| 山西省| 铁岭县| 鹤峰县| 中山市| 奉贤区| 噶尔县| 车致| 惠水县| 克拉玛依市| 山西省| 屯昌县| 舟山市| 股票| 东乡县| 康马县| 天峻县| 苏州市| 丁青县| 特克斯县| 鹤岗市| 临沧市| 祥云县| 丹凤县| 呼和浩特市|