方法1:
list.stream().collect( Collectors.collectingAndThen( Collectors.toCollection( () -> new TreeSet<>(Comparator.comparing(m -> m.get("id").toString())) ), ArrayList::new ) );
方法2:
public class StreamEx {
public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
}
list.stream().filter(StreamEx.distinctByKey(x ->
x.get("id"))).collect(Collectors.toList());