comparable

best way for get min and max value from a list of Comparables in java

I think in 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 s...

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

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 doe...

Why doesn't java.lang.Number implement Comparable?

Does anyone know why java.lang.Number does not implement Comparable? This means that you cannot sort Numbers 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 do...

Why is compareTo on an Enum final in Java?

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 Enum has this restriction? ...

Improving Comparable<T> compareTo performance

I profiled my code and found out that my class, which implements Comparable<T>, spends 8x more cpu time in compareTo(Object) than in compareTo(T) I assume that the slowdown is because of virtual table lookup for this method. Is there a way to force static invocation of the function? (like in non virtual C++ methods) I still wa...

Java Generics and Infinity (Comparable)

With the type Integer you can do this: int lowest = Integer.MIN_VALUE; What can I do if I use generics? K lowest = <...>; I need this in order to implement something similar to a PriorityQueue. I have access to a node I want to remove from the queue, but it is not the min. 1. I need to make it the min by decreasing the key of that...

In java what does extending from a comparable mean

I see code like this class A implements Comparable<A> { } What does this mean, what are the advantages and disadvantages of it? ...

Java: Sort a Collection using a CollatorKey

Hello, what I would like to achieve is to sort a colletion of objects by a string value. However in a locale dependant way using a collator. Due to performance reasons I do not want to use the Collator compare() method (as below in the code) rather the CollationKey class, as the java API states the using a CollationKey is much faster. ...

Fun with Java generics

Anybody knows how to write the piece of code below using generics AND avoiding compiler warnings ? (@SuppressWarnings("unchecked") is considered cheating). And, maybe, checking via generics that the type of "left" is the same as the type of "right" ? public void assertLessOrEqual(Comparable left, Comparable right) { if (left == nul...

Java: What is the difference between implementing Comparable and Comparator?

I have seen both used. When would you use one over the other? ...

Java Binary Tree, how to implement Node?

In the tree class I'm suppose to compare two node, for you know searching and adding items. I have some issues with how to make it comparable. When one adds data(generic, anything) to the tree one calls the Tree class which then makes a new Node object. How can I declare the variable data/element in the Node class so that it is of type E...

Java Generics: compareTo and "capture#1-of ?"

The following gives me an error message: public static List<Comparable<?>> merge(Set<List<Comparable<?>>> lists) { List<Comparable<?>> result = new LinkedList<Comparable<?>>(); HashBiMap<List<Comparable<?>>, Integer> location = HashBiMap.create(); int totalSize; for (List<Comparable<?>> l : lists) { location.put(l, 0); totalSiz...

Java: Simple issue with Interfaces and Generics

I made an interface to work with JGraphT. My intended use is like Comparable, in that implementing Comparable allows objects to be used with certain data structures. Simiarly, I have a JGraphT function that I want to work with anything that is Distanceable. public interface Distanceable<E> { /** * A representation of the dista...

Why isn't Collections.binarySearch() working with this comparable?

I have this Player class which implements the Comparable interface. Then I have an ArrayList of Players. I'm trying to use binarySearch() on the list of Players to find one Player, but Java is giving me a "cannot find symbol: method binarySearch(java.util.ArrayList< Player>,Player)". This the Player class: class Player implements Compa...

Java: Integer obj can't cast to Comparable

I'm having problems trying to pass an Integer object from a driver class as an argument for function of a SortedArray Generic class I created. From my driver class, I convert the user's int input into an Integer object to be cast onto Comparable of my SortedArray class. I continue to receive the error: "Exception in thread "main" java.l...

Java Comparable Interface

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 ...

Java Comparable Interface compareTo method...

I don't see anything that I am doing wrong, but NetBeans gives me the following error: incomparable types required: boolean found: java.lang.Object public int compareTo(Object obj) { if( obj instaceof Employee){ Employee employee = (Employee) obj; if(this.weekly_earnings > employee.weekly_earnings) return...

Does the specific signed integer matter when implementing compareTo in a Comparable <Type> class?

When implementing compareTo(), does the degree of "difference" need to be taken into account? For instance, if I have 3 objects, C1, C2, and C3, such that C1 < C2 < C3. Should C1.compareTo(C2) return an integer that is less than C2.compareTo(C3)? The documentation for the Comparable interface doesn't seem to specify one way or anothe...

how can I implement Comparable more than once?

I'm upgrading some code to Java 5 and am clearly not understanding something with Generics. I have other classes which implement Comparable once, which I've been able to implement. But now I've got a class which, due to inheritance, ends up trying to implement Comparable for 2 types. Here's my situation: I've got the following classes/i...

Consistent Equals() results, but inconsistent TreeMap.containsKey() result

I have the following object Node: private class Node implements Comparable<Node>(){ private String guid(); ... public boolean equals(Node o){ return (this == o); } public int hashCode(){ return guid.hashCode(); } public int compareTo(Node o...