71

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;
3
  • Why not just set temp to be 2 at the begining? And why use object Integer instead of int? Solution looks good enough if you change what i said Commented Apr 18, 2012 at 11:49
  • What you have written already is a working solution, except that you are missing the else statement. Also notice that comparing strings is case sensitive which might be an issue if you didn't get it to work properly. Also, using contains like suggested is another valid approach Commented Apr 18, 2012 at 12:01
  • @JimmyGustafsson : yes man i was doin that only but i got confused... anyway thanks Commented Apr 19, 2012 at 4:09

4 Answers 4

101
temp = bankAccNos.contains(no) ? 1 : 2;
Sign up to request clarification or add additional context in comments.

2 Comments

The List.contains() method checks for identity, not equality. It works well for integers but not strings (which is what OP asked for). Iterating over the list, or better, using Java8 List stream is the way to go for List<String>
@Michal the documentation states otherwise docs.oracle.com/javase/8/docs/api/java/util/… It checks for equality, not identity: " returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e))"
16

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.

3 Comments

The List.contains() method checks for identity, not equality. It works well for integers but not strings (which is what OP asked for). Iterating over the list, or better, using Java8 List stream is the way to go for List<String>
@Michal Not true as the documentation says otherwise (and I've just tested). Hit the link in my answer and read for yourself.
More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)). So it's equality not identity.
7
    List list1 = new ArrayList();
    list1.add("one");
    list1.add("three");
    list1.add("four");

    List list2 = new ArrayList();
    list2.add("one");
    list2.add("two");
    list2.add("three");
    list2.add("four");
    list2.add("five");


    list2.stream().filter( x -> !list1.contains(x) ).forEach(x -> System.out.println(x));

The output is:

two
five

Comments

0
*public class Demo {
   public static void main(String[] args) {
      List aList = new ArrayList();
      aList.add("A");
      aList.add("B");
      aList.add("C");
      aList.add("D");
      aList.add("E");
      if(aList.contains("C"))
         System.out.println("The element C is available in the ArrayList");
      else
         System.out.println("The element C is not available in the ArrayList");
      if(aList.contains("H"))
         System.out.println("The element H is available in the ArrayList");
      else
         System.out.println("The element H is not available in the ArrayList");
   }
}*

The Output will be:

The element C is available in the ArrayList The element H is not available in the ArrayList

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.