6

The method

public static int binarySearch(Object[] a, Object key) 

of the Arrays class in its implementation navigates through the array argument a following the binarySearch algorithm and converts the elements of a into Comparable and invokes compareTo(key) until it either finds a match or runs out of possibilities.

I'm stumped by the implementation however, if it's the case that the method will always cast the elements to Comparable, and furthermore will throw a ClassCastException if it encounters an element that does not implement Comparable, would it not be clearer for the API user that the method will take into account the comparator of the elements of the array only and not the comparator of the key, be more foolproof by preventing compilation where an invocation was made where the type of the array was not compatible with Comparable, and also execute more efficiently if the method were defined as

public static int binarySearch(Comparable[] a, Object key) 

? What are the advantages to defining the first argument as an array of Object?

EDIT I only saw this after I posted the question and it had been answered but there is a related post here: Why does Arrays.sort take Object[] rather than Comparable[]? where they state that if the method took the parameters (Comparable[], Object) it would not be possible to pass an array of type Object[] to that method without "reallocation" which is also expensive.

Community
  • 1
  • 1
Mishax
  • 4,442
  • 5
  • 39
  • 63

3 Answers3

2

I think the best declaration would actually be a generic one:

public static <T extends Comparable<? super T>> int binarySearch(T[] a, T key)

My guess is that, simply, this method was created in an early Java version using Objects and could not be changed for reasons of backwards compatibility. This has happened in various other places as well: arrays with their toString() and Cloneable are two noteworthy examples.

arshajii
  • 127,459
  • 24
  • 238
  • 287
0

Perhaps it would be good if the method was implemented as

public static int binarySearch(Comparable[] a, Comparable key)

The key has to be a Comparable though, because otherwise how would you know when you found the element you're looking for?

I think it boils down to the idea that it's only logical that everything needs to be Comparable in this situation.

Millie Smith
  • 4,536
  • 2
  • 24
  • 60
  • You've perhaps unintentionally proved my point about it not being intuitive. The key does not have to be Comparable. Only the compareTo method of the elements of the array are executed by the implementation. The key is never queried. – Mishax Oct 12 '13 at 15:38
  • Unless you're implicitly checking that you've found it via a hashCode, how do you not need the key to be a Comparable? – Millie Smith Oct 12 '13 at 15:39
  • It invokes compareTo key, so key pretty much has to be Comparable. It should be the same Comparable type that is calling compareTo most likely. – Millie Smith Oct 12 '13 at 15:41
  • You are right about the "most likely". However the comparators of the elements of the array are being executed and the method executed is compareTo and the signature is compareTo(T o) not compareTo(Comparable c). You could create a Comparable class WhizBang that can compare itself with a Book, a CelestialBeing, a Boolean, a BeyondComparable, whatever you want. – Mishax Oct 12 '13 at 15:45
  • Correct, but they don't have to have an is-a relationship to to be Comparable. Suppose you had an array of Books and a String key for an author's name, and you wanted to know where in the array of books you could find the books by Millie Smith. A String is not a book, and a String is final and while it is comparable, it only knows how to compare itself to another String. You could do the same with a Color, which is not Comparable, and do a BinarySearch in your Books for where the blue Books started. – Mishax Oct 12 '13 at 16:13
0

See Why does Arrays.sort take Object[] rather than Comparable[]?. The issue is that this would cause problems for arrays that were declared as type Object[] even if they contained only Comparables.

Community
  • 1
  • 1
Mishax
  • 4,442
  • 5
  • 39
  • 63