gpt4 book ai didi

java - 比较器java中计数器变量的奇怪输出

转载 作者:行者123 更新时间:2023-11-29 07:33:09 25 4
gpt4 key购买 nike

为了进一步了解java集合的Comparator功能,我写了一段代码。我有两组,每组有 3 个元素。我想比较一下。我在下面发布我的代码和计数器变量的输出。谁能解释为什么变量 i 给出了这个奇怪的输出?我无法理解流程。

public class TestProductBundle {
public static void main(String args[]) {

TestProductBundle productBundle = new TestProductBundle();

Set<ClassA> hashSetA = new HashSet<ClassA>() {
{
add(new ClassA("name", 1, "desc"));
add(new ClassA("name", 2, "desc"));
add(new ClassA("name", 3, "desc"));
}
};

Set<ClassA> hashSetB = new HashSet<ClassA>() {
{
add(new ClassA("name1", 2, "desc1")); //"name" & "desc" are different than previous
add(new ClassA("name2", 1, "desc2"));
add(new ClassA("name3", 3, "desc3"));
}
};

if (productBundle.compareCollection(hashSetA, hashSetB)) {
System.out.println("Equal set of tree");
} else {
System.out.println("Unequal set of tree");
}
}

@SuppressWarnings("serial")
public boolean compareCollection(Set<ClassA> collection1, Set<ClassA> collection2) {

TreeSet<ClassA> treeSetA = new TreeSet<ClassA>(new CompareID()) {
{
addAll(collection1);
}
};

TreeSet<ClassA> treeSetB = new TreeSet<ClassA>(new CompareID()) {
{
addAll(collection2);
}
};

if (treeSetA.containsAll(treeSetB) && treeSetB.containsAll(treeSetA))
return true;
else
return false;
}
}

ClassA 和实现 Comparator 的类的代码。

class ClassA {
String name;
int id;
String desc;

public ClassA(String name, int id, String desc) {
this.name = name;
this.id = id;
this.desc = desc;
}

int getId() {
return id;
}
}

&

class CompareID implements Comparator<ClassA> {
int i = 0;

@Override
public int compare(ClassA o1, ClassA o2) {
System.out.println(i++); // Counter variable
if (o1.getId() > o2.getId())
return 1;
else if (o1.getId() < o2.getId())
return -1;
else
return 0;

}
}

输出是(也在调试器中交叉验证)

0
1
2
3
0 // why started from 0 again ?
1
2
3
4
5
6
7
8
4 // What the hell !!!
5
6
7
8
Equal set of tree // is that correct output ?

最佳答案

我不确定您发现了什么奇怪的地方。您有两个 TreeSet 实例,每个实例都有自己的CompareID 实例用作Comparator,并且每个CompareID 实例维护它的自己的柜台。

因此,看到每个计数器值(0、1、2 等)出现两次也就不足为奇了。至于计数器值出现的顺序,取决于TreeSet的内部实现。

至于

Equal set of tree // is that correct output ?

是的,两个集合包含相同的元素。顺序无关紧要。澄清一下 - TreeSet 的方法 containscontainsAll 认为元素 x 包含在 中TreeSet 如果 compare(x,y)==0 对于 TreeSet 的某些元素 y,其中 compare 是提供的 Comparatorcompare 方法。因此,在您的示例中,只有 id 属性确定两个元素是否相等。

以下场景解释了输出:

0 // compare method of 1st CompareID object executed
1 // compare method of 1st CompareID object executed
2 // compare method of 1st CompareID object executed
3 // compare method of 1st CompareID object executed
0 // compare method of 2nd CompareID object executed
1 // compare method of 2nd CompareID object executed
2 // compare method of 2nd CompareID object executed
3 // compare method of 2nd CompareID object executed
4 // compare method of 1st CompareID object executed
5 // compare method of 1st CompareID object executed
6 // compare method of 1st CompareID object executed
7 // compare method of 1st CompareID object executed
8 // compare method of 1st CompareID object executed
4 // compare method of 2nd CompareID object executed
5 // compare method of 2nd CompareID object executed
6 // compare method of 2nd CompareID object executed
7 // compare method of 2nd CompareID object executed
8 // compare method of 2nd CompareID object executed

编辑:首先,您要向第一个 TreeSet 添加元素(因此第一个 Comparatorcompare 被连续多次调用) ,那么你正在向第二个 TreeSet 添加元素(因此第二个 Comparatorcompare 被连续多次调用),然后你调用treeSetA.containsAll(treeSetB) 导致连续多次调用第一个 Comparatorcompare,最后调用 treeSetB.containsAll(treeSetA) 导致连续多次调用第二个 Comparatorcompare

关于java - 比较器java中计数器变量的奇怪输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39386415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com