Check if a value exists in ArrayList
Just use ArrayList.contains(desiredElement). For example, if you're looking for the conta1 account from your example, you could use something like:
if (lista.contains(conta1)) {
System.out.println("Account found");
} else {
System.out.println("Account not found");
}
Edit:
Note that in order for this to work, you will need to properly override the equals() and hashCode() methods. If you are using Eclipse IDE, then you can have these methods generated by first opening the source file for your CurrentAccount
object and the selecting Source > Generate hashCode() and equals()...
How to check if an arrayList has a value inside of a value
You can use the Stream method allMatch();
if(words.stream().allMatch(word -> word.contains("Axe"))) {
//something
}
Check if a value exists in an object's list array
You can use streams api to easily implement your question
Supposed your Cliente
class had a name
property. Based on the comments you have provided, you can use the checkedCliente in the second if statement
if(!name && name.trim().equals("")){
Cliente checkedCliente = clientes.stream()
.filter(x -> name.equals(x.getName())) // check name is there?
.findAny()
.orElse(null);
if(checkedCliente == null){
Cliente cliente = new Cliente(name);
clientes.add(cliente);
System.out.println("Cliente "+cliente.getName+" Adicionado")
}
else {
System.out.println("Cliente " + cliente.getName() + "is already in the collection");
// checkedCliente can be used for updates etc in here.
}
}
Checking if an Object exists in ArrayList
You need to override your equals
and hashcode
method, like this:
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Order order = (Order) o;
return Objects.equals(tavolo, order.tavolo) &&
Objects.equals(ora, order.ora);
}
@Override
public int hashCode() {
return Objects.hash(tavolo, ora);
}
Related Topics
Performance Difference Between Java 8 Lambdas and Anonymous Inner Classes
Totally Confused with Java.Exe
How to Achieve Transfer File Between Client and Server Using Java Socket
Why Does This Subtraction Not Equal Zero
How to Change Background Color of Jtabbedpane
Should I Retrieve Database Record in Struts2 View Layer
Java Command Line with External .Jar
Does the Preparedstatement Avoid SQL Injection
Filling Combobox from Database by Using Hibernate in Java
How to Create New Xml File from Existing Database in Postgresql Database Using Java
When Does the Main Thread Stop in Java
Compile Time VS Run Time Dependency - Java
How to Pass an Integer Class Correctly by Reference
How to Merge Documents Correctly