Monday, December 15, 2008

Java compareTo method for TreeSet

  • To use java comparable class, implement Comparable<> in your class
  • Write a compareTo method as below:

public int compareTo(TeamDTO teamDTO)

    {

TreeMap map = new TreeMap();

map.put(getKey(teamDTO), teamDTO.getId());

map.put(getKey(this), this.getId());

if (map.firstKey().toString().equals(getKey(this)))

    return -1;

return 1;

    }