當前位置:首頁 » 編程語言 » java集合差集

java集合差集

發布時間: 2023-07-27 05:06:04

A. 用java編寫程序,集合元素為小寫字母,實現集合的交,並,補,差運算

public static void main(String[] args) {
Set<Character> result = new HashSet<Character>();
Set<Character> set1 = new HashSet<Character>() {
{
add('a');
add('b');
add('c');
add('d');
add('e');
}
};

Set<Character> set2 = new HashSet<Character>() {
{
add('a');
add('b');
add('c');
}
};

result.clear();
result.addAll(set1);
result.retainAll(set2);
System.out.println("交集:" + result);

result.clear();
result.addAll(set1);
result.removeAll(set2);
System.out.println("差集:" + result);

result.clear();
result.addAll(set1);
result.addAll(set2);
System.out.println("並集:" + result);

result.clear();
result.addAll(set1);
if(result.containsAll(set2)){
result.removeAll(set2);
System.out.println("補集:"+result);
}else{
System.out.println("無補集");
}

}

B. java集合求差值和並集!

差集
ArrayList<String> stuList = new ArrayList<String>();
stuList.add("aa");
stuList.add("bb");
stuList.add("cc");
stuList.add("dd");
ArrayList<String> stuList2 = new ArrayList<String>();
stuList2.add("bb");
stuList2.add("cc");
stuList2.add("ee");
stuList2.add("ff");
for (String s : stuList2) {
if (stuList.contains(s)) {
stuList.remove(s);
} else {
stuList.add(s);
}
}
System.out.println(stuList2);
合集
ArrayList stuList = new ArrayList();
stuList.add("aa");
stuList.add("bb");
stuList.add("cc");
stuList.add("dd");
ArrayList stuList2 = new ArrayList();
stuList2.add("bb");
stuList2.add("cc");
stuList2.add("ee");
stuList2.add("ff");
Set set=new HashSet();
for (Object object : stuList) {
set.add(object);
}
for (Object object : stuList2) {
set.add(object);
}
System.out.println(set);

C. 用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);
}

D. java找到兩個list的交集並集差集

//交集
set1.retainAll(set2);
//差集
set1.removeAll(set2);
//並集1

set1.addAll(set2);

E. Java中list集合的交集和差集的用法和如何將

只要一小句便可求出交集--> list1.retainAll(list2); list1和list2的位置無所謂,這樣循環一下list1試試,如果還有多個list,一樣的道理

F. Java中的Set類差集問題差集為什麼是1,2不是1,2,8,9,;

如下英文是Guava中注釋原文。意思是說該方法返回只存在於set1獨有的數據,至於set2中獨有數據和set1和set2交集的數據直接忽略。而且返回的只是不可變的Set視圖,不會修改原set中數據。
/**
* Returns an unmodifiable view of the difference of two sets. The
* returned set contains all elements that are contained by set1 and
* not contained by set2. set2 may also contain elements not
* present in set1; these are simply ignored. The iteration order of
* the returned set matches that of set1.
*
* Results are undefined if set1 and set2 are sets based
* on different equivalence relations (as HashSet, TreeSet,
* and the keySet of an IdentityHashMap all are).
*/

熱點內容
紅帆oa伺服器地址查詢 發布:2025-02-07 14:31:41 瀏覽:657
文本框腳本圖片 發布:2025-02-07 14:23:28 瀏覽:231
少兒編程c語言 發布:2025-02-07 14:22:50 瀏覽:218
一階低通濾波器c語言 發布:2025-02-07 14:22:37 瀏覽:852
電腦的東西為什麼粘貼不到伺服器 發布:2025-02-07 14:21:04 瀏覽:197
手機脫模解壓視頻 發布:2025-02-07 14:20:18 瀏覽:473
密碼多少密碼多少密碼多少密碼 發布:2025-02-07 14:07:30 瀏覽:857
我的世界啟動器電腦伺服器 發布:2025-02-07 14:07:27 瀏覽:484
愛加密深圳科技有限 發布:2025-02-07 14:07:26 瀏覽:87
c語言密碼星號 發布:2025-02-07 14:07:24 瀏覽:801