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
By using compareToIgnoreCase, http://docs.oracle.com/javase/1.3/docs/api/java/lang/String.html#compareToIgnoreCase%28java.lang.String%29 you should be able to do what you want!
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;
}
}
Another solution:
public class IgnorecaseList extends ArrayList<String>{
@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.
Traditionally, you can develop your own logic to compare strings held by an ArrayList. There may be several ways to do so like the one shown below.
public boolean containsCaseInsensitive(String strToCompare, ArrayList<String>list)
{
for(String str:list)
{
if(str.equalsIgnoreCase(strToCompare))
{
return(true);
}
}
return(false);
}
Why shouldn't be used some direct and convenient ways like a SortedSet as shown below with a case insensitive comparator?.
Set<String> a = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
a.add("A");
a.add("B");
a.add("C");
Set<String> b = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
b.add("a");
b.add("b");
b.add("c");
System.out.println(b.equals(a));
Would compare two different sets ignoring case and return true
, in this particular situation and your comparision would work without any issue.
In my case, as all my strings in the ArrayList are in downcase, I just execute the String.toLowerCase() method on the contains() parameter. Like this:
If (yourArrayList.contains (parameterInput.toLowerCase()) {
// your code here
}
As you can see, you can do the oposite, if yout arrayList has upperCase strings:
If (yourArrayList.contains (parameterInput.toUpperCase ()) {
// your code here
}
Using this approach, you do not need to override anything. The exception is in the case when your arrayList have a mix of upper and lower cases.
Its a best way to convert your list item into lowercase. After convert you will use contain method. like
List<String> name_list = new ArrayList<>();
name_list.add("A");
name_list.add("B");
Create lowercase list using above created name_list
List<String> name_lowercase_list = new ArrayList<>();
for(int i =0 ; i<name_list.size(); i++){
name_lowercase_list.add(name_list.get(i).toLowerCase().toString());
}
for(int i =0 ; i<name_list.size(); i++){
String lower_case_name = name_list.get(i).toLowerCase().toString();
if(name_list.get(i).contains(your compare item) ||
name_lowercase_list.get(i).contains(your compare item) ){
//this will return true
}
}