实体集合中属性相同的排除
public static List<User> removeDuplicate(List<User> users){
Set<User> set = new TreeSet<>(new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
return o1.getUserName().compareTo(o2.getUserName());
}
});
set.addAll(users);
return new ArrayList<User>(set);
}
实体集合的groupBy操作
public static void main(String[] args){
List<TestGroup> groupList = new ArrayList<>();
TestGroup g1 = new TestGroup();
g1.setVip("精品");
g1.setOthter("test");
TestGroup g2 = new TestGroup();
g2.setVip("精品");
g2.setOthter("test2");
TestGroup g3 = new TestGroup();
g3.setVip("运营");
g3.setOthter("test2");
TestGroup g4 = new TestGroup();
g4.setVip("高级");
g4.setOthter("test2");
TestGroup g5 = new TestGroup();
g5.setVip("高级");
g5.setOthter("test2");
TestGroup g6 = new TestGroup();
g6.setVip("定制");
g6.setOthter("test2");
TestGroup g7 = new TestGroup();
g7.setVip("定制");
g7.setOthter("test2");
TestGroup g8 = new TestGroup();
g8.setVip("定制");
g8.setOthter("test2");
TestGroup g9 = new TestGroup();
g9.setVip("定制");
g9.setOthter("test2");
List<TestGroup> groups = Arrays.asList(g1,g2,g3,g4,g5,g6,g7,g8,g9);
groupList.addAll(groups);
Map<String,Long> result = groups.stream().
collect(Collectors.groupingBy(TestGroup::getVip,Collectors.counting()));
for(String s:result.keySet()){
System.out.println(s + ":" + result.get(s));
}
}