1

I have an arrayList which I need to compare against String.

What I have done:

ArrayList<String> val = new ArrayList<String>();
val= getValues();

If I print val , it gives me expected values.But

if(val.contains("abcd"))

It is returning false although at time of printing values of val it consists of abcd.

What can possibly be wrong?

Edited: How my arraylist is getting values:

IOUtils.copy(inputStream , write)
str = write.toString()

ArrayList<String> list = new ArrayList<String>();
list.addAll(Arrays.asList(str));
return list;
13
  • you need to deep compare it Commented Apr 21, 2014 at 5:13
  • does your getValues() method return a String? Commented Apr 21, 2014 at 5:15
  • No, it is returning arrayList Commented Apr 21, 2014 at 5:16
  • 1
    May be you need to trim and compare Commented Apr 21, 2014 at 5:18
  • 1
    @user3505394 focus on trim and case sensitive of the value Commented Apr 21, 2014 at 5:32

4 Answers 4

2

you need to make sure that val contains string exactly as abcd(no space, no uppercase). But if it is not case-sensitive and you allow space, then you may check it like this:

boolean isExist = false;
for(int i=0;i<val.size();i++){
    if(val.get(i).trim().toLowerCase().equals("abcd")){
        isExist=true;
        break;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

If getValues() returns an arraylist of strings, you need to ensure that the string "abcd" is exactly as given in parameter. Also since according to the docs, the contains method implements the equals method for comparison, you should make sure that the string has the right case as equals is case sensitive.

Comments

0

contain() method works by comparing elements of Arraylist on equals() method. If your arraylist has "abcd", it should return 'true'. Try checking if your getValues() method returns some hidden character/space along with "abcd".

Comments

-1

do something like below.

for(int i=0;i<val.size();i++){
    if(val.get(i).contains("abcd") || val.get(i).contains("ABCD")){
      // do some thing here
    }
}

1 Comment

may i know the reason of down-vote of my ans and up-vote for aprox same ans

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.