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

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);
主站蜘蛛池模板: 噶尔县| 宁阳县| 渝中区| 杨浦区| 分宜县| 靖边县| 南川市| 将乐县| 安丘市| 芷江| 冷水江市| 镇平县| 新晃| 六安市| 东乌珠穆沁旗| 达拉特旗| 武胜县| 化隆| 淳安县| 治多县| 台南市| 柳河县| 商城县| 邵阳县| 云浮市| 山丹县| 洪洞县| 西乡县| 苏尼特左旗| 长垣县| 蕉岭县| 六盘水市| 潞西市| 湟源县| 西城区| 治多县| 衡阳县| 鄂托克旗| 寿阳县| 杭锦后旗| 谢通门县|