[Commons]——集合操作

本文通过一个Java示例展示了如何使用Commons Lang库中的CollectionUtils类来操作集合,包括求并集、交集和差集的方法。示例中创建了两个整数集合,并演示了这些集合操作。

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

public class test4 {//Commons集合操作
	public static void main(String[] args) {
		Set<Integer> set1=new HashSet<Integer>();
		set1.add(1);
		set1.add(2);
		set1.add(3);
		set1.add(4);
		Set<Integer> set2=new HashSet<Integer>();
		set2.add(3);
		set2.add(4);
		set2.add(5);
		set2.add(6);
		//并集
		Collection<Integer> col=CollectionUtils.union(set1, set2);
		for(Integer temp:col){
			System.out.print(temp+"  ");
		}
		System.out.println();
		//交集
		col=CollectionUtils.intersection(set1, set2);
		//col=CollectionUtils.retainAll(set1, set2);
		for(Integer temp:col){
			System.out.print(temp+"  ");
		}
		System.out.println();
		//差集
		col=CollectionUtils.subtract(set2, set1);
		for(Integer temp:col){
			System.out.print(temp+"  ");
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值