检查ArrayList中是否存在值

问题描述:

如何检查在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(); 

只需使用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()...

+7

equals()方法应该在CurrentAccount中被覆盖以确定它们何时是同一个对象 – Javi 2010-12-09 23:28:16

+1

在这种情况下,hashcode()也需要被覆盖。每hashcode()合约等于对象必须具有相同的散列码。 – zockman 2010-12-09 23:46:40

更好,当你正在检查一个值的存在,使用HashSetArrayList产生的这些方法。 Java文档的HashSet说:"This class offers constant time performance for the basic operations (add, remove, contains and size)"

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

你好只是指我的回答this post在这里解释 有没有必要迭代列表只是覆盖等于方法。

修订 equels方法而不是==

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

} 

当数组列表包含原始数据类型的对象。

Use this function: 
arrayList.contains(value); 

if list contains that value then it will return true else false. 

当数组列表包含UserDefined DataType的对象时。

Follow this below Link 

How to compare Objects attributes in an ArrayList?

我希望这个解决方案将帮助你。 谢谢