检查ArrayList中是否存在值

如何检查ArrayList中是否存在值?

List<CurrentAccount> lista = new ArrayList<CurrentAccount>();


CurrentAccount conta1 = new CurrentAccount("Alberto Carlos", 1052);
CurrentAccount conta2 = new CurrentAccount("Pedro Fonseca", 30);
CurrentAccount conta3 = new CurrentAccount("Ricardo Vitor", 1534);
CurrentAccount conta4 = new CurrentAccount("João Lopes", 3135);


lista.add(conta1);
lista.add(conta2);
lista.add(conta3);
lista.add(conta4);


Collections.sort(lista);


System.out.printf("Bank Accounts:" + "%n");
Iterator<CurrentAccount> itr = lista.iterator();
while (itr.hasNext()) {
CurrentAccount element = itr.next();
System.out.printf(element + " " + "%n");
}
System.out.println();
854669 次浏览

只需使用ArrayList.contains (desiredElement)即可。例如,如果您正在从示例中查找conta1帐户,您可以使用如下内容:

if (lista.contains(conta1)) {
System.out.println("Account found");
} else {
System.out.println("Account not found");
}

<强>编辑: 注意,为了让它工作,你需要正确地覆盖equals ()hashCode ()方法。如果你正在使用Eclipse IDE,那么你可以通过首先打开你的CurrentAccount对象的源文件并选择Source > Generate hashCode() and equals()...

来生成这些方法

在检查值是否存在时,最好使用HashSet而不是ArrayList

Java文档HashSet

该类为基本操作(添加、删除、包含和大小)提供恒定的时间性能。

ArrayList.contains()可能需要遍历整个列表才能找到你要找的实例。

请参考我在这篇文章上的回答。

没有必要遍历List;重写equals方法并使用.contains

@Override
public boolean equals (Object object) {
boolean result = false;
if (object == null || object.getClass() != getClass()) {
result = false;
} else {
EmployeeModel employee = (EmployeeModel) object;
if (this.name.equals(employee.getName()) && this.designation.equals(employee.getDesignation())   && this.age == employee.getAge()) {
result = true;
}
}
return result;
}

这样叫它:

public static void main(String args[]) {


EmployeeModel first = new EmployeeModel("Sameer", "Developer", 25);
EmployeeModel second = new EmployeeModel("Jon", "Manager", 30);
EmployeeModel third = new EmployeeModel("Priyanka", "Tester", 24);


List<EmployeeModel> employeeList = new ArrayList<EmployeeModel>();
employeeList.add(first);
employeeList.add(second);
employeeList.add(third);


EmployeeModel checkUserOne = new EmployeeModel("Sameer", "Developer", 25);
System.out.println("Check checkUserOne is in list or not");
System.out.println("Is checkUserOne Preasent = ? " + employeeList.contains(checkUserOne));


EmployeeModel checkUserTwo = new EmployeeModel("Tim", "Tester", 24);
System.out.println("Check checkUserTwo is in list or not");
System.out.println("Is checkUserTwo Preasent = ? " + employeeList.contains(checkUserTwo));


}

如果我们已经提供了equals的实现,我们可以使用contains方法来检查一个项是否存在,而hashCode else对象引用将用于相等比较。同样,对于列表,containsO(n)操作,因为它是O(1)用于HashSet,所以最好以后使用。在Java 8中,我们还可以使用流根据项的相等性或特定的属性来检查项。

Java 8

CurrentAccount conta5 = new CurrentAccount("João Lopes", 3135);
boolean itemExists = lista.stream().anyMatch(c -> c.equals(conta5)); //provided equals and hashcode overridden
System.out.println(itemExists); // true


String nameToMatch = "Ricardo Vitor";
boolean itemExistsBasedOnProp = lista.stream().map(CurrentAccount::getName).anyMatch(nameToMatch::equals);
System.out.println(itemExistsBasedOnProp); //true