Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList contains case sensitivity

Tags:

java

arraylist

I am currently using the contains method belonging to the ArrayList class for making a search. Is there a way to make this search case insensitive in java? I found that in C# it is possible to use OrdinalIgnoreCase. Is there a java equivalent, or another way to do this? Thanks.

like image 349
John Avatar asked Jan 05 '12 23:01

John


People also ask

Does ArrayList contain case sensitive?

If you also need the String back in the original case, contains() will only help to indicate that it is present in the list. A HashMap could retrieve the original string given a uppercase or lowercase string, and would have a much better search characteristic (but it would not retain the original order of the strings).

Is list contains method case sensitive in Java?

In this example, we will show you how to check HashSet contains element case insensitive in Java. contains() method of Collection interface returns true if this set contains the specified element. But the problem is contains() method only check the equality of element (case sensitive).

How do you make an ArrayList case-insensitive?

The solution is not to make the elements case-insensitive (which technically would mean reimplementing String--one cannot extend it because it is final--with a wrapper class whose equals and compareTo methods are case-insensitive), but rather to make the comparison case-insensitive.

Is set contains case sensitive?

HashSet's contains() method is case sensitive and does not allow the use of comparators. We could use TreeSet instead of HashSet which allow Comparator thus facilitating case-insensitive search and comparison. Using the comparator String. CASE_INSENSITIVE_ORDER we could perform case ignored search.


1 Answers

You can use this exactly like you'd use any other ArrayList. You can pass this List out to other code, and external code won't have to understand any string wrapper classes.

public class CustomStringList3 extends ArrayList<String> {     @Override     public boolean contains(Object o) {         String paramStr = (String)o;         for (String s : this) {             if (paramStr.equalsIgnoreCase(s)) return true;         }         return false;     } } 
like image 161
Aaron J Lang Avatar answered Sep 24 '22 02:09

Aaron J Lang