1. 用java编写程序,求集合的并集、交集和差集
publicstaticvoidmain(String[]args){
Integer[]A={1,2,3,4};
Integer[]B={1,3,7,9,11};
List<Integer>listA=Arrays.asList(A);
List<Integer>listB=Arrays.asList(B);
List<Integer>jiaoji=newArrayList<Integer>();
for(Integera:listA){
if(listB.contains(a)){
jiaoji.add(a);
}
}
System.out.println(jiaoji);
List<Integer>bingji=newArrayList<Integer>();
for(Integera:listA){
if(!bingji.contains(a)){
bingji.add(a);
}
}
for(Integerb:listB){
if(!bingji.contains(b)){
bingji.add(b);
}
}
System.out.println(bingji);
List<Integer>chaji=newArrayList<Integer>();
for(Integera:listA){
if(!listB.contains(a)){
chaji.add(a);
}
}
System.out.println(chaji);
}
2. java学习中,怎么求交集和并集
publicstaticvoidtest3(){
Set<String>cc=newHashSet<String>();
cc.add("2");
cc.add("a");
cc.add("b");
cc.add("x");
Set<String>dd=newHashSet<String>();
dd.add("b");
dd.add("1");
dd.add("2");
dd.add("3");
dd.add("a");
Set<String>ff=newHashSet<String>();
ff.addAll(cc);
ff.addAll(dd);
System.out.println(ff.toString());
}
这是并集
3. Java求两Collection的交集
intersection()没问题。使用1.7测试代码如下:
importjava.util.ArrayList;
importjava.util.Collection;
publicclassTest{
publicstaticvoidmain(String[]args){
String[]arr1={"M116","M140","M250","M120","M98"};
String[]arr2={"M116","M140","M250","M187","M98","M120"};
Collection<String>collection1=toCollection(arr1);
Collection<String>collection2=toCollection(arr2);
Collection<String>collection=intersection(collection1,collection2);
for(Strings:collection){
System.out.println(s);
}
}
publicstatic<T>Collection<T>intersection(Collection<T>collection1,
Collection<T>collection2){
Collection<T>collection=newArrayList<T>();//Howwoulditbe??
for(Tt:collection1){
if(collection2.contains(t)){
collection.add(t);
}
}
returncollection;
}
publicstatic<T>Collection<T>toCollection(T[]arr){
Collection<T>collection=newArrayList<T>();
for(Tt:arr){
collection.add(t);
}
returncollection;
}
}
4. JAVA怎么取多个List集合的交集
1、把多个list放到一个list中,生成 List<List<Object>>结构
2、遍历list,使用java8的规约操作,两两求交集
list.stream().rece((list1,list2) -> { list1.retainAll(list2); return list1;}).orElse(emptyList());
5. java找到两个list的交集并集差集
//橘散庆交集
set1.retainAll(set2);
//差圆握集
set1.removeAll(set2);
//并掘举集1
set1.addAll(set2);
6. java求交集和并集
处理集合交并集运算的类,实现java专门的集合类
简单实现代码如下:
package test;
import java.util.HashSet;
import java.util.Set;
public class Test {
public static void main(String[] args) {
Set<Integer> result = new HashSet<Integer>();
Set<Integer> set1 = new HashSet<Integer>(){{
add(1);
add(3);
add(5);
}};
Set<Integer> set2 = new HashSet<Integer>(){{
add(1);
add(2);
add(3);
}};
result.clear();
result.addAll(set1);
result.retainAll(set2);
System.out.println("交集:"+result);
result.clear();
result.addAll(set1);
result.addAll(set2);
System.out.println("并集:"+result);
}
}
7. java 怎么设计一个set类 然后new两个set对象 进行交集 并集 求代码
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class MySet {
public static void main(String[] args) {
Set set1 = new TreeSet();
set1.add("aa");
set1.add("bb");
set1.add("CC");
Set set2 = new TreeSet();
set2.add("aa");
set2.add("eeff");
Set unionSet = MySet.union(set1, set2);
Set intersectionSet = MySet.intersection(set1, set2);
printSet(unionSet);
System.out.println("-----------------------------");
printSet(intersectionSet);
}
private static void printSet(Set set) {
Iterator ite = set.iterator();
while(ite.hasNext()){
System.out.println(ite.next().toString());
}
}
public static Set union(Set set1, Set set2){
Set set = new TreeSet();
set.addAll(set1);
set.addAll(set2);
return set;
}
public static Set intersection(Set set1, Set set2){
Set set = new TreeSet();
if(set1.size() > set2.size()){
set.addAll(set2);
}else{
set.addAll(set1);
}
Iterator ite = set.iterator();
while(ite.hasNext()){
Object obj = ite.next();
if(!(set1.contains(obj) && set2.contains(obj))){
set.remove(obj);
}
}
return set;
}
}
--------------------------
CC
aa
bb
eeff
-----------------------------
aa