Java8

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)
        );
相關文章
相關標籤/搜索