收藏品来了List<Myclass> mylist;
MyClass 包含各种字段,包括 int id。
我只需要在 mylist 中保留那些具有相同 id 字段的 Myclass 实例。
那些。例如,一个收藏有 10 个副本。它的 ID:0,0,2,3,4,5,6,7,9,9。
应该有一个集合,其中包含 4 个 Myclass 实例,id 为 0、0、9、9
我试着这样做:
mylist.removeAll(mylist.stream()
.collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparingInt(Myclass ::getId))),
ArrayList::new)));
但没有通过。
1 个回答