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