Check if a value exists in ArrayList

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 … Read more

How to count the number of occurrences of an element in a List

I have an ArrayList, a Collection class of Java, as follows: ArrayList<String> animals = new ArrayList<String>(); animals.add(“bat”); animals.add(“owl”); animals.add(“bat”); animals.add(“bat”); As you can see, the animals ArrayList consists of 3 bat elements and one owl element. I was wondering if there is any API in the Collection framework that returns the number of bat occurrences … Read more

How to declare an ArrayList with values? [duplicate]

This question already has answers here: Initialization of an ArrayList in one line (33 answers) Closed 6 years ago. ArrayList or List declaration in Java has questioned and answered how to declare an empty ArrayList but how do I declare an ArrayList with values? I’ve tried the following but it returns a syntax error: import … Read more