java小技巧(二):JAVA 交集,差集,并集
“java常见小错误”系列文章推荐:
上一篇: java小技巧(一):进制转换
前文推荐:java常见小错误(一):变量类型自动转换与强制转换
【版权申明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权);
本博客的内容来自于java小技巧(二):JAVA 交集,差集,并集;
学习、合作与交流联系q384660495;
本博客的内容仅供学习与参考,并非营利;
前言
笔试的时候突然想到一个问题,如何求集合的交并差集,然后就有了这一篇文章。希望对每一位有所帮助!
一、HashMap、HashSet优雅的初始化方式
Map<String, Object> map = new HashMap<String, Object>() {
{
put("name", "June");
put("age", 12);
}
};
Set<Integer> set2 = new HashSet<Integer>(){
{
add(1);
add(2);
add(3);
}
};
其实,外层的一组“{}”表示的是一个匿名类,内层的一对“{}”表示的是实例初始化块,然后这边还有一点需要明白,实例初始化块的代码在编译器编译过后,是放在类的构造函数里面的,并且是在原构造函数代码的前面。
怎么理解这个呢?其实大概就是这样:
class People{
public void say(){
System.out.println("people.say");
}
}
new People(){
{
say();
}
};
这段代码其实就是等价于:
class People{
public void say(){
System.out.println("people.say");
}
{
say();
}
}
new People();
这样应该就能理解了吧。
二、交集、并集、差集
1、实验数据准备
Set<Integer> set1 = new HashSet<Integer>(){{
add(1);
add(3);
add(5);
}};
Set<Integer> set2 = new HashSet<Integer>(){{
add(1);
add(2);
add(3);
}};
2、实验结果展示
并集:
Set<Integer> set = new HashSet<>();
set.addAll(set1);
set.addAll(set2);
System.out.println("并集 = " + set);
//并集 = [1, 2, 3, 5]
交集:
Set<Integer> set = new HashSet<>();
set.addAll(set1);
set.retainAll(set2);
System.out.println("交集 = " + set);
//交集 = [1, 3]
差集:
Set<Integer> set = new HashSet<>();
set.addAll(set1);
set.removeAll(set2);
System.out.println("set1 - set2 的差集 = " + set);
//set1 - set2 的差集 = [5]
其实list也可以达到同样的效果,具体可参考采用java8 lambda表达式 实现 java list 交集 并集 差集 去重复并集
public static void main(String[] args) {
List<String> list1 = new ArrayList<String>();
list1.add("1");
list1.add("2");
list1.add("3");
list1.add("5");
list1.add("6");
List<String> list2 = new ArrayList<String>();
list2.add("2");
list2.add("3");
list2.add("7");
list2.add("8");
// 交集
List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
System.out.println("---交集 intersection---");
intersection.parallelStream().forEach(System.out :: println);
// 差集 (list1 - list2)
List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
System.out.println("---差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out :: println);
// 差集 (list2 - list1)
List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
System.out.println("---差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out :: println);
// 并集
List<String> listAll = list1.parallelStream().collect(toList());
List<String> listAll2 = list2.parallelStream().collect(toList());
listAll.addAll(listAll2);
System.out.println("---并集 listAll---");
listAll.parallelStream().forEachOrdered(System.out :: println);
// 去重并集
List<String> listAllDistinct = listAll.stream().distinct().collect(toList());
System.out.println("---得到去重并集 listAllDistinct---");
listAllDistinct.parallelStream().forEachOrdered(System.out :: println);
System.out.println("---原来的List1---");
list1.parallelStream().forEachOrdered(System.out :: println);
System.out.println("---原来的List2---");
list2.parallelStream().forEachOrdered(System.out :: println);
}
还有简单一点的做法。
//有重复并集
list1.addAll(list2);
System.out.println(list1);
// 去重复并集
list2.removeAll(list1);
list1.addAll(list2);
System.out.println(list1);
// 交集
list1.retainAll(list2);
System.out.println(list1);
// 差集
list1.removeAll(list2);
System.out.println(list1);
总结
本篇文章总结了一些对集合的基本操作,其实大体就几种API,希望对读者有所帮助!