Hi i have the arraylist it stores the string values. I want to check the some string is exists in the list or not. Here i want ignore the case sensitive.
code
public static ArrayList< String > arrFoodItems = new ArrayList< String >();
if(arrFoodItems.contains("string"))
{
System.out.println("already available in the List.");
}
else
{
System.out.println("Not available");
}
It’s working fine. But in the following example it fails. How to do it without using loops.
Example: List contains: “Rice, Chicken,…”
Now you are check for “rice” is exits in the list. In this case we are getting “not exists”.
Don’t using this
for(int i=0; i < arrFoodItems.size(); i++)
{
if(arrFoodItems.get(i)..equalsIgnoreCase(string))
{
System.out.println("already available in the List.");
}
}
You could put all your strings in a
TreeSetwith a custom Comparator that ignores the case:For more complex strategies / locale, you can also use a Collator: