- 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);
推薦閱讀
- Mastering JavaScript Object-Oriented Programming
- Learning Real-time Processing with Spark Streaming
- 深入理解Django:框架內幕與實現原理
- Servlet/JSP深入詳解
- Processing互動編程藝術
- JS全書:JavaScript Web前端開發指南
- Easy Web Development with WaveMaker
- 軟件架構:Python語言實現
- Cocos2d-x學習筆記:完全掌握Lua API與游戲項目開發 (未來書庫)
- Corona SDK Mobile Game Development:Beginner's Guide(Second Edition)
- Visualforce Developer’s guide
- Spring+Spring MVC+MyBatis從零開始學
- Django 3.0應用開發詳解
- 零基礎輕松學C++:青少年趣味編程(全彩版)
- Android系統下Java編程詳解