java两个List的交集,并集

java两个List的交集,并集
⽅法⼀:使⽤apache的CollectionUtils⼯具类(推荐)
public static void main(String[] args){
String[] arrayA =new String[]{"1","2","3","4"};
String[] arrayB =new String[]{"3","4","5","6"};
List<String> listA = Arrays.asList(arrayA);
List<String> listB = Arrays.asList(arrayB);
//1、并集 union
System.out.println(CollectionUtils.union(listA, listB));
//输出: [1, 2, 3, 4, 5, 6]
//2、交集 intersection
System.out.println(CollectionUtils.intersection(listA, listB));
//输出:[3, 4]
//3、交集的补集(析取)disjunction
System.out.println(CollectionUtils.disjunction(listA, listB));
//输出:[1, 2, 5, 6]
//4、差集(扣除)
System.out.println(CollectionUtils.subtract(listA, listB));
//输出:[1, 2]
}
⽅法⼆:List⾃带⽅法
public static void main(String[] args){
String[] arrayA =new String[]{"1","2","3","4"};
String[] arrayB =new String[]{"3","4","5","6"};
List<String> listA = Arrays.asList(arrayA);
List<String> listB = Arrays.asList(arrayB);连云港黄海机械厂
//1、交集
List<String>  jiaoList =new ArrayList<>(listA);
System.out.println(jiaoList);
//输出:[3, 4]
//2、差集
List<String>  chaList =new ArrayList<>(listA);
System.out.println(chaList);
//输出:[1, 2]
//3、并集 (先做差集再做添加所有)
List<String>  bingList =new ArrayList<>(listA);
bingList.addAll(listB);//添加[3,4,5,6]
System.out.println(bingList);
//输出:[1, 2, 3, 4, 5, 6]
}
注意 : intersection和retainAll的差别
要注意的是它们的返回类型是不⼀样的,intersection返回的是⼀个新的List集合,⽽retainAll返回是Bollean类型那就说明retainAll⽅法是对原有集合进⾏处理再返回原有集合,会改变原有集合中的内容。
个⼈观点:1、从性能⾓度来考虑的话,List⾃带会⾼点,因为它不⽤再创建新的集合。2、需要注意的是:因为retainAll因为会改变原有集合,所以该集合需要多次使⽤就不适合⽤retainAll。
注意: Arrays.asList将数组转集合不能进⾏add和remove操作。
原因:调⽤Arrays.asList()⽣产的List的add、remove⽅法时报异常,这是由Arrays.asList() 返回的市Arrays的内部类ArrayList, ⽽不是java.util.ArrayList。Arrays的内部类ArrayList和java.util.ArrayList都是继承AbstractList,remove、add等⽅法AbstractList中是默认throw UnsupportedOperationException⽽且不作任何操作。java.util.ArrayList重新了这些⽅法⽽Arrays的内部类ArrayList没有重新,所以会抛出异常。
所以正确做法如下
String[] array ={"1","2","3","4","5"};
List<String> list = Arrays.asList(array);
List arrList =new ArrayList(list);
arrList.add("6");
⽅法三:JDK1.8 stream 新特性
String[] arrayA =new String[]{"1","2","3","4"};
String[] arrayB =new String[]{"3","4","5","6"};
List<String> listA = Arrays.asList(arrayA);
List<String> listB = Arrays.asList(arrayB);
// 交集
List<String> intersection = listA.stream().filter(item -> ains(item)).collect(toList());
System.out.println(intersection);
//输出:[3, 4]
/
/ 差集 (list1 - list2)
List<String> reduceList = listA.stream().filter(item ->!ains(item)).collect(toList());
System.out.println(reduceList);
//输出:[1, 2]
// 并集(新建集合:1、是因为不影响原始集合。2、Arrays.asList不能add和remove操作。
List<String> listAll = listA.parallelStream().collect(toList());
List<String> listAll2 = listB.parallelStream().collect(toList());
listAll.addAll(listAll2);
System.out.println(listAll);
//输出:[1, 2, 3, 4, 3, 4, 5, 6]
// 去重并集
List<String> list =new ArrayList<>(listA);
list.addAll(listB);
List<String> listAllDistinct = list.stream().distinct().collect(toList());
System.out.println(listAllDistinct);
//输出:[1, 2, 3, 4, 5, 6]
总结 : 这三种推荐第⼀种⽅式,因为第⼆种还需要确定该集合是否被多次调⽤。第三种可读性不⾼。
对象集合交、并、差处理
因为对象的equels⽐较是⽐较两个对象的内存地址,所以除⾮是同⼀对象,否则equel返回永远是false。
但我们实际开发中 在我们的业务系统中判断对象时有时候需要的不是⼀种严格意义上的相等,⽽是⼀种业务上的对象相等。在这种情况下,原⽣的equals⽅法就不能满⾜我们的需求了,所以这个时候我们需要重写equals⽅法。
说明 :String为什么可以使⽤equels⽅法为什么只要字符串相等就为true,那是因为String类重写了equal和hashCode⽅法,⽐较的是值。
public class Person {
邢源高private String name;
private Integer age;
public Person(String name, Integer age){
this.name = name;
this.age = age;
}
/**
* 为什么重写equals⽅法⼀定要重写hashCode⽅法下⾯也会讲
*/
甘肃农业大学学报
@Override
public int hashCode(){
String result = name + age;财权
return result.hashCode();
}
/**
* 重写 equals ⽅法根据name和age都相同那么对象就默认相同
*/
@Override
public boolean equals(Object obj){
Person u =(Person) obj;
Name().Name())&&(this.age.Age())); }
/**
* 重写 toString ⽅法
*/
@Override
public String toString(){
return"Person{"+
"name='"+ name +'\''+
", age="+ age +
'}';
}
}
这⾥根据name和age都相同那么就默认相同对象。
public static void main(String[] args){
List<Person> personList = wArrayList();
Person person1 =new Person("⼩⼩",3);
Person person2 =new Person("中中",4);
ppp13personList.add(person1);
莱维特personList.add(person2);
List<Person> person1List = wArrayList();
Person person3 =new Person("中中",4);
Person person4 =new Person("⼤⼤",5);
person1List.add(person3);
person1List.add(person4);
/**
* 1、差集
*/
System.out.println(CollectionUtils.subtract(personList, person1List));
//输出:[Person{name='⼩⼩', age=3}]
/**
* 2、并集
*/
System.out.println(CollectionUtils.union(personList, person1List));
/
/输出:[Person{name='⼩⼩', age=3}, Person{name='中中', age=4}, Person{name='⼤⼤', age=5}] /**
* 3、交集
*/
System.out.println(CollectionUtils.intersection(personList, person1List));
//输出:[Person{name='中中', age=4}]
/**
* 4、交集的补集(析取)
*/
System.out.println(CollectionUtils.disjunction(personList, person1List));
//输出:[Person{name='⼩⼩', age=3}, Person{name='⼤⼤', age=5}]
}

本文发布于:2024-09-25 05:24:31,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/473190.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:集合   对象   需要   原有   相等
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议