comparable

What do the return values of Comparable.compareTo mean in Java?

China☆狼群 提交于 2019-11-28 15:42:36
What is the difference between returning 0 , returning 1 and returning -1 in compareTo() in Java? Official Definition From the reference docs of Comparable.compareTo(T) : Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception iff y.compareTo(x) throws an exception.) The implementor must also ensure that the relation

comparing and thenComparing gives compile error

情到浓时终转凉″ 提交于 2019-11-28 13:31:53
I am trying to sort List of employees by name then age using Java8 Comparator , I have created below Comparator but it gives me a compiler error Type mismatch: cannot convert from Comparator<Object> to <unknown> Comparator<String> c = Comparator.comparing(s -> s.split("\\s+")[0]) .thenComparingInt(s -> Integer.parseInt(s.split("\\s+")[1])); //compile error but it works if I explicitly specify the Type Comparator<String> c = Comparator.<String, String> comparing(s -> s.split("\\s+")[0]) .thenComparingInt(s -> Integer.parseInt(s.split("\\s+")[1])); //works or by creating two Compartor s and

Java error: “Comparison method violates its general contract!”

只愿长相守 提交于 2019-11-28 12:03:51
I have this code: package org.optimization.geneticAlgorithm; import org.optimization.geneticAlgorithm.selection.Pair; public abstract class Chromosome implements Comparable<Chromosome> { public abstract double fitness(); public abstract Pair<Chromosome> crossover(Chromosome parent); public abstract void mutation(); public int compareTo(Chromosome o) { int rv = 0; if (this.fitness() > o.fitness()) { rv = -1; } else if (this.fitness() < o.fitness()) { rv = 1; } return rv; } } And every time I run this code I get this error: Exception in thread "main" java.lang.IllegalArgumentException:

Comparable classes in Python 3

牧云@^-^@ 提交于 2019-11-28 10:11:55
What is the standard way of making a class comparable in Python 3? (For example, by id.) For a full set of comparison functions I have used the following mixin, which you could put in say for example a mixin.py in your module. class ComparableMixin(object): def _compare(self, other, method): try: return method(self._cmpkey(), other._cmpkey()) except (AttributeError, TypeError): # _cmpkey not implemented, or return different type, # so I can't compare with "other". return NotImplemented def __lt__(self, other): return self._compare(other, lambda s, o: s < o) def __le__(self, other): return self

java.lang.Comparable and equals

懵懂的女人 提交于 2019-11-28 08:59:09
If I implement java.lang.Comparable for a class, do I still have to override the equals() method? Or will the Comparable work for equals as well? If the answer is no , then what if some discrepancy arises? Let's say the way I term two objects as equal within the equals() method is different from the way I term two objects of the same class as equal within the compareTo() of the Comparable . Moreover, if I implement Comparable , do I also have to override equals() ? While it is recommended (and pretty sensible) that having a.compareTo(b) == 0 imply that a.equals(b) (and visa versa), it is not

How does the compareTo() method, compare strings? [closed]

不打扰是莪最后的温柔 提交于 2019-11-28 05:19:31
问题 Such as if I were to compare the Strings "Hello" and "World". How does it know Hello is greater than World? The only thing I can come up with is, maybe it uses the ASCII Table as reference? Thanks for the help! 回答1: it compares two strings lexographically . check here in the String API. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one

Why is compareTo on an Enum final in Java?

拟墨画扇 提交于 2019-11-28 04:26:51
An enum in Java implements the Comparable interface. It would have been nice to override Comparable 's compareTo method, but here it's marked as final. The default natural order on Enum 's compareTo is the listed order. Does anyone know why a Java enums have this restriction? For consistency I guess... when you see an enum type, you know for a fact that its natural ordering is the order in which the constants are declared. To workaround this, you can easily create your own Comparator<MyEnum> and use it whenever you need a different ordering: enum MyEnum { DOG("woof"), CAT("meow"); String sound

How to implement a generic `max(Comparable a, Comparable b)` function in Java?

假装没事ソ 提交于 2019-11-28 02:44:52
问题 I'm trying to write a generic max function that takes two Comparable s. So far I have public static <T extends Comparable<?>> T max(T a, T b) { if (a == null) { if (b == null) return a; else return b; } if (b == null) return a; return a.compareTo(b) > 0 ? a : b; } This fails to compiles with The method compareTo(capture#5-of ?) in the type Comparable<capture#5-of ?> is not applicable for the arguments (T) What I think this is saying is that that the ? in Comparable<?> may be interpreted as

Why does the Java Collections Framework offer two different ways to sort?

我只是一个虾纸丫 提交于 2019-11-27 21:41:42
If I have a list of elements I would like to sort, Java offers two ways to go about this. For example, lets say I have a list of Movie objects and I’d like to sort them by title. One way I could do this is by calling the one-argument version of the static java.util.Collections.sort( ) method with my movie list as the single argument. So I would call Collections.sort(myMovieList). In order for this to work, the Movie class would have to be declared to implement the java.lang.Comparable interface, and the required method compareTo( ) would have to be implemented inside this class. Another way to

Implementing Comparable, compareTo name clash: “have the same erasure, yet neither overrides the other”

穿精又带淫゛_ 提交于 2019-11-27 17:40:55
问题 I'd like to have a compareTo method that takes a Real (a class for working with arbitrarily large and precise real numbers [well, as long as it's less than 2^31 in length at the moment]) and a compareTo method that takes an Object, but Java isn't letting me and I'm not experienced enough to know why. I just tried to modify the class to implement Comparable and I got these error messages below. I don't really understand what the error messages mean but I know it's got something to do with the