首页 > 技术文章 > Java8

weixiaotao 2019-04-27 11:02 原文

1.去重

// 根据UserName去重
        List<UserEntity> distinct2 = userEntities.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(UserEntity::getUserName))), ArrayList::new)
        );
        // 根据UserName + Worknum 去重
        List<UserEntity> distinct3 = userEntities.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(e-> e.getUserName() + ";" + e.getWorknum()))), ArrayList::new)
        );

 

推荐阅读