• java List 去重


    1.使用 jdk8 中 stream.distinct().collector

    需要重写实体类中的 equals()和hashCode()方法

    @Data
    @EqualsAndHashCode(callSuper = true)
    @Accessors(chain = true)
    public class SyncDataNodeInfo extends BaseEntity {
    
        private static final long serialVersionUID = 1L;
        /**
         * 当前节点ID
         */
        private String nodeId;
    
        /**
         * 当前节点name
         */
        private String currentNodeName;
    
    
        @Override
        public boolean equals(Object o){
            if(this == o){
                return true;
            }
            if(o == null || getClass() != o.getClass()){
                return false;
            }
            SyncDataNodeInfo syncDataNodeInfo = (SyncDataNodeInfo) o;
            return nodeId.equals(syncDataNodeInfo.nodeId);
        }
    
        @Override
        public int hashCode(){
            return Objects.hash(nodeId);
        }
    }
    
    public static void main(String[] args) {
            List<SyncDataNodeInfo> newSyncDataNodeInfoList = new ArrayList<>();
            SyncDataNodeInfo syncDataNodeInfo = new SyncDataNodeInfo();
            syncDataNodeInfo.setParentNodeId("111");
            syncDataNodeInfo.setCurrentNodeName("zzz");
            newSyncDataNodeInfoList.add(syncDataNodeInfo);
            
            newSyncDataNodeInfoList.stream().distinct().collect(Collectors.toList());
        }
    

      

     针对 List中的String和普通的int,long等可以直接使用,不需要重写equals和hashCode方法

    stream().distinct().collect(Collectors.toList());

  • 相关阅读:
    windows修复移动硬盘文件夹打不来的问题
    secureCRT设置
    core dump配置
    ssh免密登陆的问题解决
    crontab的环境变量
    tmux配置
    shell的技巧笔记
    记一次route配置不起作用的问题解决过程
    crontab笔记
    Python中的一个诡异编码问题的追踪
  • 原文地址:https://www.cnblogs.com/pass-ion/p/14246616.html
Copyright © 2020-2023  润新知