導航:首頁 > 編程語言 > java取差集

java取差集

發布時間:2023-04-24 02:07:19

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編寫程序,求集合的並集、交集和差集

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集合求差值和並集!

差集
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 獲取兩個map集合中不同的數據,求大神

Map[key1].value
Map[key2].value

❺ 用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:兩個數組,一個數組是另一個數組的子集,如何取補集比如大數組4,5,6,子集數組5,6,如何取4

importjava.util.ArrayList;
importjava.util.List;
publicclassstrSplit{
publicstaticvoidmain(Stringargs[])
{
List<String>list1=newArrayList<String>();
List<String>list2=newArrayList<String>();
list1.add("g");
list1.add("s");
list1.add("a");
list1.add("f");
list2.add("g");
list2.add("c");
list2.add("b");
滾爛list2.add("a");
//取交集
list1.retainAll(list2);
System.out.print(list1);
//取補集橡備和
梁盯//list1.removeall(list2);
//System.out.print(list1);
}
}

❽ java兩個int數組判斷不重復的值,有難度

packagetest;

importjava.util.Arrays;

publicclassMyTester
{
privatestaticStringresolve(神陸蠢int[]A,int[]B)
{
Strings1=Arrays.toString(A).replaceAll("\s","").replaceAll("[\[\]]",",");
for(inti=0;i<B.length;i++)
{
if(s1.indexOf(B[i])==-1)
{
s1=s1.replaceFirst("悉則,"+B[i]+",",",");
}
}
returns1.replaceAll("^\,|\,$","");
}

publicstaticvoidmain(String[]args)
{
int[]A={1,游陪6,3,1,8};
int[]B={1,3,6};
Strings1=resolve(A,B);
System.out.println(s1);

A=newint[]{4,3,3,3,2};
B=newint[]{3,3,4};
s1=resolve(A,B);
System.out.println(s1);
}
}

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

//橘散慶交集
set1.retainAll(set2);
//差圓握集
set1.removeAll(set2);
//並掘舉集1

set1.addAll(set2);

閱讀全文

與java取差集相關的資料

熱點內容
760貼片機編程視頻 瀏覽:331
歐姆龍plc編程第36講 瀏覽:907
我的世界如何將一個伺服器弄崩 瀏覽:6
php網站訪問量代碼 瀏覽:431
怠速壓縮機咔咔響 瀏覽:176
怎麼才能修改APP中的數據 瀏覽:688
哪裡有搶單的app 瀏覽:462
演算法概率題 瀏覽:465
長方形拉伸的命令 瀏覽:279
python代碼函數編程技術 瀏覽:194
java正則式 瀏覽:429
外包程序員好進嗎 瀏覽:384
雲伺服器服務模型架構 瀏覽:901
刪文件夾什麼指令 瀏覽:509
極速抖音已加密怎麼辦 瀏覽:603
matlab拉格朗日演算法框圖 瀏覽:430
華為公司計算機視覺演算法顧問 瀏覽:254
夏老師講的單片機 瀏覽:298
在編程中如何將圖片放大 瀏覽:163
appstore怎麼看是否付費 瀏覽:603