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

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);
主站蜘蛛池模板: 惠州市| 筠连县| 搜索| 新民市| 博野县| 西青区| 大关县| 白城市| 新宾| 枣阳市| 双辽市| 平昌县| 出国| 安溪县| 沂南县| 万年县| 叙永县| 黔西| 和平区| 呼图壁县| 岳西县| 北安市| 无为县| 瓦房店市| 那坡县| 会同县| 万州区| 莱州市| 莱西市| 三河市| 策勒县| 萍乡市| 肥西县| 宁强县| 金川县| 襄垣县| 军事| 皋兰县| 白玉县| 阿鲁科尔沁旗| 博罗县|