comparable

Create a compareTo to a Generic Class that Implements Comparable

别来无恙 提交于 2019-11-29 11:50:43
问题 I have a Generic Class with two type variables, which implements java.lang.Comparable. public class DoubleKey<K,J> implements Comparable<DoubleKey<K,J>>{ private K key1; private J key2; public DoubleKey(K key1, J key2){ this.key1 = key1; this.key2 = key2; } public K getFirstKey(){ return this.key1; } public J getSecondKey(){ return this.key2; } // need for Comparable interface public int compareTo(DoubleKey<K,J> aThat){ ... } } Becuase i implemeted it with Comparable, I need to write the

Java “cannot cast to Comparable” when using TreeMap [duplicate]

给你一囗甜甜゛ 提交于 2019-11-29 10:05:50
Possible Duplicate: Java: SortedMap, TreeMap, Comparable? How to use? I am using the Java JungI graph package and Netbeans 7. I am getting the following error from Java: Exception in thread "main" java.lang.ClassCastException: graphvisualization.MyVertex cannot be cast to java.lang.Comparable at java.util.TreeMap.put(TreeMap.java:542) Here is the code associated with the error: SortedMap<MyVertex, Double> vMap = new TreeMap<MyVertex, Double>(); double curRank = 0; for(MyVertex v: g.getVertices()) //g is a SparseGraph<MyVertex, MyEdge> { curRank = vertexRank.getVertexScore(v); vMap.put(v,

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

ぐ巨炮叔叔 提交于 2019-11-29 09:20:11
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 one type for parameter a, and another for parameter b, so that they can't be compared. How do I dig

Comparable and Comparator Interface in Java

房东的猫 提交于 2019-11-29 07:52:22
I want to write a generic Pair class, which has two members: key and value. The only requirement to this class is that both key and value should implements the Comparable interface, otherwise Pair class will not accept them as type parameter. First I code it like this: public class Pair<T1 extends Comparable, T2 extends Comparable> But the JDK 1.6 compiler will generate warning about this: Comparable is a raw type. References to generic type Comparable<T> should be parameterized Then I tried to add type parameters and the code now looks like this: public class Pair<T1 extends Comparable<?

Java, Why it is implied that objects are equal if compareTo() returns 0?

孤者浪人 提交于 2019-11-29 05:34:00
Let's have a class Person . Person has a name and height. Equals and hashCode() takes into account only name. Person is comparable (or we implement comparator for it, does not matter which one). Persons are compared by height. It seems reasonable to expect a situation where two different persons can have same height, but eg. TreeSet behaves like comapareTo()==0 means equals, not merely same size. To avoid this, comparison can secondarily look at something else if size is the same, but then it cannot be used to detect same sized different objects. Example: import java.util.Comparator; import

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

痴心易碎 提交于 2019-11-29 03:38:18
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 horrible way I'm trying to give the class some flexibility with all the different method signatures for

Why does Arrays.sort take Object[] rather than Comparable[]?

断了今生、忘了曾经 提交于 2019-11-29 03:08:58
I was wondering why the sort method of the Arrays class is asking for a parameter of type Object[]. Why the parameter is not of type Comparable[]. If you don't pass a Comparable[] it's generating a ClassCastException. Why ... public static void sort(Object[] a) and not public static void sort(Comparable[] a) ? Thanks Because the second form would require a reallocation of the array. Even if you know that your array contains only comparables, you cannot just cast it to Comparable[] if the original type was Object[], since the array type does not match. You can do: Object[] arr = new String[0];

Equals and Comparable with Sets

烈酒焚心 提交于 2019-11-29 02:01:14
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 indicate a duplicate in a Set (from the definition of a Set ). I have no objection to using this technique but

ordering a hashset example?

删除回忆录丶 提交于 2019-11-28 23:10:47
I need an example on how to use a comparable class on a HashSet to get an ascending order. Let’s say I have a HashSet like this one: HashSet<String> hs = new HashSet<String>(); How can I get hs to be in ascending order? Use a TreeSet instead. It has a constructor taking a Comparator . It will automatically sort the Set . If you want to convert a HashSet to a TreeSet , then do so: Set<YourObject> hashSet = getItSomehow(); Set<YourObject> treeSet = new TreeSet<YourObject>(new YourComparator()); treeSet.addAll(hashSet); // Now it's sorted based on the logic as implemented in YourComparator. If

Does a natural comparator exist in the standard api?

半腔热情 提交于 2019-11-28 22:39:51
问题 I need a comparator as part of a strategy pattern that can either use the natural ordering of the objects or some custom ordering. For the natural ordering case, I wrote a simple comparator: private static class NaturalComparator<T extends Comparable<? super T>> implements Comparator<T> { @Override public int compare(T o1, T o2) { return o1.compareTo(o2); } } Seems simple enough, but I was wondering if anyone knew of one in the standard API. I looked at TreeMap, and it does it without such a