[英]How to use Collections.binarySearch for ArrayList<List<String>> arr in java
I have an ArrayList<List<String>>
and im trying to use the built-in binary search in the collection but I always come up with an error我有一个
ArrayList<List<String>>
并且我正在尝试在集合中使用内置的二进制搜索,但我总是会遇到错误
int index = Collections.binarySearch(arraylistdata, id);
Where arraylistdata is my ArrayList
of List<String>
.其中arraylistdata是我的
List<String>
的ArrayList
。
public class CollectionsTest {
@Test
void test() {
List<String> list = List.of("a", "b", "c");
int index = Collections.binarySearch(list, "b");
Assertions.assertEquals(1, index);
}
}
This works fine, but you need to known that Collections#binarySearch
accept a List<? extends Comparable<? super T>>
这工作正常,但你需要知道
Collections#binarySearch
接受List<? extends Comparable<? super T>>
List<? extends Comparable<? super T>>
List<? extends Comparable<? super T>>
as its first parameter. List<? extends Comparable<? super T>>
作为它的第一个参数。
While in your case, ArrayList<List<String>>
does not match the requirement, as List<String>
or List<?>
itself is not a Comparable
object.在您的情况下,
ArrayList<List<String>>
不符合要求,因为List<String>
或List<?>
本身不是Comparable
object。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.