Questions tagged [comparable]

In Java, this interface is implemented by a class to indicate that it can be compared to another object and therefore ordered. In Go, comparable is a predeclared interface constraint for generic types that support == and != operators. Make sure to use the appropriate language tag along with this.

In Java, this interface is implemented by a class to indicate that it can be compared to another object and therefore ordered. The interface defines one method: compareTo(). Classes can either implement it as a raw type, as with older versions of Java:

public class MyClass implements Comparable
{
    public int compareTo(Object other)
    {
        MyClass toCompare = (MyClass) other;
        //...
    }
}

Or, as of Java 1.5, a class can use generics:

public class MyClass implements Comparable<MyClass>
{
    public int compareTo(MyClass other)
    {
        //...
    }
}

The general contract of compareTo() is:

  • If this is less than the parameter, it returns a value less than zero.
  • If this is greater than the parameter, it returns a value greater than zero.
  • Otherwise, if this is equal to the parameter, returns zero.

The actual value of compareTo() is irrelevant; only the sign of the return value matters.

Javadoc for java.lang.Comparable


In Go, comparable is a predeclared interface constraint. It can be used directly or it can be embedded in other interface constraints. It cannot be used in union terms. Specifications: Type Constraints:

The predeclared interface type comparable denotes the set of all concrete (non-interface) types that are comparable. Specifically, a type T implements comparable if:

  • T is not an interface type and T supports the operations == and !=; or
  • T is an interface type and each type in T's type set implements comparable.

Example usage:

func IsZero[T comparable](v T) bool {
    return v == *new(T)
}
1251 questions
4
votes
1 answer

Implement remove(Object o) in generic collection

I am writing a generic collection based on a binary tree model. class MyTree > extends AbstractCollection{...} The underlying Node class (among others) contains the following methods: public Node getLeft() // left…
Niklas Mertsch
  • 1,399
  • 12
  • 24
4
votes
1 answer

How to minimally reconcile Scala's @specialized and java.lang.Comparable?

I have a third party Java API with methods like this (simplified) : public > C jFoo(C c); I want to write a wrapper like this: def foo[C <: Comparable[C]](c: C): C = jFoo(c) For allowing the wrapper method to accept…
mikołak
  • 9,605
  • 1
  • 48
  • 70
4
votes
3 answers

How to change the order of compareBy in Kotlin

I need a comparator, in Kotlin, to order every object that enter in my recycler view (using Fast Adapter). I need to order the objects by an Integer, where the biggest ones come first. The code above order the object that enters in my list, but the…
Guilherme Lima Pereira
  • 1,402
  • 5
  • 17
  • 35
4
votes
6 answers

Writing a comparable for an Arrays.Sort of Points

I have this code : import java.awt.Point; import java.util.Arrays; public class Haupt { // creates an array of anz Point objects public static Point[] generatePointArray(int anz) { Point[] pa = new Point[anz]; for(int i =…
Altin
  • 41
  • 1
  • 2
4
votes
2 answers

Comparable interface depends upon size?

I was working on Comparable interface and found that it is throwing IllegalArgumentException when i put size=50 in the program below and is working fine when i put size =5. public class Test { public static void main(String[] args) { …
T-Bag
  • 10,916
  • 3
  • 54
  • 118
4
votes
1 answer

How does Comparator.comparing() function work?

For the code below: ForComparatorDemo object1 = new ForComparatorDemo("object-1",5); ForComparatorDemo object2 = new ForComparatorDemo("object-2",4); ForComparatorDemo object3 = new ForComparatorDemo("object-3",3); ForComparatorDemo object4 = new…
Milind Vinkar
  • 159
  • 1
  • 9
4
votes
3 answers

Java overriding compareTo with exception handling

Assume I have this class: public abstract class GraphEdge implements Comparable { public abstract double getLength() throws DependencyFailureException; @Override public int compareTo(Object obj) { return…
jamesdeath123
  • 4,268
  • 11
  • 52
  • 93
4
votes
4 answers

Why Java TreeMap not printing all its inserted entries

Pls tell what is wrong is happening here. I have a Person class which I'm using as a key in TreeMap. I have implemented Comparable also so that TreeMap can do sorting. public class Person implements Comparable{ private String name; private int…
user3626306
  • 137
  • 2
  • 2
  • 9
4
votes
1 answer

Java Comparator, Comparable and TreeSet.contains

edit - slightly simplified example below (any simpler and i'm not sure it contains all of the elements of the desired behavior) Below is a code snippet that represents one thing I'm trying to do with Comparator. I would like for contains to return…
mikesol
  • 1,177
  • 1
  • 11
  • 20
4
votes
4 answers

Inheriting Comparable with generics

Trying to design a superclass that ensures all sub-classes are inherently Comparable. /** * A base class implementing Comparable with itself by delegation. * @param - The type being wrapped. */ static class Distinct>…
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
4
votes
2 answers

Generic Issue in LocalDate

I have the following code. I was expecting Arrays.asList(LocalDate.of(2014, 1, 1), LocalDate.of(2015, 2, 2)) to convert into the list of LocalDate. But I am getting compile time error in max(dates). What might be the cause of this error? import…
Pradip Kharbuja
  • 3,442
  • 6
  • 29
  • 50
4
votes
3 answers

Comparable vs Raw Comparable

I was recently writing a simple generic selection sort method for the fun of it, when I got a little confused about something. Here's my code: public static > void sort(List list) { for (int i = 0; i < list.size();…
Decoy
  • 317
  • 1
  • 4
  • 7
4
votes
2 answers

TreeSet contains/remove not working?

I am keeping Node objects in a TreeSet: public TreeSet viewNodes = new TreeSet(); Node looks like this: public class Node implements Comparable{ private long nodeID; ... public long getID() { return nodeID; …
Casey
  • 41
  • 4
4
votes
5 answers

Make java class Comparable to 2 different Classes

I would like to implement the Comparable Interface "Twice" public class Segment implements Comparable{ @Override public int compareTo(Segment o) { return 0; } @Override public int compareTo(Point p) { return 0; } Is it…
4
votes
4 answers

Why doesn't invalid compareTo cause Collections.sort to crash?

Consider the following compareTo method, implementing the Comparable interface.: @Override public int compareTo(MyObject o) { if (o.value.equals(value) return 0; return 1; } Apparantly, the programmer implemented the compareTo as…
wvdz
  • 16,251
  • 4
  • 53
  • 90