A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王善辉 中级黑马   /  2015-4-27 23:49  /  507 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

TreeMap map=new TreeMap(  new Comparetor(){
   public boolean compare(){
return -1;
}
});
我这里就不写Student类了,也不写泛型拉
map.put("1",student1);
map.put("2",student2);
map.put("3",student3);
map.put("4",student4);

Set set=map.keyset();


for(  String stu :set){
        Student student=map.get(stu);
        System.out.println(stu+"---"+student);
}
-------------------------------------------------

结果:1------unll
          2------unll
          3------unll
          4------unll
知道为啥不?提示看原码

5 个回复

倒序浏览
没看过0.0  看看去 0.0
回复 使用道具 举报
内部内覆盖方法的返回值不是boolean,而是int
回复 使用道具 举报
public boolean compare(){
return -1;
}    代码有问题吧,
回复 使用道具 举报
看了你的题目,然后我试着做了哈,好像找到了原因
是不是因为get(object key)的方法啊
因为我使用entrySet试了哈没问题,就是keySet有问题,然后就看了哈treemap的get方法的源码
  1.     public V get(Object key) {
  2.         Entry<K,V> p = getEntry(key);
  3.         return (p==null ? null : p.value);
  4.     }
复制代码



我的代码是:
  1. import java.util.Comparator;
  2. import java.util.Iterator;
  3. import java.util.Map;
  4. import java.util.Map.Entry;
  5. import java.util.Set;
  6. import java.util.TreeMap;

  7. public class Test3 {
  8.         public static void main(String[] args) {
  9.                 TreeMap<String, Student> map = new TreeMap<String, Student>(
  10.                                 new Comparator<String>() {

  11.                                         @Override
  12.                                         public int compare(String o1, String o2) {
  13.                                                 // TODO Auto-generated method stub
  14.                                                 return 1;
  15.                                         }
  16.                                 });
  17.                 map.put("1", new Student());
  18.                 map.put("2", new Student());
  19.                 map.put("3", new Student());
  20.                 map.put("4", new Student());
  21.                
  22.                 System.out.println(map);
  23.                
  24.                 Set<String> set = map.keySet();

  25.                 for (String stu : set) {
  26.                         Student student = map.get(stu);
  27.                         System.out.println(stu + "---" + student);
  28.                 }
  29.                 Set<Map.Entry<String, Student>> set1=map.entrySet();
  30.                 Iterator<Entry<String, Student>> it=set1.iterator();
  31.                 while(it.hasNext()){
  32.                         Entry<String, Student> en=it.next();
  33.                         String id=en.getKey();
  34.                         Student stu=en.getValue();
  35.                         System.out.println(id+">>>"+stu);
  36.                 }
  37.                
  38.         }
  39. }

  40. class Student  {

  41. }
复制代码


写了这么多,求告知答案
回复 使用道具 举报
一你有可能没有定义Student类 定义过之后一定要重写Tosring方法  就这么多
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马