Can I somehow tell the array.contains() method to not make the lookup case sensitive?
List<String> data = Arrays.asList(
"one", Two", "tHRee"); //lots of entries (100+)
data.contains("three");
Can I somehow tell the array.contains() method to not make the lookup case sensitive?
List<String> data = Arrays.asList(
"one", Two", "tHRee"); //lots of entries (100+)
data.contains("three");
contains
just check if an object is present in the List. So you can't do a case insensitive lookup here, because "three" is a different object than "Three".
A simple approach to solve this would be
public boolean containsCaseInsensitive(String s, List<String> l){
for (String string : l){
if (string.equalsIgnoreCase(s)){
return true;
}
}
return false;
}
and then
containsCaseInsensitive("three", data);
Java 8+ version:
public boolean containsCaseInsensitive(String s, List<String> l){
return l.stream().anyMatch(x -> x.equalsIgnoreCase(s));
}
it will require you to write your own contains()
method.
pointers for custom contains()
:
to make comparison case-insensitive be sure to apply any of toLowerCase or toUpperCase on BOTH parameter and the current element.
List list; public boolean contains(String str){
for(int i=0;i<list.size();i++){
if(list.elementAt(i).toLowerCase().Equals(str.toLowerCase())) return true;
}
return false;
}