Java 集合运算(并集、交集与差集)

本文介绍了Java中集合的并集、交集和差集操作,强调了在进行集合运算时需要注意元素的唯一性。通过实例展示了如何利用HashSet或TreeSet确保运算结果无重复元素,并提供了蓝桥杯相关算法题链接,以加深理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、用法

[List obj1].retainAll([List obj2]);    //交集
[List obj1].addAll([List obj2]);    //并集
[List obj1].removeAll([List obj2]);    //差集,obj1-obj2 
  • 集合运算的返回值是一个集合
  • 集合运算时一般先把目标集合拷贝备份,不然运算后集合元素将被更改,如:
private List union(List l1,List l2) {
		List list =new ArrayList(Arrays.asList(new Object[l1.size()]));
		Collections.copy(list, l1); //将l1拷贝到list中
		list.addAll(l2);
		
		return list;
		
	}
  • 在并集运算中,使用 [List obj].addAll([List obj])的方法得到的结果是两个集合中的所有元素,有可能有重复的元素,跟数学上意义上的并集有所差别 。欲求数学意义上的并集则需进行转化(因此,一般将元素存储到HashSet或TreeSet中,能保证集合运算结果中元素的唯一性),例如:
		/*实际的集合运算结果*/
		//print the union of the two list
		System.out.println("\n");
		System.out.println("Union: ");
		
		list=opt.union(l1, l2);
		for(i=0;i<list.size();i++) {
			System.out.print(list.get(i)+" ");
		}
		
		/*运算结果
			Union: 
			1 2 3 4 6 8 2 4 7 9
		*/
  • 转换成数学意义上的并集结果
		/*转换后变成数学意义上的运算结果*/
		//print the union of the two list
		System.out.println("\n");
		System.out.println("Union: ");
		
		list=opt.union(l1, l2);
		for(i=0;i<list.size();i++) {
			System.out.print(list.get(i)+" ");
		}
		
		//The real union of the two list....
		System.out.println("\n");
		HashSet unionlist=new HashSet();
		for(i=0;i<list.size();i++) {
			unionlist.add(list.get(i));
		}
				
		System.out.println("The real union of the two list is : ");
		Iterator ite=unionlist.iterator();
		while(ite.hasNext()) {
			System.out.print(ite.next()+" ");
		}
				
	/*
	Union: 
	1 2 3 4 6 8 2 4 7 9 

	The real union of the two list is : 
	1 2 3 4 6 7 8 9 
	*/

说明:

  • split() 方法根据匹配给定的正则表达式来拆分字符串。
  • . |* 等转义字符,必须得加 \ ;
  • 多个分隔符,可以用 | 作为连字符 ;

 

2、集合运算实例:

import java.util.*;
public class setOpt {

	//The operation of Intersect
	private List intersetct(List l1,List l2) {
		List list=new ArrayList(Arrays.asList(new Object[l1.size()]));
		Collections.copy(list, l1);
		list.retainAll(l2);
		
		return list;
	}
	
	//The operation of Union
	private List union(List l1,List l2) {
		List list =new ArrayList(Arrays.asList(new Object[l1.size()]));
		Collections.copy(list, l1);
		list.addAll(l2);
		
		return list;
		
	}
	
	//The operation of Diff
	private List diff(List l1,List l2) {
		List list =new ArrayList(Arrays.asList(new Object[l1.size()]));
		Collections.copy(list, l1);
		list.removeAll(l2);
		
		return list;
	}
	
	public static void main(String[] args) {

		String str1[]="1;2;3;4;6;8".split(";");
		String str2[]="2;4;7;9".split(";");
		int i,j;
		
		setOpt opt=new setOpt();
		
		List l1=new ArrayList();
		List l2=new ArrayList();
		List list;
		
		for(i=0;i<str1.length;i++) {
			l1.add(str1[i]);
		}
		
		for(j=0;j<str2.length;j++) {
			l2.add(str2[j]);
		}
		
		//print the intersection of the two list
		System.out.println("Intersection: ");
		list=opt.intersetct(l1, l2);
		for(i=0;i<list.size();i++) {
			System.out.print(list.get(i)+" ");
		}
		
		//print the union of the two list
		System.out.println("\n");
		System.out.println("Union: ");
		
		list=opt.union(l1, l2);
		for(i=0;i<list.size();i++) {
			System.out.print(list.get(i)+" ");
		}
		
				//The real union of the two list....
				System.out.println("\n");
				HashSet unionlist=new HashSet();
				for(i=0;i<list.size();i++) {
					unionlist.add(list.get(i));
				}
				
				System.out.println("The real union of the two list is : ");
				Iterator ite=unionlist.iterator();
				while(ite.hasNext()) {
					System.out.print(ite.next()+" ");
				}
				
				
		//print the diff of the two list
		System.out.println("\n");
		System.out.println("Diff: ");
		list=opt.diff(l1, l2);
		for(i=0;i<list.size();i++) {
			System.out.print(list.get(i)+" ");
		}
		
		

	}

}

/*运行结果:
Intersection: 
2 4 

Union: 
1 2 3 4 6 8 2 4 7 9 

The real union of the two list is : 
1 2 3 4 6 7 8 9 

Diff: 
1 3 6 8 
*/

附:
蓝桥杯集合运算算法题(TreeSet存储元素的唯一性求解法):
https://blog.youkuaiyun.com/CR_fun/article/details/79294541

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值