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 ...
随机推荐
- 2017.11.7~8模拟测试总结---暨NOIP2017考前对策
最后两天了,第三天就是NOIP2017--Day1了. 刚刚考完了这个学期从开学以来的最后一场模拟赛了.首先要对于这场模拟赛做一次深刻的反思. 考完才猛地惊叹这是最后一场模拟赛了,然而题目并不难,也保 ...
- 第二篇 .NET高级技术之密闭类和静态类及扩展方法
1.密闭类是修饰为sealed的类, sealed不能有子类.一般只有系统中的一些基本类声明为sealed.面试题:是否可以编写一个类继承自String类? 答:不能,因为string被声明为了sea ...
- php 获得上周数据
$lastMondy = date('Y-m-d', strtotime('-2 sunday +1 days', time()));$lastSundy = date('Y-m-d', strtot ...
- Cloudera Manager是啥?主要是干啥的?
简单来说,Cloudera Manager是一个拥有集群自动化安装.中心化管理.集群监控.报警功能的一个工具(软件),使得安装集群从几天的时间缩短在几个小时内,运维人员从数十人降低到几人以内,极大的提 ...
- Qt样式表之三:实现按钮三态效果的三种方法
按钮的三态,指的是普通态.鼠标的悬停态.按下态.Qt中如果使用的是默认按钮,三态的效果是有的,鼠标放上去会变色,点击的时候有凹陷的效果. 但是如果自定义按钮实现三态效果有三种方法,一种是设置背景图,主 ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) D
Description A tree is an undirected connected graph without cycles. The distance between two vertice ...
- 洛谷 P3960 列队
https://www.luogu.org/problemnew/show/P3960 常数超大的treap #pragma GCC optimize("Ofast") #incl ...
- Android逆向分析工具表
逆向分析工具表 工具 描述 网址 androidterm Android Terminal Emulator http://code.google.com/p/androidterm/ droidbo ...
- h5-19-文件操作-文件域
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 在线编译器Coding Ground
http://www.tutorialspoint.com/codingground.htm Free Online IDE and Terminal - Edit, Compile, Execute ...