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

java差集

發布時間: 2023-01-02 04:00:04

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

㈡ java集合類的交集及差集

我也在找差集的方法,不過你的交集代碼讓我感覺好多好亂,只要一小句便可求出交集--> list1.retainAll(list2); list1和list2的位置無所謂,這樣循環一下list1試試,如果還有多個list,一樣的道理。----雖然時間有點很久了,但是搜到了你的知道,還是過來看看。

㈢ java求兩個數組的補集

import java.util.Arrays; //Java中如何把兩個數組合並為一個 public class gog { public static void main(String[] args) { String [] str1 = {"J","a","v","a","中"}; String [] str2 = {"如","何","把","兩","個","數","組","合","並","為",...

㈣ 用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("無補集");
}

}

㈤ 用一個參數的JAVA程序實現集合的交並差運算

import java.util.HashSet;
import java.util.Iterator;

public class Testcase {
int x1[], x2[];

Testcase(int a[], int b[]) {
x1=a;
x2=b;

}

Testcase(Testcase d) {
x1=d.x1;
x2=d.x2;
}

public void Jiaoji(Testcase a) {
for(int i=0; i < a.x1.length; i++){
for(int j=0; j < a.x2.length; j++){
if(a.x1[i] == a.x2[j])
System.out.print(a.x1[i] + ",");
}
}

}

public static void main(String[] args) {
int x1[]= {
1, 4, 6, 9, 12, 18, 19, 45
};
int x2[]= {
4, 7, 9, 13, 19, 23, 29, 67
};
Testcase b=new Testcase(x1, x2);
b.Jiaoji(b);

System.out.println("並集是;");
int union[]=union(x1, x2);

for(int i:union){
System.out.print(i+" ");
}

System.out.print("\n差集是: ");
int diff[]=difference(x1, x2);
for(int i:diff){
System.out.print(i+" ");
}
}

//並集
static public int[] union(int a[], int b[]) {

HashSet<Integer> set=new HashSet<Integer>();
for(int i:a){
set.add(new Integer(i));
}
for(int i:b){
set.add(new Integer(i));
}
int size=set.size();
int out[]=new int[size];
Iterator<Integer> iter=set.iterator();
for(int i=0;i<size;i++){
//
//while(i.hasNext()){
out[i]=((Integer)iter.next()).intValue();
}
return out;
}
//差集
static public int[] difference(int a[], int b[]) {
HashSet<Integer> set=new HashSet<Integer>();
for(int i:a){
set.add(new Integer(i));
}

for(int i:b){
set.remove(new Integer(i));
}

int size=set.size();
int out[]=new int[size];
Iterator<Integer> iter=set.iterator();
for(int i=0;i<size;i++){

out[i]=((Integer)iter.next()).intValue();
}
return out;
}
}
========
輸出
4,9,19,並集是;
1 4 6 7 67 9 12 13 45 19 18 23 29
差集是: 1 18 6 12 45
==================
順便練了下集合

㈥ java找到兩個list的交集並集差集

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

set1.addAll(set2);

㈦ 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);

㈧ 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).
*/

㈨ java 求交集 並集 差集

import java.util.*;

public class ArrayTest {
public static void main(String[] args) {
int[] a = {1,6,4,5,2,3,};
int[] b = {2,3,4,56,7,8,99};
int[] t = ArrayTest.並集(a, b);
for(int i:t)System.out.print(i+" ");

System.out.println();

t = ArrayTest.交集(a, b);
for(int i:t)System.out.print(i+" ");
}

static int[] 並集(int[] a,int[] b){
Arrays.sort(a);
Arrays.sort(b);
int[] t = new int[a.length];
System.array(a,0,t,0,t.length);
out:
for(int i:b){
for(int j:a){
if(i==j)continue out;
}
t=putInt(t,i);
}
Arrays.sort(t);
return t;
}

static int[] 交集(int[] a,int[] b){
Arrays.sort(a);
Arrays.sort(b);
int[] t = new int[0];
for(int i:a){
for(int j:b){
if(i==j){
t=putInt(t,i);
break;
}
}
}
return t;
}

static int[] putInt(int[] a,int i){
int[] t = new int[a.length+1];
System.array(a, 0,t,0,a.length);
t[a.length]=i;
return t;
}
}

//做了交集,並集,差集自己想吧

熱點內容
javaweb開發教程視頻教程 發布:2025-07-05 13:24:41 瀏覽:662
康師傅控流腳本破解 發布:2025-07-05 13:17:27 瀏覽:225
java的開發流程 發布:2025-07-05 12:45:11 瀏覽:669
怎麼看內存卡配置 發布:2025-07-05 12:29:19 瀏覽:271
訪問學者英文個人簡歷 發布:2025-07-05 12:29:17 瀏覽:820
1970linux 發布:2025-07-05 12:12:43 瀏覽:109
解壓挑刺 發布:2025-07-05 12:12:12 瀏覽:537
rarlinux壓縮 發布:2025-07-05 12:08:52 瀏覽:399
手機點菜app怎麼連接電腦伺服器 發布:2025-07-05 11:13:05 瀏覽:944
配置控制台干什麼用的 發布:2025-07-05 10:54:51 瀏覽:963