I have tried all the answers from the related questions, like following:
Implement binary search using the `Collections.binarySearch` signature
Can't use binary search with Object Arraylist?
But none of them have worked for me.
The thing is that I want to do binarySearch()
to find an object with a particular attribute in an ArrayList
.
I am using the following code for this:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class SearchingThread extends Thread {
private String search;
private ArrayList<Vehicle> vehicles;
public SearchingThread(String search, ArrayList<Vehicle> vehicles) {
this.search = search;
this.vehicles = vehicles;
}
public void run() {
Comparator<Vehicle> comp = new Comparator<Vehicle>() {
@Override
public int compare(Vehicle o1, Vehicle o2) {
return o1.getModel().compareTo(o2.getModel());
}
};
int index = Collections.binarySearch(vehicles, search, comp);
}
}
Here search
is the variable with the model
that I want to search in the ArrayList vehicles
.
I am getting the following error for this:
The method binarySearch(List, T, Comparator) in the type Collections is not applicable for the arguments (ArrayList, String, Comparator)
I am not able to use it, can anyone help me understand the cause and solution to the error.
Edit:
Sorry for not posting this before, sorting is not an issue. I have sorted the array list accordingly beforehand.