comparable

Equals and Comparable with Sets

和自甴很熟 提交于 2019-11-27 16:23:32
问题 I posted some code here which correctly solved a problem the poster had. OP wanted to remove duplicates and bring certain special items to the top of a list. I used a TreeSet with a special Comparable class which wrapped the Locale they were working with to achieve what they wanted. I then got to thinking ... as you do ... that I was eliminating duplicates by returning 0 from the compareTo method, not by returning true from an equals implementation as one would need to do to correctly

What do < and > mean such as implements Comparable<BigInteger>?

只谈情不闲聊 提交于 2019-11-27 15:40:42
In Java 1.4.2 , class java.math.BigInteger implements interfaces Comparable , Serializable . In Java 1.5.0 , class java.math.BigInteger implements interfaces Serializable , Comparable<BigInteger> . This is just an example to help me ask about < and > . What I am really wondering about is the < and > stuff. My question is threefold: what does the <BigInteger> part of the implements statement mean? what is that syntax called? and what does it do? P.S.: It's really hard to google for < and > and impossible to search SO for < and > in the first place. Thanks! Read the Java Generics Tutorial . The

What is the best way to get min and max value from a list of Comparables that main contain null values?

柔情痞子 提交于 2019-11-27 15:15:39
I am thinking about something like this: public static <T extends Comparable<T>> T minOf(T...ts){ SortedSet<T> set = new TreeSet<T>(Arrays.asList(ts)); return set.first(); } public static <T extends Comparable<T>> T maxOf(T...ts){ SortedSet<T> set = new TreeSet<T>(Arrays.asList(ts)); return set.last(); } But is not null safe, which is something I want too. Do you know a better way to solve this problem? EDIT: After the comments I have also tried min(): public static <T extends Comparable<T>> T minOf(T...ts){ return Collections.min(Arrays.asList(ts), new Comparator<T>(){ public int compare(T o1

comparing and thenComparing gives compile error

a 夏天 提交于 2019-11-27 07:43:58
问题 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])

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

ⅰ亾dé卋堺 提交于 2019-11-27 06:42:53
问题 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

Comparable classes in Python 3

冷暖自知 提交于 2019-11-27 05:50:09
问题 What is the standard way of making a class comparable in Python 3? (For example, by id.) 回答1: 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

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

依然范特西╮ 提交于 2019-11-27 04:32:03
问题 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

What does comparison being consistent with equals mean ? What can possibly happen if my class doesn't follow this principle?

核能气质少年 提交于 2019-11-27 04:15:47
From the JavaDoc of TreeMap : Note that the ordering maintained by a sorted map (whether or not an explicit comparator is provided) must be consistent with equals if this sorted map is to correctly implement the Map interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Map interface is defined in terms of the equals operation, but a map performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal. The behavior of a sorted map

Comparable and Comparator contract with regards to null

こ雲淡風輕ζ 提交于 2019-11-27 01:57:04
Comparable contract specifies that e.compareTo(null) must throw NullPointerException . From the API : Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false . On the other hand, Comparator API mentions nothing about what needs to happen when comparing null . Consider the following attempt of a generic method that takes a Comparable , and return a Comparator for it that puts null as the minimum element. static <T extends Comparable<? super T>> Comparator<T> nullComparableComparator() { return new

java.lang.Comparable and equals

老子叫甜甜 提交于 2019-11-27 01:47:57
问题 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() ? 回答1: While it is