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();

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

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划