space_Home 2017-06-12 05:03 采纳率: 50%
浏览 921
已采纳

下面Java代码中怎么添加泛型

import java.util.* ;

class Employee implements Comparator {
public int compare (Object a, Object b) {
int k ;
String aStr, bStr ;
aStr = (String) a ;
bStr = (String) b ;
k = aStr.compareTo(bStr) ;
if (k == 0)
return aStr.compareTo(bStr) ;
else
return k ;
}
}

public class TestDemo {
public static void main (String args[]) {
TreeMap tm = new TreeMap (new Employee()) ;
tm.put("Z、张三", new Double(3534.34)) ;
tm.put("L、李四", new Double(126.22)) ;
tm.put("W、王五", new Double(1578.40)) ;
tm.put("Z、赵六", new Double(99.62)) ;
tm.put("S、孙七", new Double(-29.08)) ;
Set set = tm.entrySet() ;
Iterator itr = set.iterator() ;
while (itr.hasNext()) {
Map.Entry me = (Map.Entry) itr.next() ;
System.out.print(me.getKey() + ":") ;
System.out.println(me.getValue()) ;
}
System.out.println() ;
double balance = ((Double) tm.get("Z、张三")).doubleValue() ;
tm.put("Z、张三", new Double(balance + 2000)) ;
System.out.println("张三最新的资金数为:" + tm.get("Z、张三")) ;
}
}

  • 写回答

1条回答 默认 最新

  • lihongbiao0610 2017-06-12 05:43
    关注

    String字符串就已经实现了Comparator 接口。所以不用传入一个Comparator
    直接写成:
    TreeMap tm = new TreeMap();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败