0

I have the method:

<T extends Comparable<T>> T moreApproxEqual(T object, T less, T greater) {
    //TODO: return less or greater, depending on which is closer to object
    return null;
}

I have a list of T and from that I found the variables "less" and "greater" to be the closest two values in the list to the variable "object". Is there any way I can tell if one of the two objects is closer to "object" without any more information on the objects?

DeadEli
  • 983
  • 2
  • 13
  • 28
  • Since those are `Comparable` you can use `compareTo`... – Giovanni Botta May 15 '15 at 19:22
  • 2
    `compareTo` doesn't promise anything about the "closeness" of the values, though, last i checked. You can't rely on the return value to be bigger if the two objects are more dissimilar. – cHao May 15 '15 at 19:24
  • @cHao While this is true, if you are creating the classes yourself, you could technically have your `compareTo()` method return a distance (where negative => less than and positive => greater than). You should probably just create a `distance()` method for this though instead... – NoseKnowsAll May 15 '15 at 19:26
  • 1
    @NoseKnowsAll: A `distance()` method would indeed be far more appropriate. No good reason to dual-purpose `compareTo()` like that, as it would add behavior you can't rely on from any other implementation. – cHao May 15 '15 at 19:36

1 Answers1

5

No. You can tell that object is between less and greater, but in general, Comparable gives you no information about "distance" in any meaningful sense.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • This is correct. You can not tell which one is closer if you have only less or more information. – MaxZoom May 15 '15 at 19:29