这是我添加的,你看一下符不符合你的要求。
- import java.util.*;
- class TComp implements Comparator<Object>
- {
- public int compare(Object a, Object b) {
- int i, j, k;
- String aStr, bStr;
-
- aStr = (String) a;
- bStr = (String) b;
-
- i = aStr.lastIndexOf(" ");
- j = bStr.lastIndexOf(" ");
-
- k = aStr.substring(i).compareTo(bStr.substring(j));
- if (k==0)
- {
- return aStr.compareTo(bStr);
- }
- else return k;
- }
- }
- public class DemoMapComparator{
- public static void main(String[] args) {
- TreeMap<String, Double> tm = new TreeMap<String, Double>(new TComp());
- tm.put("JW Wang", 2000.2);
- tm.put("JP Huang", 20.13);
- tm.put("Min Huang", 300.25);
-
- System.out.println(tm);
-
- Set<Map.Entry<String,Double>> st = tm.entrySet();
- System.out.println(st);
-
- Iterator<Map.Entry<String,Double>> i = st.iterator();
-
- while (i.hasNext())
- {
- Map.Entry<String,Double> me = (Map.Entry<String,Double>)i.next();
- System.out.print(me.getKey() + ": ");
- System.out.println(me.getValue());
- }
-
- double d = ((Double)tm.get("JP Huang")).doubleValue();
- tm.put("JP Huang", d + 1000);
- System.out.println("JP Huang's new balance: " + tm.get("JP Huang"));
-
- }
- }
复制代码
|