两个list的并集,只需去除重复元素即可;

将两个list放入同一个set中即可;

两个list的交集:

1将其中一个list放入set,

2循环另一个list,每次向set塞值,

3判断set的总数是否变化,如果不变,该值就是交集的一员;

    static void getIntersection() {
List<Long> r1 = new ArrayList<>();
r1.add(1L);
r1.add(2L);
r1.add(3L);
r1.add(4L);
r1.add(5L);
System.out.println("M" + r1);
List<Long> r2 = new ArrayList<>();
r2.add(11L);
r2.add(12L);
r2.add(13L);
r2.add(4L);
r2.add(5L);
Set<Long> hashSet = new HashSet<>(r1);
System.out.println("N" + r2); List<Long> r3 = new ArrayList<>();
int count = hashSet.size();
for (int i = 0; i < r2.size(); i++) {
hashSet.add(r2.get(i));
if (hashSet.size() == count) {
r3.add(r2.get(i));
} else {
count++;
}
}
System.out.println("交集" + r3);
}

求两个list的交集和并集的更多相关文章

  1. 求两个集合的交集和并集C#

    我是用hashset<T>来实现的 具体如代码所示 using System; using System.Collections.Generic; using System.Linq; u ...

  2. java求两个集合的交集和并集,比较器

    求连个集合的交集: import java.util.ArrayList; import java.util.List; public class TestCollection { public st ...

  3. java用最少循环求两个数组的交集、差集、并集

    import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List ...

  4. Python3.7.1学习(三)求两个list的差集、并集与交集

    在python3.7.1对列表的处理中,会经常使用到Python求两个list的差集.交集与并集的方法. 下面就以实例形式对此加以分析. # 求两个list的差集.并集与交集# 一.两个list差集# ...

  5. js取两个数组的交集|差集|并集|补集|去重示例代码

    http://www.jb51.net/article/40385.htm 代码如下: /** * each是一个集合迭代函数,它接受一个函数作为参数和一组可选的参数 * 这个迭代函数依次将集合的每一 ...

  6. java使用bitmap求两个数组的交集

    一般来说int代表一个数字,但是如果利用每一个位 ,则可以表示32个数字 ,在数据量极大的情况下可以显著的减轻内存的负担.我们就以int为例构造一个bitmap,并使用其来解决一个简单的问题:求两个数 ...

  7. python两个 list 交集,并集,差集的方法+两个tuple比较操作+两个set的交集,并集,差集操作+两个dict的比较操作

    转自:http://blog.chinaunix.net/uid-200142-id-3992553.html 有时候,为了需求,需要统计两个 list 之间的交集,并集,差集.查询了一些资料,现在总 ...

  8. python 两个 list 获取交集,并集,差集的函数

    1. 获取两个 list 的交集 a = [1, 2, 3, 4] b = [1, 2, 5] print(list(set(a).intersection(set(b)))) 2. 获取两个 lis ...

  9. python两个 list 获取交集,并集,差集的方法

    1. 获取两个list 的交集 #方法一: a=[2,3,4,5] b=[2,5,8] tmp = [val for val in a if val in b] print tmp #[2, 5] # ...

随机推荐

  1. 【HDU 6008】Worried School(模拟)

    Problem Description You may already know that how the World Finals slots are distributed in EC sub-r ...

  2. 3.3.2 使用 cut 选定字段

        cut 命令是用来剪下文本文件里的数据,文本文件可以是字段类型或是字符类型.后一种数据类型在遇到需要从文件里剪下特定的列时,特别方便.请注意:一个制表字符在此被视为单个字符.          ...

  3. 如何将表的行数赋值给变量(MySQL)

    delimiter $$ drop procedure if exists test_at $$ create definer=root@localhost procedure test_at() b ...

  4. Apollo配置中心的使用

    1. 自己搭建Apollo配置中心 碰到如下错误: nested exception is org.hibernate.HibernateException: Access to DialectRes ...

  5. ORACLE-023:令人烦恼的 ora-01722 无效数字

    https://blog.csdn.net/yysyangyangyangshan/article/details/51762746

  6. python016 Python3 数据结构

    Python3 数据结构本章节我们主要结合前面所学的知识点来介绍Python数据结构. 列表Python中列表是可变的,这是它区别于字符串和元组的最重要的特点,一句话概括即:列表可以修改,而字符串和元 ...

  7. N个数求和(模拟)

    本题的要求很简单,就是求N个数字的和.麻烦的是,这些数字是以有理数分子/分母的形式给出的,你输出的和也必须是有理数的形式. 输入格式: 输入第一行给出一个正整数N(≤100).随后一行按格式a1/b1 ...

  8. 79. could not initialize proxy - no Session 【从零开始学Spring Boot】

    [原创文章,转载请注明出处] Spring与JPA结合时,如何解决懒加载no session or session was closed!!! 实际上Spring Boot是默认是打开支持sessio ...

  9. 服务器安装oracle前的内存调整

    #当前内存大小为512MB,安装oracle时执行检查... Checking physical memory requirements ... Expected result: 922MB Actu ...

  10. MacOS & iOS

    MacOS & iOS https://github.com/qinjx/30min_guides/blob/master/ios.md https://www.cnblogs.com/xgq ...