enum compareTo()

W

Wojtek

I have the following code:
----------------------------------
public enum Type implements Comparable<Type>
{
TYPE_A("a"),TYPE_B("b");

private String sortOrder;

private Type( String sort )
{
sortOrder=sort;
}

private String getSort()
{
return sortOrder;
}

@Override
public int compareTo(Type type)
{
return getSort().compareTo(type.getSort());
}
}
----------------------------------

However it seems that enum has a compareTo method which is final, so I
cannot implement my own sort order when I use the enum as a key in a
TreeMap.

Why is this?
 
M

Mark Space

Wojtek said:
I have the following code:
However it seems that enum has a compareTo method which is final, so I

Huh. Yet when I look at the Java doc, compareTo() is not declared
final. Why is that?


Maybe if you read the Java doc, you'd notice that the base enum type
already implements Comparable. I haven't tried to override a compareTo
method, but it might work if you just got rid of the "implements
Comparable<Type>" part of your enum declaration....
 
W

Wojtek

Mark Space wrote :
Huh. Yet when I look at the Java doc, compareTo() is not declared final.
Why is that?


Maybe if you read the Java doc, you'd notice that the base enum type already
implements Comparable. I haven't tried to override a compareTo method, but
it might work if you just got rid of the "implements Comparable<Type>" part
of your enum declaration....

Same error.
 
J

Joshua Cranmer

Wojtek said:
However it seems that enum has a compareTo method which is final, so I
cannot implement my own sort order when I use the enum as a key in a
TreeMap.

Why is this?

Well, you can always override the compareTo method on a TreeMap by
passing in your own Comparator...
 
W

Wojtek

Joshua Cranmer wrote :
Well, you can always override the compareTo method on a TreeMap by passing in
your own Comparator...

Ok, that works.

Now I feel like a rebel :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top