Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparable interface?

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would expect that the sort method in the Arrays class is defined to accept a
Comparable[] array so that the compiler can complain if anyone ever calls sort with an array
whose element type doesn’t implement the Comparable interface. Sadly, that is not the case.
Instead, the sort method accepts an Object[] array and uses a clumsy cast:
// from the standard library--not recommended
if (((Comparable) a[i]).compareTo(a[j]) > 0)
{
// rearrange a[i] and a[j]
. . .
}

P.244 - Core Java Fundamentals 8th Edition

Would you kindly clarify with an application? Why would I want to a Comparable[] interface?
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. As per current implementation:

pass a non-Comparable (doesnt implement Comparable)
> no compile error
> at runtime, , it will throw the class cast exception.

2. If done using Comparable[]

pass a non-Comparable (doesnt implement Comparable)
> compile error

===========

As seen above, it is clearer, more type-safe and less error-prone, to use Comparable[].
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesus Angeles wrote:1. As per current implementation:

pass a non-Comparable (doesnt implement Comparable)
> no compile error
> at runtime, , it will throw the class cast exception.

2. If done using Comparable[]

pass a non-Comparable (doesnt implement Comparable)
> compile error

===========

As seen above, it is clearer, more type-safe and less error-prone, to use Comparable[].


That is to say that when trying to Compare() classes which do not implement Comparable interface, you either get a (checked) exception or a compile error.
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Camilleri wrote:
That is to say that when trying to Compare() classes which do not implement Comparable interface, you either get a (checked) exception or a compile error.


Yes, an exception (during runtime, as per current api), or a compile error (if the api is what the author was suggesting - Arrays.sort(Comparable[]).

Just to add...

There is no such thing as Compare(). It is Arrays.sort().

1. Currently, the API is Arrays.sort(Object[]).

2. The author is suggesting that it would have been better if it was: Arrays.sort(Comparable[])

Example:

java.awt.Button does not implement Comparable. If you pass an array of Button to the .sort(), meaning Arrays.sort(Button[]), you get below:

1. Currently, the API is Arrays.sort(Object[]).

> no compile error
> at runtime, since you passed Button[], it will try to cast Button, into a Comparable. Thereby throwing class cast exception.

2. The author is suggesting that it would have been better if it was: Arrays.sort(Comparable[])

> since you are passing Button[] where it is required to pass Comparable[], a compile error will happen. The error will be something like "The method sort(Comparable[]) in the type Arrays is not applicable for the arguments (Button[])".

=====================================



I suggest that you study the Comparable interface.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know, Jon, you have found an inconsistency in the API.

java.util.Collections has two methods for sorting Lists:
public static <T extends Comparable<? super T>> void sort(List<T> list)
public static <T> void sort(List<T> list, Comparator<? super T> c)

Based on this I would actually expect the following four methods in java.util.Arrays:
public static <T extends Comparable<? super T>> void sort(T[] a)
public static <T extends Comparable<? super T>> void sort(T[] a, int fromIndex, int toIndex)
public static <T> void sort(T[] a, Comparator<? super T> c)
public static <T> void sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)

However, the first two don't use a method generic type but plain old Object[] instead. This has not been fixed for Java 7 (yet).

There must be a very good reason why Sun didn't do this, though. I looked in their (well, Oracle's) bug database but I couldn't find any entry about this. That means that nobody got bothered by this before.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic