I'm trying to perform a binary search with a case-insensitive comparator, but I keep getting errors whatever I try...
Trial 1:
Arrays.binarySearch(arr, "text", String::compareToIgnoreCase);
Source: https://stackoverflow.com/a/30191946/900394
This gives an error:
Method references are not supported at language level '7'
Assuming I want to work with Java 7, I tried these additional methods:
Trial 2:
Comparator<String> caseInsensitiveComparator = new Comparator<String>() {
@Override
public int compare(String s, String t1) {
return s.compareToIgnoreCase(t1);
}
};
Arrays.binarySearch(arr, "text", caseInsensitiveComparator);
Source: https://stackoverflow.com/a/14154185/900394
Error:
Required type: Comparator<? super Object>
Provided: Comparator
Note that this method is working for sorting a list (i.e. this comparator is suitable for Collections.sort()
)
Trial 3:
Arrays.binarySearch(arr, "text", String.CASE_INSENSITIVE_ORDER);
Source: https://stackoverflow.com/a/17491652/900394
Error:
Required type: Comparator<? super Object>
Provided: Comparator
Note that this method is also working on Collections.sort()
Trial 4:
Build a class that implements Comparator<String>
:
public class CaseInsensitiveComparatorClass implements Comparator<String> {
@Override
public int compare(String s, String t1) {
return s.compareToIgnoreCase(t1);
}
}
Arrays.binarySearch(arr, "text", new CaseInsensitiveComparatorClass ());
Source: https://stackoverflow.com/a/30191797/900394
Error:
Required type: Comparator<? super java.lang.Object>
Provided: CaseInsensitiveComparatorClass
Note that this method is also working on Collections.sort()
Can you provide me with one method that does work, or point out what I'm doing wrong?
EDIT:
This is the declaration of arr
:
private List<String> lst;
So it's actually a list, which I convert to an array on the method invocation:
Arrays.binarySearch(lst.toArray(),...