方法一 通过 Collectors.toCollection
testCommodityList
.stream()
.collect(
Collectors.toCollection(
() -> new TreeSet<>(
Comparator.comparing(tc -> tc.getName())
)
)
);
方法二 通过collectingAndThen+toCollection
testCommodityList
.stream()
.collect(
Collectors.collectingAndThen(
Collectors.toCollection(
() -> new TreeSet<>(
Comparator.comparing(tc -> tc.getName())
)
),ArrayList::new
)
);