java(List或Array数组)求交集、并集、差集, 泛型工具类
业务需要求不同类型的交集、并集、差集为避免代码冗余编写工具类。
注:list 转数组需传入数组,如果将原数组传入将会改变原数组的值,同时泛型数组又不可以实例化,解决方案:Arrays.copyOf(n,list.size()) ,使用copyOf功能,开辟返回集合的等长新数组,避免修改原数组。
public static <T>T[] getIntersection(T[] n,T[] m){
List<T> list= MathUtils.getIntersection(Arrays.asList(n),Arrays.asList(m));
return list.toArray(Arrays.copyOf(n,list.size()));
}
如下为工具类详细代码!!!!
package com.lock.demo.common; import java.util.*; /**
* @author niunafei
* @function
* @email niunafei0315@163.com
* @date 2018/12/21 下午2:24
*/
public class MathUtils { /**
* list 求差集
* @param n
* @param m
* @param <T>
* @return
*/
public static <T>List getDifferenceSet(List<T> n,List<T> m){
//转化最长列表
Set<T> set=new HashSet<>(n.size()>m.size()?n:m);
//循环最短列表
for (T t:n.size()>m.size()?m:n) {
if(set.contains(t)){
set.remove(t);
}else {
set.add(t);
}
}
return new ArrayList(set);
}
/**
* list 求交集
* @param n
* @param m
* @param <T>
* @return
*/
public static <T>List getIntersection(List<T> n,List<T> m){
Set<T> setN= new HashSet<>(n);
Set<T> setM=new HashSet<>(m);
setN.retainAll(setM);
return new ArrayList(setN);
} /**
* list 集合并集
* @param n
* @param m
* @param <T>
* @return
*/
public static <T>List getUnion(List<T> n,List<T> m){
Set<T> setN= new HashSet<>(n);
Set<T> setM=new HashSet<>(m);
setN.addAll(setM);
return new ArrayList(setN);
} /**
* 数组求差集
* @param n
* @param m
* @param <T>
* @return
*/
public static <T>T[] getDifferenceSet(T[] n,T[] m){
List<T> list= MathUtils.getDifferenceSet(Arrays.asList(n),Arrays.asList(m));
return list.toArray(Arrays.copyOf(n,list.size()));
}
/**
* 数组求交集
* @param n
* @param m
* @param <T>
* @return
*/
public static <T>T[] getIntersection(T[] n,T[] m){
List<T> list= MathUtils.getIntersection(Arrays.asList(n),Arrays.asList(m));
return list.toArray(Arrays.copyOf(n,list.size()));
}
/**
* 数组并集
* @param n
* @param m
* @param <T>
* @return
*/
public static <T>T[] getUnion(T[] n,T[] m){
List<T> list=MathUtils.getUnion(Arrays.asList(n),Arrays.asList(m));
return list.toArray(Arrays.copyOf(n,list.size()));
} public static void main(String[] args){
List<Integer> list=new ArrayList<>(Arrays.asList(1,2,3,4));
List<Integer> list1=new ArrayList<>(Arrays.asList(3,4,5,6));
System.out.println("list 差集"+getDifferenceSet(list,list1));
System.out.println("list 并集"+getUnion(list,list1));
System.out.println("list 交集"+getIntersection(list,list1));
Integer[] array=new Integer[]{1,2,3,4};
Integer[] array1=new Integer[]{3,4,5,6};
//差集[1, 2, 5, 6]
System.out.println("array 差集"+Arrays.toString(getDifferenceSet(array,array1)));
//并集[1, 2, 3, 4, 5, 6]
System.out.println("array 并集"+Arrays.toString(getUnion(array,array1)));
//交集[3, 4]
System.out.println("array 交集"+Arrays.toString(getIntersection(array,array1))); } }
https://www.aliyun.com/acts/product-section-2019/new-users?userCode=q3tq2yrp
java(List或Array数组)求交集、并集、差集, 泛型工具类的更多相关文章
- js求对象数组的交集/并集/差集/去重
1.求交集 var arr1 = [{name:'name1',id:1},{name:'name2',id:2},{name:'name3',id:3}]; var arr1Id = [1,2,3] ...
- js求两个数组的交集|并集|差集|去重
let a = [1,2,3], b= [2, 4, 5]; 1.差集 (a-b 差集:属于a但不属于b的集合) a-b = [1,3] (b-a 差集:属于b但不属于a的集合) b-a = [4 ...
- SQL求 交集 并集 差集
故事是这样的….. 故事情节: 表 tb_test 有两列, colA , colB; 求 colA , colB 的并交差集… -- 计算并集 SELECT DISTINCT colB FROM t ...
- LINQ操作数组(交集,并集,差集,最值,平均,去重复)
数组是大学里经常拿来做算法练习的对象.一些经典算法非常有价值,考试.装逼.面试都十分有用.但现在是效率时代,编程讲究生产效率,利用LINQ,可以让程序猿避免写一些基本算法,把精力花在业务处理上. 下面 ...
- python [] 数组 list 交集 并集 差集
>>> a = [1,2,3] >>> b = [2,4,5] >>> list(set(a).intersection(set(b))) [2] ...
- stl set求交集 并集 差集
#include <iostream>#include <set> using namespace std; typedef struct tagStudentInfo{ i ...
- 如何求ArrayList集合的交集 并集 差集 去重复并集
需要用到List接口中定义的几个方法: addAll(Collection<? extends E> c) :按指定集合的Iterator返回的顺序将指定集合中的所有元素追加到此列表的末尾 ...
- java用最少循环求两个数组的交集、差集、并集
import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List ...
- C# 数组的交集、差集、并集
C# 数组的交集.差集.并集 工作中经常会用这方面的知识来检查那些字段是必须输入的,那些是禁止输入. using System; using System.Collections.Generic; u ...
随机推荐
- 百度编辑器ueditor插件的基本使用
注意:该插件是基于tpframe开发,请在tpframe框架上使用 插件下载地址:https://pan.baidu.com/s/1MOJbd1goQC0Bn5-7HcIdKA 插件下载下来后解压到a ...
- 黑客攻防技术宝典web实战篇:攻击本地编译型应用程序习题
猫宁!!! 参考链接:http://www.ituring.com.cn/book/885 随书答案. 1. 如果不采用特殊的防御措施,为什么栈缓冲区溢出比堆溢出更容易被攻击者利用? 利用基于栈的溢出 ...
- the little schemer 笔记(10)
第十章 What Is the Value of All of This? entry条目 是由list表组成的 pair 对,pair 对的第一个list表是集合 set.另外,两个list表的长 ...
- python实现汉诺塔(递归)
def hanoi(n, A, B, C): if n > 0: hanoi(n-1, A, C, B) print("%s->%s" % (A, C)) hanoi( ...
- canvas绘图出现模糊,解决方法
在项目开发中发现,canvas有一个问题,绘制的图会出现模糊现象. 解决方法之一:将canvas元素放大2倍,然后将整个canvas元素或者其父元素缩小两倍. <!DOCTYPE html> ...
- STM32HAL库学习之前言
HAL库:HAL 的全称是: Hardware Abstraction Layer (硬件抽象层) ,是ST最新推荐的库.包括基本库和扩展库(功能外展):三种编程模型(轮询.中断和 DMA) 灵活的回 ...
- HDU 4565 So Easy! 数学 + 矩阵 + 整体思路化简
http://acm.hdu.edu.cn/showproblem.php?pid=4565 首先知道里面那个东西,是肯定有小数的,就是说小数部分是约不走的,(因为b限定了不是一个完全平方数). 因为 ...
- Android如何用阿里云的API进行身份证识别
准备工作:在libs下添加 alicloud-Android-apigateway-sdk-1.0.1.jar,commons-codec-1.10-1.jar 在build.gradle添加 co ...
- Debian9镜像安装问题
Debian9下载地址 https://www.debian.org/distrib/ Debian9有三个镜像文件 第一个包含系统2.3两个主要是一些软件的安装包只需下载第一个安装系统即可 默认安装 ...
- postgres的强制类型转换与时间函数
一.类型转换postgres的类型转换:通常::用来做类型转换,timestamp到date用的比较多select now()::dateselect now()::varchar 示例1:日期的 ...