在 Java 中查找匹配对象

Finding matching objects in Java

我目前正在尝试根据它们的值匹配 2 个对象。除了,它不是 a.a = a.a,而是 a.a = a.ba.b = b.a。这意味着覆盖 equals 是一个选项,但它肯定不是正确的选项。

虽然对这些对象进行排序会使匹配时间更快,但人口会很少,因此没有必要。此外,compareTo 也不完全正确,原因与 equals.

相同

我只是简单地制定自己的方法以防万一吗?将有 4 个字段要匹配,这就是为什么我没有预先使用 if 语句的原因。

public boolean isOpposite(Object other) {
    return (this.a == other.b) ? true : false;
}

也有可能对象会implement/extend一个基础对象来承担更多的字段并实现自己的匹配方式。

我正在考虑使用 LinkedList,因为我知道它比 ArrayList 使用起来更快,但我也一直在考虑使用 Map。 编辑:更好地解释对象

    public class Obj {     
public String a;     
public String b;     
public String c;     
public double d;    
}

关系如下:

    Obj obj1, obj2;
obj1.a == obj2.b //.equals for String of course
obj1.b == obj2.a
obj1.c == obj2.c
obj1.d == obj2.d * -1

如您所述,覆盖 equalscompareTo 不是正确的方法。因为假设这两种方法都应该是可传递的,即 A eq B and B eq C => A eq C 但它不适用于 "opposite" 对象。很高兴知道,因为您无法定义等价 class 并将其划分为子集,但您需要找到所有对(取决于您的用例)。

不确定,你的目标是什么。如果您有一些装有此类对象的容器并且您需要找到满足条件的所有对,那么恐怕您需要进行 n^2 次比较。

我可能会创建两个散列集,一个包含原件,第二个包含反面,并询问第二个散列集是否包含原始散列集的每个成员的反面。

我已经做了一些测试并确定我知道如何实现它的最干净的方法是使用 ArrayList<Obj>

这是我的实现:

public static List<ObjGroup> getNewSampleGroup(int size) {
    List<ObjGroup> sampleGroup = new ArrayList<ObjGroup>();
    sampleGroup.add(new ObjGroup((generateNumbers(size, 1)))); //Positives
    sampleGroup.add(new ObjGroup((generateNumbers(size, -1)))); //Negatives
    return sampleGroup;
}

private static List<Obj> generateNumbers(int size, int x) {
    List<Obj> sampleGroup = new ArrayList<Obj>();
    for (int i = 0; i < size; i ++) {
        Random rand = new Random();
        String randC;
        String randA;
        String randB;
        double randD;

        if (x == 1) {
            randD = rand.nextInt((maxP - minP + 1) + minP);
            randA = "aval";// + String.valueOf(rand.nextInt((max - min + 1) + min));
            randB = "bval";// + String.valueOf(rand.nextInt((max - min + 1) + min));
            randC = "cval";// + String.valueOf(rand.nextInt((max - min + 1) + min));
        } else {
            randD = rand.nextInt((maxP - minP + 1) + minP) * -1;
            randA = "bval";// + String.valueOf(rand.nextInt((max - min + 1) + min));
            randB = "aval";// + String.valueOf(rand.nextInt((max - min + 1) + min));
            randC = "cval";// + String.valueOf(rand.nextInt((max - min + 1) + min));
        }
        sampleGroup.add(new Obj(randA, randB, randC, randD));
    }
    return sampleGroup;
}

public List<ObjGroup> findMatches(List<ObjGroup> unmatchedList) {
    List<Obj> pivotPos = unmatchedList.get(0).getObjs(); //First grouping are positives
    List<Obj> pivotNeg = unmatchedList.get(1).getObjs(); //Second grouping are negatives
    List<ObjGroup> matchedList = new ArrayList<ObjGroup>();
    long iterations = 0;

    Collections.sort(pivotPos);
    Collections.sort(pivotNeg, Collections.reverseOrder());

    for (Iterator<Obj> iteratorPos = pivotPos.iterator(); iteratorPos.hasNext();) {
        final Obj focus = iteratorPos.next();
        iteratorPos.remove(); //Remove this once pulled as you won't match it again.
        for (Iterator<Obj> iteratorNeg = pivotNeg.iterator(); iteratorNeg.hasNext();) {
            final Obj candidate = iteratorNeg.next();
            if (compare(focus, candidate)) {
                matchedList.add(new ObjGroup(new ArrayList<Obj>() {
                    {
                        add(focus);
                        add(candidate);
                    }
                }));
                iteratorNeg.remove(); //Remove this once matched as you won't match it again.
                break;
            }
            iterations ++;
        }
        iterations ++;
    }
    return matchedList;
}

I 运行 这是针对 4,000,000 个伪 运行dom Obj 对象的样本大小。这是我的输出:

Starting matching test.
18481512007 iterations.
3979042 matched objects.
10479 unmatched objects.
Processing time: 44 minutes.
There were 1989521 number of matches found.
Closing matching test.