List A和B

A.removeAll(B);
A.addAll(B);

例如有如下实体类:


/**
* hashset是如何保持元素的唯一性呢?
* 是通过元素的hashcode和equals来表示:
* 如果hashCode值一样,则比较equals是否为true
* 如果hashCode值不一样,不用比较equals
*/
/**
* List是如何集合中元素相同的呢?
* 是通过元素的hashcode和equals来表示:
* 如果hashCode值一样,则比较equals是否为true
* 如果hashCode值不一样,不用比较equals
*/
public class UserTable {
private String linkdoodid; private String linkdoodname;   public UserTable() {
super();
}
  public UserTable(String linkdoodid,String linkdoodname){
    supert();
    this.linkdoodid=linkdoodid;
    this.linkdoodname=linkdoodname;
  } public String getLinkdoodid() {
return linkdoodid;
} public void setLinkdoodid(String linkdoodid) {
this.linkdoodid = linkdoodid == null ? null : linkdoodid.trim();
} public String getLinkdoodname() {
return linkdoodname;
} public void setLinkdoodname(String linkdoodname) {
this.linkdoodname = linkdoodname == null ? null : linkdoodname.trim();
} @Override
public boolean equals(Object obj) {
if (!(obj instanceof UserTable)) {
return false;
}
UserTable userTable = (UserTable) obj;
return this.linkdoodid.equals(userTable.linkdoodid);
} @Override
public int hashCode() {
return linkdoodid.hashCode();
}
}

测试:

public class HashSetTest {
public static void main(String[] args) {
    //List
    List<UserTable> listA=new ArrayList<UserTable>();
    listA.add(new UserTable("A1001","LJ"));
    listB.add(new UserTable("B1002","MH"));     List<UserTable> listB=new ArrayList<UserTable>();
    listB.add(new UserTable("B1002","SM"));
    listB.add(new UserTable("C1001","TM"));
    
    listA.removeAll(listB);//由于UserTable的hashCode和equal 都是以linkdoodid 来判断,所以“B1002”算重复元素    
    listA.addAll(listB);     //HashSet
   HashSet<UserTable> hs = new HashSet<UserTable>();
   hs.add(new UserTable("a1", 20));
   hs.add(new UserTable("a2", 30));
   hs.add(new UserTable("a3", 40));
   hs.add(new UserTable("a3", 40));
   Iterator<Person> iterator = hs.iterator();
   while(iterator.hasNext()){
   Person p = iterator.next();
   System.out.println(p.getName()+" "+p.getAge());
  }
}
}

Java List合并去重的更多相关文章

  1. JAVA List<> 合并去重

    List<A>和List<B>,A/B中均没有重复的,现在保证A/B合并为C,且C中没有重复的. 参考http://blog.csdn.net/secondjanuary/ar ...

  2. PHP数组合并+与array_merge的区别分析 & 对多个数组合并去重技巧

    PHP中两个数组合并可以使用+或者array_merge,但之间还是有区别的,而且这些区别如果了解不清楚项目中会要命的! 主要区别是两个或者多个数组中如果出现相同键名,键名分为字符串或者数字,需要注意 ...

  3. java poi 合并单元格

    java poi 合并单元格 2017年03月29日 16:39:01 翠烟你懊恼 阅读数:26561   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.n ...

  4. 两个List合并去重

    今天遇到一个合并去重问题,从网上搜索一样总结出来两个比较简单的方法,这里去重是只能取出地址相同的数据,例如:如果两个字符串的值相同但都是单独new出来的这样去不了 @Test public void ...

  5. Java数组合并方法学习。

    参考博客: https://blog.csdn.net/liu_005/article/details/72760392 https://blog.csdn.net/jaycee110905/arti ...

  6. linux shell文件合并 去重 分割

    1,合并+去重+分割 转载:shell 文件合并,去重,分割 - kakaisgood - 博客园 (cnblogs.com) 第一:两个文件的交集,并集前提条件:每个文件中不得有重复行1. 取出两个 ...

  7. java实现合并两个已经排序的列表

    相对于C++来说,Java的最大特点之一就是没有令人困惑的指针,但是我们不可否认,在某些特定的情境下,指针确实算的上一把利刃.虽然Java中没有明确定义出指针,但是由于类的思想,我们可以使用class ...

  8. Java文件合并

    文件分割与合并是一个常见需求,比如:上传大文件时,可以先分割成小块,传到服务器后,再进行合并.很多高大上的分布式文件系统(比如:google的GFS.taobao的TFS)里,也是按block为单位, ...

  9. Java+XSL合并多个XML文件

    使用 Java 解析 XML 文件有许多成熟的工具,如 dom4j 等等.但在一些场景中,我们可能使用 Ant.Maven 等构建工具对多个 XML 文件进行合并,我们希望可以直接通过脚本文件,或者简 ...

随机推荐

  1. MySQL主从同步配置(Ubuntu)

    一.在两台Linux机器上安装MySQL 二.Master主服务器配置(192.168.1.3) 1.编辑my.cnf(命令查找文件位置:find / -name my.cnf) vi /etc/my ...

  2. SSH框架整合项目(一)——搭建平台和引入依赖

    前言:这个项目是我的第一个实验性项目,最初的立意是制作一个个性化的BBS.由于BBS能够综合大部分功能,因此作为练手的项目来说再好不过.从写第一行代码到完成测试版大概历时2周.中间遇到了不少以前在学习 ...

  3. 数据分析 - 斯特鲁普效应(Stroop effect)

    数据分析 - 斯特鲁普效应(Stroop effect) Reinhard得到了一份斯特鲁普效应试验的数据,我们来分析下,文字的颜色,是否会影响受试者的反应. 这里先看看什么是斯特鲁普效应: 斯特鲁普 ...

  4. A ship is always safe at the shore - but that is not what it is built for.

    A ship is always safe at the shore - but that is not what it is built for. 船靠岸边总是安全的,但那不是建造它的目的.

  5. ImportError: cannot import name 'check_arrays'

    from sklearn.utils.validation import check_arrays 执行 from sklearn.utils.validation import check_arra ...

  6. 立即执行函数: (function(){...})() 与 (function(){...}()) 有什么区别?

    没有区别. function foo() {...} // 这是定义,Declaration:定义只是让解释器知道其存在,但是不会运行. foo(); // 这是语句,Statement:解释器遇到语 ...

  7. django之分页、cookie装饰器

    一.分页代码如下 from django.utils.safestring import mark_safe class Page: def __init__(self, current_page, ...

  8. ExecutorService线程池应用

    //线程数量 int threadNum = lists.size(); //创建一个线程池 ExecutorService pool = Executors.newFixedThreadPool(t ...

  9. [转载]LazyWriter(惰性写入器) 进程的作用

      Q:What Does the LazyWriter Process Do? The LazyWriter process is a periodic process that checks th ...

  10. Python3基础 访问列表 两个索引值之间的所有元素

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...