Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中查找匹配对象_Java_Matching_Hashset_Treemap - Fatal编程技术网

在Java中查找匹配对象

在Java中查找匹配对象,java,matching,hashset,treemap,Java,Matching,Hashset,Treemap,我目前正试图根据两个对象的值匹配它们。除了,它不是a.a=a.a,而是a.a=a.b和a.b=b.a。这意味着重写等于是一个选项,但肯定不是正确的选项 虽然对这些对象进行排序将加快匹配时间,但总体数量将很小,因此没有必要。另外,compareTo也不完全正确,原因与等于的原因相同 我是否只是制定自己的方法以防万一?将有4个字段要匹配,这就是为什么我不在前面使用if语句的原因 public boolean isOpposite(Object other) { return (this.a

我目前正试图根据两个对象的值匹配它们。除了,它不是
a.a=a.a
,而是
a.a=a.b
a.b=b.a
。这意味着重写
等于
是一个选项,但肯定不是正确的选项

虽然对这些对象进行排序将加快匹配时间,但总体数量将很小,因此没有必要。另外,
compareTo
也不完全正确,原因与
等于
的原因相同

我是否只是制定自己的方法以防万一?将有4个字段要匹配,这就是为什么我不在前面使用if语句的原因

public boolean isOpposite(Object other) {
    return (this.a == other.b) ? true : false;
}
还有一种可能性是,对象将实现/扩展一个基础对象,以获得更多字段并实现其自己的匹配方式

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

    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

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

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


我可能会创建两个散列集,一个是原始散列集,另一个是相反的散列集,然后询问第二个散列集是否包含原始散列集的每个成员的对立面

我做了一些测试,并确定我知道如何实现这一点的最干净的方法是使用
ArrayList

这是我的实现:

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;
}

为什么您认为当访问时间为O(n)(摊销)时,
LinkedList
会更快?会不断访问列表中的对象。另一个人告诉我,LinkedList在这方面更快。我被告知错误了吗?你能用更具体的术语解释这个类的等价关系吗(例如,4个相关的类成员是什么,以及这些成员对平等的定义是什么)?如果你只从前面或后面得到东西,那么是的-这是恒定的时间。如果您从中间获取内容,那么它最多需要遍历N/2个元素才能找到它。
ArrayList
更快,因为它支持随机访问(由数组支持)。@Makoto感谢您的区别。我现在明白了。谢谢你的建议,Jiri,是的,N^2比较不是我所期待的。对于大约40000个条目,N^2在按枢轴排序并分为2个列表后,平均迭代次数为100000000次。我将尝试使用hashset,并在完成后回复我的发现。再次感谢,,
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.