主题:[讨论]比较两个集合
postg_cm
[专家分:40] 发布于 2007-10-16 17:17:00
我想比较两个集合,如果两个集合中的元素都相同则返回真,如果有一个不相同则返回假,这个程序怎么写?谢谢各位大侠!
回复列表 (共4个回复)
沙发
haoboy0817 [专家分:880] 发布于 2007-10-16 20:07:00
哥们给你解决了,我把你所谓的“集合”假设为对象数组(当然此程序同样适用一般数组,不过可以更简单),希望你能明白!!
class CompareTest
{
public static void main(String[] args)
{
Student [] sd1=new Student[]{new Student(1,"zhangsan"),new Student(2,"lisi"),new Student(3,"wangwu"),new Student(4,"zhaoliu")};
Student [] sd2=new Student[]{new Student(1,"zhangsan"),new Student(2,"lisi"),new Student(3,"wangwu"),new Student(4,"zhaoliu")};
boolean bl=false;
if(sd1.length!=sd2.length)
{
bl=false;
}
else
{
for(int i=0;i<sd1.length;i++)
{
if(sd1[i].equals(sd2[i]))
{
bl=true;
}
else
{
bl=false;
break;
}
}
}
if(bl==true)
{
System.out.println("the two sets are equal");
}
else
{
System.out.println("the two sets aren't equal");
}
}
}
class Student //implements Comparable
{
int num;
String name;
Student(int num,String name)
{
this.num=num;
this.name=name;
}
public int hashCode()
{
return num*name.hashCode();
}
public boolean equals(Object o)
{
Student s=(Student)o;
return s.num==num&&s.name.equals(name);
}
}
板凳
sjhlovejava [专家分:1690] 发布于 2007-10-16 20:45:00
道理同我给的另一个程序:
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Test4 {
/**
* @param args
*/
public static void main(String[] args) {
List lista = new ArrayList();
List listb = new ArrayList();
lista.add("A");
lista.add("B");
lista.add(1);
lista.add(8);
listb.add("B");
listb.add("A");
listb.add(1);
listb.add(8);
System.out.println(judgeCollection(lista, listb));
String[] stra = {"a","b","c"};
String[] strb = {"c","a","b"};
System.out.println(judgeArray(stra, strb));
}
public static Boolean judgeCollection(Collection A,Collection B){
//首先比较元素个数是否一样
if(A.size() == B.size()){
//这边就不考虑集合内存在相同元素的情况
Integer index = 0;
for(Object objb : B.toArray()){
for(Object obja : A.toArray()){
if(obja.equals(objb)){
//与A元素相同时,自加1,若全相同index将与A集合数量相等
index++;
break;
}
}
}
//若相等,则说明两个集合中的元素都相同
if(index == A.size()){
return true;
}else{
return false;
}
}else{
//若不一样,则返回false
return false;
}
}
public static Boolean judgeArray(String[] A,String[] B){
//首先比较元素个数是否一样
if(A.length == B.length){
//这边就不考虑集合内存在相同元素的情况
Integer index = 0;
for(Object objb : B){
for(Object obja : A){
if(obja.equals(objb)){
//与A元素相同时,自加1,若全相同index将与A集合数量相等
index++;
break;
}
}
}
//若相等,则说明两个集合中的元素都相同
if(index == A.length){
return true;
}else{
return false;
}
}else{
//若不一样,则返回false
return false;
}
}
}
3 楼
daifei4321 [专家分:2590] 发布于 2007-10-16 23:39:00
真的要用的话,建议别自己写,使用JAVA默认的集合.
自己写着只能玩玩的.JAVA的java.util.Set类非常强大,实现了Generic,同步,算法也很高级.尽管前面3项我们都能做到,但是JAVA自带的类里还有很多NATIVE代码提高效率,不是我们用JAVA写的集合类能比的.
用法:
import java.util.*;
......
......
Set<Integer> s1 = new HashSet<Integer>();
Set<Integer> s2 = new HashSet<Integer>();
//这里Integer可以换成自己想要的类,只要实现Comparable接口.
s1.add(1);s1.add(2);s1.add(3);
s2.add(3);s2.add(2);s2.add(1);
System.out.println(s1.equals(s2));//这就比较完了,相当简单吧.
//把代码放到适当的地方运行试试吧.
4 楼
fihuang [专家分:0] 发布于 2007-10-18 02:38:00
学习中.~~
daifei4321.学java多久了啊..思路真清晰~~强~!!!!!!!!!!!!!!!!!
我来回复