How can I check if a value that is written in scanner exists in an 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();
We can use contains
method to check if an item exists if we have provided the implementation of equals
and hashCode
else object reference will be used for equality comparison. Also in case of a list contains
is O(n)
operation where as it is O(1)
for HashSet
so better to use later. In Java 8 we can use streams also to check item based on its equality or based on a specific property.
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
public static void linktest()
{
System.setProperty("webdriver.chrome.driver","C://Users//WDSI//Downloads/chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://toolsqa.wpengine.com/");
//List<WebElement> allLinkElements=(List<WebElement>) driver.findElement(By.xpath("//a"));
//int linkcount=allLinkElements.size();
//System.out.println(linkcount);
List<WebElement> link = driver.findElements(By.tagName("a"));
String data="HOME";
int linkcount=link.size();
System.out.println(linkcount);
for(int i=0;i<link.size();i++) {
if(link.get(i).getText().contains(data)) {
System.out.println("true");
}
}
}
Better to use a HashSet
than an ArrayList
when you are checking for existence of a value.
Java docs for HashSet
says: "This class offers constant time performance for the basic operations (add, remove, contains and size)"
ArrayList.contains()
might have to iterate the whole list to find the instance you are looking for.
Just use .contains
. For example, if you were checking if an ArrayList arr
contains a value val
, you would simply run arr.contains(val)
, which would return a boolean representing if the value is contained. For more information, see the docs for .contains
.
Please refer to my answer on this post.
There is no need to iterate over the List
just overwrite the equals
method.
Use equals
instead of ==
@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;
}
Call it like this:
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));
}
When Array List contains object of Primitive DataType.
Use this function:
arrayList.contains(value);
if list contains that value then it will return true else false.
When Array List contains object of UserDefined DataType.
Follow this below Link
How to compare Objects attributes in an ArrayList?
I hope this solution will help you. Thanks
Source: Stackoverflow.com