14

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

3
  • 4
    What exactly do you want? You can't compare a String to an ArrayList. Be precise and show what you did until now. Commented Jul 7, 2011 at 8:38
  • 1
    What do you mean "compare"? Do you mean check if the ArrayList contains a particular String? Commented Jul 7, 2011 at 8:39
  • ya i want to check one sting whether it is present in ArrayList or not? in java Commented Jul 7, 2011 at 8:44

7 Answers 7

22

Use

ArrayList.contains("StringToBeChecked");

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

Sign up to request clarification or add additional context in comments.

3 Comments

just curious about capital A - the first letter in ArrayList.contains("StringToBeChecked");
I didn't give the exact name of the variable. Gave more of an idea of what can be done. In actual scenario, instead of ArrayList, use your variable of type ArrayList.
there is a difference between ArrayList and arrayList isn't it?
8

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

1 Comment

Arrays.asList("abc","xyz").contains("abc");
8

This is your method:

   private boolean containsString(String testString, ArrayList<String> list) 
   {
        return list.contains(testString);
   }

Comments

4

There are different options you could consider :

  • a simple for each loop using equals on each member of your list to compare it to string A.
  • sorting all strings in list in order to boost up comparison and find if a string in the list matches A.
  • using a sortedSet to achieve the above
  • putting alls string in the intern string pool using intern method on every item in the list and on string A. This will allow to compare strings using == and not equal anymore. Comparison would then be much faster.
  • using a hashmap to achieve better comparison speeds
  • an identity hashmap to mix the two preceding strategies.

Well, the are pros and cons to those methods, it all depends on what you want to do and why and how you compare strings.

Regards, Stéphane

Comments

4

ArrayList can contain one or more Strings. You cannot compare String with ArrayList. But you can check whether the ArrayList contains that String ot not using contains() method

String str = //the string which you want to compare
ArrayList myArray =// my array list
boolean isStringExists = myArray.contains(str);// returns true if the array list contains string value as specified by user

Comments

3

If you are willing to use Lambdaj, then check the presence of the String as:

private boolean isContains(ArrayList<String> listOfStrings, String testString) {
    return (Lambda.select(listOfStrings, Matchers.equalTo(testString)).size() != 0);  
}

Using static import of select and equalTo increase the readability:

select(listOfStrings, equalTo(testString));

Comments

0

Convert your Array to Array list first

List al = Arrays.asList("Your Array");

Then compare using contains or equals

if (al.contains("Your String") {
    // Code
}

1 Comment

Please add an explanation

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.