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
Another solution:
public class IgnorecaseList extends ArrayList{
@Override
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
@Override
public int indexOf(Object o) {
if(o instanceof String){
for (int i = 0; i < this.size(); i++) {
if(((String)o).equalsIgnoreCase(get(i))){
return i;
}
}
}
return -1;
}
}
contains()
method uses indexOf... In this sollution you can also know in where position is the string. list.add("a")
-> list.indexOf("A") == 0
or list.indexOf("a") == 0
..
You should also consider using a Set instead of List.