zoukankan      html  css  js  c++  java
  • 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);
    
        }
    
    }
  • 相关阅读:
    .NET 统一用户管理 -- 统一鉴权
    .NET 统一用户管理 -- 单点登录
    基于.net 职责链来实现 插件模式
    电商开放平台设计
    docker搭建一个渗透测试环境 bwapp为例
    dcoker运行msf
    关于构造靶场
    判断网站是不是真实ip
    H3C设备配置ARP攻击防御
    Java代码审计 HTTP头操纵 response.addHeader()
  • 原文地址:https://www.cnblogs.com/ooo0/p/7640144.html
Copyright © 2011-2022 走看看