public class BinarySearchCollections {
public static void search(List<String> list) {
list.clear();
list.add("b");
list.add("a");
list.add("c");
System.out.println(Collections.binarySearch(list, "b"));
System.out.println(list);
}
public static void main(String[] args) {
List<String> lis = new ArrayList<String>();
BinarySearchCollections bs = new BinarySearchCollections();
bs.search(lis);
}
}
Here I am getting ans as -3 (as it is telling me at what location it is going to be added) but I already have b in my list.