|
Hello,
Is it possible to use a Comparator without implementing the Comparable class? For instance, if I had the following:
MyClass { Comparator comp; OrderedListInheritance(Comparator c) { this.comp = c; } }
Could I then use comp to compare two objects...
Started by behrk2 on
, 4 posts
by 4 people.
Answer Snippets (Read the full thread at stackoverflow):
Comparator and Comparable are two separate and independent entities, only.
Comparable is an interface implemented by objects this case) reversed.
You use Comparator .
You don't use Comparable .
|
|
I have a list of objects I need to sort on a field, say Score. Without giving much thought I wrote a new class that implements Comparator, that does the task and it works.
Now looking back at this, I am wondering if I should have instead have the my class...
Started by pkrish on
, 7 posts
by 7 people.
Answer Snippets (Read the full thread at stackoverflow):
Based on an internal ID
Implement a Comparator if you have a more....
Is-the-difference-between-implementing-comparable-and-comparator
I would say the following: Implement Comparable for something like a natural ordering, e.g.
|
|
I'm really trying to like generics, but so far the trouble they've caused outweighs any benefits. Please, please show me I'm wrong.
I understand the necessity of adding @SuppressWarnings("unchecked") when using generic-free frameworks (Spring, Hibernate...
Started by Monkey Boson on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
comparable, in which case you just compare them directly, or you create a comparator -
public class Bean instanceof Comparable) { @SuppressWarnings("unchecked") Comparable<Object> originalValue implements Comparable....
|
Ask your Facebook Friends
|
I see code like this
class A implements Comparable<A> { }
What does this mean, what are the advantages and disadvantages of it?
Answer Snippets (Read the full thread at stackoverflow):
The Comparable puts the sorting logic directly in the class be sorted using the Comparable....
Implementing the Comparable interface means that the class supports responsible for sorting other classes.
Sorting functionality for lists).
|
|
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...
Started by eleven81 on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
For a little more clarification in this case, have a look at the Javadocs for Comparable....
Is a type parameter - Comparable is a generic class, and in this case the angle brackets mean that the class is comparable to other BigIntegers.
|
|
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...
Answer Snippets (Read the full thread at stackoverflow):
However, 1.5 ....
Comparable is a 1.4.2 interface, while telling you is incorrect.
CompareTo(Integer i)
Objects which implement (and therefore can be casted to) Comparable must have take any object, it cannot be cast as a Comparable.
|
|
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...
Started by Julien Chastang on
, 10 posts
by 10 people.
Answer Snippets (Read the full thread at stackoverflow):
You can still call....
My guess is that by not implementing Comparable, it give more flexibility implement Comparable.
Atomic ones are mutable, so can't implement an atomic comparison .
Subclasses of Number implements Comparable itself.
|
|
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...
Started by Dani on
, 5 posts
by 5 people.
Answer Snippets (Read the full thread at stackoverflow):
It just means....
The reason you are seeing compareTo(Object) is because of Type Erasure .
Probably, there is something else which is causing the problem .
Since java does not preserve generic types at runtime, ideally both of them should behave the same .
|
|
I have this Player class which implements the Comparable interface. Then I have an ArrayList of Player s. I'm trying to use binarySearch() on the list of Player s to find one Player , but Java is giving me a " cannot find symbol: method binarySearch(java...
Started by Roly on
, 3 posts
by 3 people.
Answer Snippets (Read the full thread at stackoverflow):
Instead of:
class Player implements Comparable { [...] public int Comparable , you can make compareTo() consistent with equals() by also overriding equals() and hashCode.
Arguments (or never supply them).
|
|
The title basically says it all: if I have a java method that is generic in T, can I find out anything about T? In particular, can I check whether T implements a certain interface or extends a certain class?
I would like to do something like
public <...
Started by Johannes on
, 7 posts
by 7 people.
Answer Snippets (Read the full thread at stackoverflow):
If there is a chance only some will be comparable if (l.size() >0 && l.get(0) instanceof Comparable) { // do; doSth(List<T> l) { if(T extends Comparable) { // do one thing } else { // do another } return ) does not implement....
|