How can I check if a String
is there in the List
?
I want to assign 1
to temp
if there is a result, 2
otherwise.
My current code is:
Integer temp = 0; List<String> bankAccNos = new ArrayList<String>();//assume list contains values String bankAccNo = "abc"; for(String no : bankAccNos) if(no.equals(bankAccNo)) temp = 1;
ArrayList. contains() method can be used to check if an element exists in an ArrayList or not. This method has a single parameter i.e. the element whose presence in the ArrayList is tested. Also it returns true if the element is present in the ArrayList and false if the element is not present.
contains() in Java. ArrayList contains() method in Java is used for checking if the specified element exists in the given list or not. Returns: It returns true if the specified element is found in the list else it returns false.
if (myList. Contains(myString)) string element = myList. ElementAt(myList. IndexOf(myString));
temp = bankAccNos.contains(no) ? 1 : 2;
The List
interface already has this solved.
int temp = 2; if(bankAccNos.contains(bakAccNo)) temp=1;
More can be found in the documentation about List.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With