当前位置:首页 » 编程语言 » 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;
}
}

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

热点内容
随机启动脚本 发布:2025-07-05 16:10:30 浏览:508
微博数据库设计 发布:2025-07-05 15:30:55 浏览:13
linux485 发布:2025-07-05 14:38:28 浏览:295
php用的软件 发布:2025-07-05 14:06:22 浏览:745
没有权限访问计算机 发布:2025-07-05 13:29:11 浏览:419
javaweb开发教程视频教程 发布:2025-07-05 13:24:41 浏览:668
康师傅控流脚本破解 发布:2025-07-05 13:17:27 浏览:229
java的开发流程 发布:2025-07-05 12:45:11 浏览:670
怎么看内存卡配置 发布:2025-07-05 12:29:19 浏览:271
访问学者英文个人简历 发布:2025-07-05 12:29:17 浏览:821