Check If a Value Exists in Arraylist

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



Leave a reply



Submit