如何检查数组列表中是否存在值?

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();

当前回答

如果我们提供了equals的实现,我们可以使用contains方法来检查项是否存在,并且hashCode else对象引用将用于相等比较。此外,如果一个列表包含O(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

其他回答

当你检查一个值是否存在时,使用HashSet比ArrayList更好。

HashSet的Java文档说

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

ArrayList.contains()可能必须遍历整个列表才能找到您要查找的实例。

只需使用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()和equals()…

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

不需要遍历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对象引用将用于相等比较。此外,如果一个列表包含O(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