在Java中查找匹配的对象

时间:2015-06-19 22:55:41

标签: java matching hashset treemap

我目前正在尝试根据其值匹配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;
}

对象还有可能实现/扩展基础对象以接受更多字段并实现自己的匹配方式。

我正在考虑使用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

2 个答案:

答案 0 :(得分:2)

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

不确定,你的目标是什么。如果你有一些带有这些物体的容器,你需要找到满足条件的所有对,那么我恐怕你需要进行n ^ 2比较。

我可能会创建两个哈希集,一个是原始的,另一个是对立的,并询问第二个哈希集是否包含原始哈希集的每个成员的相反。

答案 1 :(得分:0)

我已经做了一些测试,并确定我知道如何实现这一点的最简洁方法是使用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;
}

我针对4,000,000个伪随机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.
相关问题