Modified to use TreeMap; implemented compareTo.