- Apache Spark 2.x for Java Developers
- Sourav Gulati Sumit Kumar
- 100字
- 2021-07-02 19:01:58
Collection collectors
The Collectors method also provides ways to reduce the stream into mutable data structures such as lists and sets:
//Collection Collectors
List<String>listCollected = streamSupplier.get().collect(Collectors.toList());
System.out.println("The list collected value of Stream are :: " + listCollected);
Set<String>setCollected=streamSupplier.get().collect(Collectors.toSet());
System.out.println("The set collected value of Stream are :: " + setCollected);
By default, the toSet() method produces a HashSet and hence the results are unsorted. If we want the results to be sorted, we can use the toCollection() method to specify an ordered set, in our case let's use Tree Set:
Set<String>orderedSetCollected=streamSupplier.get().collect(Collectors.toCollection(TreeSet::new));
System.out.println("The ordered set collected value of Stream are :: "+orderedSetCollected);
推薦閱讀
- Mastering Adobe Captivate 2017(Fourth Edition)
- Rust實戰
- Python語言程序設計
- 趣學Python算法100例
- VSTO開發入門教程
- SEO實戰密碼
- Windows Presentation Foundation Development Cookbook
- Visual Studio 2015高級編程(第6版)
- 軟件測試教程
- ArcGIS for Desktop Cookbook
- Geospatial Development By Example with Python
- 計算機應用基礎項目化教程
- Kotlin進階實戰
- Groovy 2 Cookbook
- JavaScript Unit Testing