[英]How to use `Collections.binarySearch()` to do a binary search though an ArrayList of objects?
I have tried all the answers from the related questions, like following: 我已经尝试过所有相关问题的答案,例如:
Implement binary search using the `Collections.binarySearch` signature 使用`Collections.binarySearch`签名实现二进制搜索
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
. 问题是我想执行binarySearch()
以在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
. 在这里, search
是要在ArrayList vehicles
搜索的model
的变量。
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) 类型Collections中的方法binarySearch(List,T,Comparator)不适用于参数(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. 我已经事先对数组列表进行了排序。
Collections#binarySearch
searches a List
for the same type of value that the list holds. Collections#binarySearch
在List
搜索与List
具有相同类型的值。 Here, you're trying to search a list of vehicles with a string, and thus getting the error your shared. 在这里,您尝试搜索带有字符串的车辆列表,从而得到共享的错误。
One approach is to create a fake vehicle just so its model can be search: 一种方法是创建假冒车辆,以便可以搜索其模型:
Vehicle modelDummy = new Vehicle();
modelDummy.setModel(search);
int index = Collections.binarySearch(vehicles, modelDummy, comp);
Note that in order to use binarySearch
like that the list must be sorted according to the Comparator
you provides (ie, sorted according to the model in this case). 请注意,为了像使用binarySearch
一样,必须根据您提供的Comparator
对列表进行排序(即,在这种情况下,根据模型进行排序)。 If this assumption is not true, you's have to use an O(n) search. 如果此假设不成立,则必须使用O(n)搜索。 Eg: 例如:
Vehicle vehicle = vehicles.stream().filter(v -> v.getModel().eqauls(search)).findFirst();
Create a view of the list: 创建列表视图:
List<String> view = new AbstractList<String>() {
@Override public int size() {
return vehicles.size();
}
@Override public String get(int i) {
return vehicles.get(i).getModel();
}
};
Then apply binary search to the view. 然后将二进制搜索应用于视图。
int index = Collections.binarySearch(view, search);
Note that whilst it would work using an anonymous class, like this, it would be better to define a named class (eg a nested class, or even a local class), in order that it can both extend AbstractList
and implement RandomAccess
, so that the binary search knows it can access efficiently by index. 请注意,虽然它可以使用匿名类来工作,但最好定义一个命名类(例如,嵌套类,甚至是本地类),以便它既可以extend AbstractList
并implement RandomAccess
,二进制搜索知道它可以通过索引有效访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.