java List间计算并集、差集、交集以及去重 原创 2021-05-14 09:39:42.0 阅读(3832)次 Java中如何计算两个List的并集、差集、交集? 并集和交集我们都很清楚,但其中这里说的差集是什么意思呢?指的是集合listA与listB差集,准确来说是用listB去删除listA中存在listB中的元素。 Java集合中针对这三个场景提供了三个方法。 - 并集使用list.addAll()方法; - 差集使用list.removeAll()方法; - 交集使用list.retainAll()方法; 其中removeAll和retainAll都会根据对象的equals方法来比对对象是否相等,要正确的使用这两个方法需要注意对象的类是否要重写equals和hashCode方法。 下面分享一下如何实现两个List的并集、差集、交集,包括List的去重 具体代码如下: ListJoinAndDiffAndInter.java ```java import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import com.alibaba.fastjson.JSON; import com.mystudy.model.UserInfo; /** * 两个List的并集、差集、交集计算,去重方法 * 差集可以用来去重 对象的比对需要重写equals和hashCode方法 * @author Administrator * */ public class ListJoinAndDiffAndInter { /** * 求两个列表的并集 */ public static void getListJoin() { List listA = new ArrayList(); List listB = new ArrayList(); UserInfo a = new UserInfo(1, "a", null); UserInfo b = new UserInfo(2, "b", null); UserInfo c = new UserInfo(3, "c", null); UserInfo d = new UserInfo(4, "d", null); listA.add(a); listA.add(b); listA.add(c); listB.add(b); listB.add(c); listB.add(d); listA.addAll(listB); //去重 List list = removeDupli(listA); System.out.println(JSON.toJSONString(list)); } /** * 求两个列表的差集,差集结果以listA为标准,可用来两个集合的去重 */ public static void getListDiff() { List listA = new ArrayList(); List listB = new ArrayList(); UserInfo a = new UserInfo(1, "a", null); UserInfo b = new UserInfo(2, "b", null); UserInfo c = new UserInfo(3, "c", null); UserInfo d = new UserInfo(4, "d", null); listA.add(a); listA.add(b); listA.add(c); listB.add(b); listB.add(c); listB.add(d); // removeAll需要UserInfo类重写equals和hashCode方法 listA.removeAll(listB); System.out.println(JSON.toJSONString(listA)); } /** * 求两个列表的交集 */ public static void getListInter() { List listA = new ArrayList(); List listB = new ArrayList(); UserInfo a = new UserInfo(1, "a", null); UserInfo b = new UserInfo(2, "b", null); UserInfo c = new UserInfo(3, "c", null); UserInfo d = new UserInfo(4, "d", null); listA.add(a); listA.add(b); listA.add(c); listB.add(b); listB.add(c); listB.add(d); // retainAll需要UserInfo类重写equals和hashCode方法 listA.retainAll(listB); System.out.println(JSON.toJSONString(listA)); } /** * 去重 * @param listA * @return */ public static List removeDupli(List listA) { // 去重,distinct需要UserInfo类重写equals和hashCode方法 List list = listA.stream().distinct().collect(Collectors.toList()); return list; } public static void main(String[] args) { // 并集 getListJoin(); // 差集 getListDiff(); // 交集 getListInter(); } } ``` UserInfo.java ```java @Data public class UserInfo { private Integer id; private String userName; private Integer deptId; public UserInfo() { } public UserInfo(Integer id, String userName, Integer deptId) { super(); this.id = id; this.userName = userName; this.deptId = deptId; } @Override public boolean equals(Object obj) { if (obj instanceof UserInfo) { UserInfo tmp = (UserInfo) obj; if (this.getId().equals(tmp.getId())) { return true; } } return false; } @Override public int hashCode() { return this.getId().hashCode(); } } ``` 执行结果如下: ``` 并集:[{"id":1,"userName":"a"},{"id":2,"userName":"b"},{"id":3,"userName":"c"},{"id":4,"userName":"d"}] 差集:[{"id":1,"userName":"a"}] 交集:[{"id":2,"userName":"b"},{"id":3,"userName":"c"}] ``` java java基础 上一篇:LinkedBlockingQueue实现定长队列自动出队与读取队列 下一篇:java Md5工具类获取字符串的Md5值
相关文章 java使用BufferedImage放大或缩小图片(4643) java Md5工具类获取字符串的Md5值(1265) 图片的base64字符转成BufferedImage(2665) java使用guava cache实现本地缓存(4854) java List数据内存分页(3084) java阻塞的线程安全的优先级队列PriorityBlockingQueue(1969) java Map通过遍历器Iterator来实现遍历过程中删除元素来避免ConcurrentModificationException异常(1136) java程序员多线程常见的代码bug(1657) 如何缩小jre1.8(1467) java DelayQueue延时队列的应用(1175) 推荐文章 使用spring4实现websocket连接(1) Parameter index out of range (1 > number of parameters, which is 0(7) spring cloud+feign+mybatis中使用seata0.9实现分布式事务(7) spring cloud gateway报错Only one connection receive subscriber allowed(82) spring cloud中Feign调用诡异报错MethodNotAllowed: status 405 reading(116) elasticsearch7.1保存时报错: Validation Failed: 1: type is missing;(7) 聊聊数据保存到MySQL后数据乱码的问题(1) jquery对象与dom对象互转(1) linux使用epel源yum安装iftop、nload、nginx等(2) linux下nginx安装其他模块(1) 热门文章 java stream去重的几种方式(40819) the dependencies of some of the beans in the application context form a cycle(16324) 解决mybatis打印查询结果集造成太多日志的问题(9839) java enum枚举转list和Map(9722) java stream List转Map与List转List与Map转List以及List转Map(8468) ServletRequest转HttpServletRequest设置header之后取不到header的问题(8434) java中BufferedImage转成 base64字符串(8098) 切分List集合为多个List集合(7385) maven修改jar包版本不生效解决办法(6990) bootstrap.yml配置报错:Could not resolve placeholder 'xx' in value (6949) 标签列表 java java java java java java java基础 微服务 异常处理 mysql clickhouse clickhouse clickhouse clickhouse clickhouse spring cloud spring boot linux elasticsearch feign jdbc spring js docker postgresql solr seata nginx maven gateway hsqldb 数据库 架构 大数据分析 分布式事务 redis canal dubbo hadoop 消息队列 win10 websocket springmvc git html select2 mybatis jenkins rocketmq quartz activemq 数据库集群 ajax bat 电脑 笔记 eclipse 设计模式 阿里云 github freemarker jvm jquery javamail redission redission对象 hystrix http hibernate springmail svn ubuntu ueditor xheditor zookeeper 分布式 小程序 开发工具 gitlab