Java tutorial
import java.util.Comparator; import java.util.Map; import java.util.Set; import java.util.TreeMap; class TComp implements Comparator<String> { public int compare(String a, String b) { int i, j, k; String aStr, bStr; aStr = a; bStr = b; i = aStr.lastIndexOf(' '); j = bStr.lastIndexOf(' '); k = aStr.substring(i).compareTo(bStr.substring(j)); if (k == 0) // last names match, check entire name return aStr.compareTo(bStr); else return k; } } class TreeMapDemo2 { public static void main(String args[]) { TreeMap<String, Double> tm = new TreeMap<String, Double>(new TComp()); tm.put("J D", new Double(3434.34)); tm.put("T S", new Double(123.22)); tm.put("J B", new Double(1378.00)); tm.put("T H", new Double(99.22)); tm.put("R S", new Double(-19.08)); Set<Map.Entry<String, Double>> set = tm.entrySet(); for (Map.Entry<String, Double> me : set) { System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } System.out.println(); double balance = tm.get("A"); tm.put("A", balance + 1000); System.out.println("A's new balance: " + tm.get("A")); } }