Java 验证列表中是否存在对象

Java 验证列表中是否存在对象,java,collections,comparator,Java,Collections,Comparator,我需要的是:验证列表中是否存在比较某些属性的对象 我在收集和比较方面遇到了麻烦。我正在尝试使用此二进制搜索进行验证: Collections.binarySearch(listFuncionarioObs2, formFuncionarioObsIns, formFuncionarioObsIns.objectComparator);//Binary search of an object in a List of this Object. 有了这个比较器: public int compar

我需要的是:验证列表中是否存在比较某些属性的对象

我在收集和比较方面遇到了麻烦。我正在尝试使用此二进制搜索进行验证:

Collections.binarySearch(listFuncionarioObs2, formFuncionarioObsIns, formFuncionarioObsIns.objectComparator);//Binary search of an object in a List of this Object.
有了这个比较器:

public int compare(FuncionarioObs func, FuncionarioObs funcToCompare) {

    int testCodigo = -1;

    if(null != func2.getCodigo()){
        testCodigo = func.getCodigo().compareTo(funcToCompare.getCodigo());
    }

    int testData = func.getData().compareTo(funcToCompare.getData());
    int testEvento = func.getEvento().compareTo(funcToCompare.getEvento());
    int testAndamento = func.getAndamento().compareTo(funcToCompare.getAndamento());

    if(testCodigo == 0 && testData == 0 && testEvento == 0 && testAndamento == 0){
        return 0;
    }else if(testData == 0 && testEvento == 0 && testAndamento == 0) {
        return 0;
    }

    return -1;

}
但是我有点迷路了,这不起作用,我不知道最好的方法。有人能帮我开灯吗

致以最良好的祝愿

编辑

我正在使用以下代码对二进制搜索之前的列表进行排序:

List<FuncionarioObs> listFuncionarioObsBD = funcionarioObsDAO.getFuncionarioObsById(sigla);
Collections.sort(listFuncionarioObsBD);
比较 你的比较不正确。现在它只是比较对象的引用。您必须更改此选项以比较对象值:

@Override public int compareTo(Account aThat) {
final int BEFORE = -1;
final int EQUAL = 0;
final int AFTER = 1;

//this optimization is usually worthwhile, and can
//always be added
if (this == aThat) return EQUAL;

//primitive numbers follow this form
if (this.fAccountNumber < aThat.fAccountNumber) return BEFORE;
if (this.fAccountNumber > aThat.fAccountNumber) return AFTER;

//booleans follow this form
if (!this.fIsNewAccount && aThat.fIsNewAccount) return BEFORE;
if (this.fIsNewAccount && !aThat.fIsNewAccount) return AFTER;

.
.
.
//all comparisons have yielded equality
//verify that compareTo is consistent with equals (optional)
assert this.equals(aThat) : "compareTo inconsistent with equals.";

return EQUAL;
}
@覆盖公共整数比较(帐户aThat){
之前的最终整数=-1;
最终整数等于0;
之后的最终整数=1;
//这种优化通常是值得的,并且可以
//始终添加
如果(this==aThat)返回相等值;
//原始数字遵循这种形式
如果(this.fAccountNumberaThat.fAccountNumber)在之后返回;
//布尔人遵循这种形式
如果(!this.fIsNewAccount&&aThat.fIsNewAccount)在之前返回;
如果(this.fIsNewAccount&&!aThat.fIsNewAccount)在之后返回;
.
.
.
//所有的比较结果都是平等的
//验证compareTo是否与equals一致(可选)
断言此。等于(aThat):“与等于不一致。”;
回报相等;
}

寻找目标 现在是下一部分。
正如CrtlAltDelete所暗示的,它取决于列表是否排序。
如果按升序排序:遍历对象,直到找到一个返回零(=成功)或一(=失败)的比较对象


对于未排序的列表,您必须遍历所有对象以搜索返回零的对象。

您的列表是否按照比较器中定义的顺序进行了排序/排序?您确定要在方法末尾返回
-1
,还是确实要返回
testCodigo
?还有:为什么有
if/else if
逻辑——这两个分支是多余的。@ControlAltDel我把我正在使用的排序放在这个比较器之前。。。。但这似乎也是多余的。。我不知道比较器是否工作正常。
@Override public int compareTo(Account aThat) {
final int BEFORE = -1;
final int EQUAL = 0;
final int AFTER = 1;

//this optimization is usually worthwhile, and can
//always be added
if (this == aThat) return EQUAL;

//primitive numbers follow this form
if (this.fAccountNumber < aThat.fAccountNumber) return BEFORE;
if (this.fAccountNumber > aThat.fAccountNumber) return AFTER;

//booleans follow this form
if (!this.fIsNewAccount && aThat.fIsNewAccount) return BEFORE;
if (this.fIsNewAccount && !aThat.fIsNewAccount) return AFTER;

.
.
.
//all comparisons have yielded equality
//verify that compareTo is consistent with equals (optional)
assert this.equals(aThat) : "compareTo inconsistent with equals.";

return EQUAL;
}