Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compare String value with ArrayList of String type in Java?

Tags:

java

My requirement is comparing String to ArrayList which contains a list of strings. Can any one suggest me?

like image 796
shiva Avatar asked Jul 07 '11 08:07

shiva


2 Answers

Look at the List#contains(T obj) method, like this:

List<String> list = new ArrayList<String>();
list.add("abc");
list.add("xyz");
list.contains("abc"); // true
list.contains("foo"); // false
like image 171
Bohemian Avatar answered Sep 30 '22 16:09

Bohemian


Use

ArrayList.contains("StringToBeChecked");

If the String is present in the ArrayList, this function will return true, else will return false.

like image 30
Logan Avatar answered Sep 30 '22 17:09

Logan