comparable

Java - how to sort object in many ways: Arrays.sort(), Comparable<T>

筅森魡賤 提交于 2019-12-19 19:55:05
问题 Let's say that I have an array with objects, where I have some employees (objects). They all have: int age , double salary . I want to sort this array so my class implements Comparable <Employee> . I've made a method: public int compareTo(Employee other) { return Double.compare(salary, other.salary); } And it's ok, sorting is working fine. But I'm sorting by double salary . Now I want to sort by int age so what now? I've made a method: public int compareAge(Employee other) { return Integer

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

穿精又带淫゛_ 提交于 2019-12-18 05:48:11
问题 This question already has answers here : Closed 6 years ago . 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

Comparable and Comparator Interface in Java

一个人想着一个人 提交于 2019-12-18 05:07:07
问题 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

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

限于喜欢 提交于 2019-12-18 03:56:29
问题 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 回答1: 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

ordering a hashset example?

╄→尐↘猪︶ㄣ 提交于 2019-12-18 02:23:32
问题 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? 回答1: 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

Why is compareTo on an Enum final in Java?

 ̄綄美尐妖づ 提交于 2019-12-17 17:36:36
问题 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? 回答1: 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

Comparable and Comparator contract with regards to null

你离开我真会死。 提交于 2019-12-17 06:49:21
问题 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.

Using Comparable for multiple dynamic fields of VO in java

一个人想着一个人 提交于 2019-12-17 06:45:10
问题 I have a class public class StudentVO { int age; String name; } I used the same class in two different areas. At one place i need to sort based on the age. In another place I need to sort based on the name and in another place i may need sorting based on both age and name. How can I do this? If one field I can override compareTo() . Is it possible to do this? 回答1: 1)You should write two Comparator for sorting on age and name separately, and then use the Collections.sort(List,Comparator).

Why doesn't java.lang.Number implement Comparable? [duplicate]

我的梦境 提交于 2019-12-17 03:55:08
问题 This question already has answers here : Comparing the values of two generic Numbers (12 answers) Closed 2 years ago . Does anyone know why java.lang.Number does not implement Comparable ? This means that you cannot sort Number s with Collections.sort which seems to me a little strange. Post discussion update: Thanks for all the helpful responses. I ended up doing some more research about this topic. The simplest explanation for why java.lang.Number does not implement Comparable is rooted in

Why doesn't java.lang.Number implement Comparable? [duplicate]

≡放荡痞女 提交于 2019-12-17 03:54:09
问题 This question already has answers here : Comparing the values of two generic Numbers (12 answers) Closed 2 years ago . Does anyone know why java.lang.Number does not implement Comparable ? This means that you cannot sort Number s with Collections.sort which seems to me a little strange. Post discussion update: Thanks for all the helpful responses. I ended up doing some more research about this topic. The simplest explanation for why java.lang.Number does not implement Comparable is rooted in