0
点赞
收藏
分享

微信扫一扫

lambda表达式集合list根据某个属性去重

醉东枫 2022-03-25 阅读 100
java集合
package com.eg.functionall.utils;

//导入所需要的jar包
import com.alibaba.fastjson.JSON;
import com.eg.functionall.dto.User;
import java.util.*;
import java.util.stream.Collectors;

public class Test {

    public static void main(String[] args) {
        List<User> list = Arrays.asList(
                new User(1,"张三","15810067544",23),
                new User(2,"李四","15810067555",36),
                new User(3,"张三","15810067544",23),
                new User(4,"赵六","15810067555",67)
        );
        List<User> result = list.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getName))), ArrayList::new
        ));
        System.out.println(JSON.toJSON(result));
    }

}

举报

相关推荐

0 条评论