- Apache Spark 2.x for Java Developers
- Sourav Gulati Sumit Kumar
- 165字
- 2021-07-02 19:01:59
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);
推薦閱讀
- 大學計算機基礎(chǔ)(第三版)
- Node.js 10實戰(zhàn)
- Django開發(fā)從入門到實踐
- Learning ASP.NET Core 2.0
- 基于免疫進化的算法及應(yīng)用研究
- Java游戲服務(wù)器架構(gòu)實戰(zhàn)
- Designing Hyper-V Solutions
- Python時間序列預(yù)測
- 微信小程序全棧開發(fā)技術(shù)與實戰(zhàn)(微課版)
- Java并發(fā)編程之美
- Application Development with Swift
- 深度學習程序設(shè)計實戰(zhàn)
- JavaScript編程精解(原書第2版)
- AI自動化測試:技術(shù)原理、平臺搭建與工程實踐
- 從零開始:C語言快速入門教程