Re: Sort Map on Value
Lew wrote :
Wojtek wrote:
Ok, so this should work then:
class PersonMapKey implements Comparable<PersonMapKey>
{
private String ivKey;
private String ivName;
PersonMapKey( String key, String name )
{
super();
This idiom puzzles me. Why call 'super()' explicitly? Particularly since
the class extends 'Object'?
Coding fluff. I know that super() is called implicitly, however I
always put it in.
ivKey = key;
ivName = name;
}
private String getKey()
{
return ivKey;
}
private String getName()
{
return ivName;
}
@Override
public int hashCode()
{
return ivKey.hashCode();
}
@Override
public boolean equals( Object key )
{
return ivKey.equals( ((PersonMapKey) key).getKey() );
}
@Override
public int compareTo( PersonMapKey personKey )
{
return ivName.compareTo( personKey.getName() );
}
Is this consistent with 'equals()'?
No, it is consistent with compareTo() as per the interface.
--
Wojtek :-)