• Post Reply Bookmark Topic Watch Topic
  • New Topic

Implementing Comparable and compareTo() method.  RSS feed

 
Joshua Twarynski
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having trouble implementing a compareTo() method.

I am creating an ArrayedHeap that stores items of type I, where I is later defined.
For example ArrayedHeap<Integer> H = new ArrayedHeap280<Integer>(10); // 10 is simply the capacity

What I fail to comprehend is that when I am creating the compareTo(I item) method I dont know what I is, seeing as it is later defined.
So how I can I implement a compareTo method when I cant use operators such as < and > or <= and >=?

When I try what I have below, eclipse tells me "The operator > is undefined for the argument type(s)
ArrayedHeap280<I>, I"

Any help would be greatly appreciated.

My code is as follows:

 
Harry Tuttle
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hello, there is no logic using > to compare two objects?! so your item should have a get-method that returns the value you want to compare.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch. Please UseCodeTags when you post source code.

So, you're creating a class that has a type parameter I, and now you want to do something with those I objects in your compareTo() method.

If you define your class like this:

then inside your class, you can't do much with objects of type I, because anything could be filled in for the I. You certainly can't compare instances of I with operators like >, because the compiler can't check that an operator > actually exists for objects of some unknown type I.

To be able to compare objects of type I, you have to specify constraints for the type parameter I. For example:

What this is expressing is: for I, you can use any type that implements the Comparable interface for I. By doing this, the compiler knows that compareTo() will always be available on I, so you can call it.

Have a look at this example:

Note: You'll never be able to use operators like < and > to compare instances of I; those operators are only defined for numeric primitive types (int, long, float, double, etc.) and cannot be used on objects.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch I have added cpde tags and you can see how much better your post looks.

The Comparable interface says it is for classes with a "natural ordering". It is obvious from the way you have written that the Item class has a "natural ordering". But what about the Heap class? How will that have a "natural ordering"? If you can't give a simple explanation, then maybe the Heap class doesn't have a "natural ordering" at all, and the Item class should implement Comparable<T>, not the Heap class.

By the way, Jesper, isn't it Heap<Comparable<T extends Comparable<? super T>>>? The compareTo method might be declared in a superclass of T.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:By the way, Jesper, isn't it Heap<Comparable<T extends Comparable<? super T>>>? The compareTo method might be declared in a superclass of T.

With java.sql.Timestamp being a good example. It (indirectly) implements Comparable<java.util.Date>, not Comparable<Timestamp>.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!