list根据某个字段去重

 

方法一:使用Set

复制代码
List<User> newList = new ArrayList<User>();
Set<String> set = new HashSet<String>();
for (User user : list) {
    String userName = user.getName();
    if (!set.contains(userName)) { //set中不包含重复的
        set.add(userName);
        newList.add(user);
    }
}

System.out.println("Set去重后的集合: " + newList);
复制代码

 

方法二:使用Map

复制代码
List<User> newList = new ArrayList<User>();

HashMap<String, String> map = new HashMap<String, String>();
for (User user : list) {
    String userName = user.getName();
    String value = map.get(userName);
    if (StringUtils.isBlank(value)) { //如果value是空的  说明取到的这个name是第一次取到
        map.put(userName, userName);
        newList.add(user); //newList就是我们想要的去重之后的结果
    }
}

System.out.println("Map去重后的集合: " + newList);
复制代码

 

方法三:使用List(可以根据多个字段)

复制代码
List<String> newList = new ArrayList<String>();
List<User> userList = new ArrayList<User>();
for (User cd : list) {
    if (!newList.contains("" + cd.getAge())) { // 多个字段的话"" + cd.getAge() + cd.getSex()
        newList.add("" + cd.getAge()); // 多个字段的话"" + cd.getAge() + cd.getSex()
        userList.add(cd);
    }
}

System.out.println("List去重后的集合: " + userList);
复制代码

 

 

 

 

附:测试User类

复制代码
/**
 * list根据某个字段去重
 *
 * @author xc
 * @version V1.0 2017年9月30日
 */
public class list根据某个字段去重 {

    private static List<User> list;

    static {
        User user1 = new User("zhangsan1", 20, true);
        User user2 = new User("zhangsan2", 20, true);
        User user3 = new User("zhangsan1", 30, true);
        list = new ArrayList<User>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
    }

    public static void main(String[] args) {
        duplicateMap();
        duplicateSet();
        duplicateList();
    }

    /**
     * 使用List
     */
    private static void duplicateList() {
        List<String> newList = new ArrayList<String>();
        List<User> userList = new ArrayList<User>();
        for (User cd : list) {
            if (!newList.contains("" + cd.getAge() + cd.getSex())) {
                newList.add("" + cd.getAge() + cd.getSex());
                userList.add(cd);
            }
        }

        System.out.println("List去重后的集合: " + userList);
    }

    /**
     * 使用set
     */
    private static void duplicateSet() {
        List<User> newList = new ArrayList<User>();
        Set<String> set = new HashSet<String>();
        for (User user : list) {
            if (user == null) {
                continue;
            }
            String userName = user.getName();
            if (userName != null) {
                if (!set.contains(userName)) { //set中不包含重复的
                    set.add(userName);
                    newList.add(user);
                } else {
                    continue;
                }
            }
        }

        System.out.println("Set去重后的集合: " + newList);

    }

    /**
     * 使用map
     */
    private static void duplicateMap() {
        List<User> newList = new ArrayList<User>();

        HashMap<String, String> map = new HashMap<String, String>();
        for (User user : list) {
            if (user == null) {
                continue;
            }
            String name = user.getName();
            if (name != null) {
                String value = map.get(name);
                if (StringUtils.isBlank(value)) { //如果value是空的  说明取到的这个name是第一次取到
                    map.put(name, name);
                    newList.add(user); //newList就是我们想要的去重之后的结果
                } else {
                    continue;
                }
            }
        }

        System.out.println("Map去重后的集合: " + newList);

    }

}
复制代码

 

posted @   草木物语  阅读(16029)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示