Your question doesn't make much sense.
Binary search works on sorted collection, so of course your element have to be comparable. Define your compareTo
and equals
methods to consider only the surname
field, and then you can use binarySearch
.
EDIT: I'm still not sure whether you are asking about the usage of the library function binarySearch
or about the implementation of a custom binary search function.
For the first case the answer is no, there is no such overloading of binarySearch
in the API. Generally in an array you want to search by entity equality, because in the intended use case of this method you already have the entity you are searching for, but you don't know if it is contained in the target array, and on which index can be found. However you want to search an entity by a key, which might sign that you are misusing ArrayList
and binarySearch
; a SortedMap
would fit better for this task.
On the other hand, if you stick to ArrayList
, than of course you can implement yourself a method like binary search, which uses only your surname
field for matching.