- 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);
推薦閱讀
- Django+Vue.js商城項目實戰
- Microsoft Exchange Server PowerShell Cookbook(Third Edition)
- 密碼學原理與Java實現
- .NET 4.0面向對象編程漫談:基礎篇
- 量化金融R語言高級教程
- Webpack實戰:入門、進階與調優
- Hands-On GUI Programming with C++ and Qt5
- RocketMQ實戰與原理解析
- Java Web開發實例大全(基礎卷) (軟件工程師開發大系)
- C++程序設計教程
- 從零開始學算法:基于Python
- 絕密原型檔案:看看專業產品經理的原型是什么樣
- Java EE 程序設計
- 歐姆龍PLC編程指令與梯形圖快速入門
- Learning Gerrit Code Review