導航:首頁 > 編程語言 > java兩個集合的交集

java兩個集合的交集

發布時間:2023-10-22 15:36:16

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

閱讀全文

與java兩個集合的交集相關的資料

熱點內容
程序員吃雞蛋炒菜 瀏覽:173
在哪裡看俄羅斯電視劇app 瀏覽:308
怎麼找資料庫伺服器地址 瀏覽:486
伺服器調試怎麼翻譯 瀏覽:921
php如何處理ajax請求 瀏覽:211
php數組下標存在 瀏覽:706
php獲取ip歸屬地 瀏覽:175
撩女程序員怎麼辦 瀏覽:508
百度伺服器做什麼 瀏覽:192
打開軟體加速伺服器有什麼危害 瀏覽:87
php去除數組下標 瀏覽:794
ipad的app內容哪裡看 瀏覽:284
遇見空間app在哪裡 瀏覽:547
用命令對一個文件內容進行統計 瀏覽:317
華為交換機配置命令縮寫 瀏覽:344
鏈接伺服器出現問題怎麼處理 瀏覽:833
華為手機怎麼打開加密 瀏覽:676
開利中央空調壓縮機 瀏覽:742
程序員面臨脫發 瀏覽:872
javaweb管理系統源碼下載 瀏覽:733