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
int index = Collections.binarySearch(arraylistdata, id);
Where arraylistdata is my ArrayList
of List<String>
.
CodePudding user response:
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>>
as its first parameter.
While in your case, ArrayList<List<String>>
does not match the requirement , as List<String>
or List<?>
itself is not a Comparable
object.