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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 大恶魔先森~ 中级黑马   /  2014-5-28 10:16  /  1182 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.util.*;
import java.util.Map.Entry;
public class TreeMapDemo
{
        public static void main(String args[])
        {
                TreeMap<Student,String> tm=new TreeMap<Student,String>();
                tm.put(new Student("lisi",20),"beijing");
                tm.put(new Student("zhangsan",19),"nanjing");
                Set<Map.Entry<Student,String>> entryset=tm.entrySet();
                Iterator<Map.Entry<Student,String>> it=entryset.iterator();
                while(it.hasNext())
                {
                        Map.Entry<Student,String> ms=it.next();
                        Student s= tm.getKey();
                        String add=tm.getValue();
                        System.out.println(s.getName()+"::"+s.getAge());
                }
        }
       
        /*public static void main(String args[])
        {
                TreeMap<Student,String> tm=new TreeMap<Student,String>();
                tm.put(new Student("lisi",20),"beijing");
                tm.put(new Student("zhangsan",19),"nanjing");
                tm.put(new Student("wangwu",20),"jiangxi");
                Set<Student> keyset=tm.keySet();
                Iterator<Student> it=keyset.iterator();
                while(it.hasNext())
                {
                        Student s=it.next();
                        System.out.println(s.getName()+"::"+s.getAge());
                }
        }
        */
       

}
class Student implements Comparable<Student>
{
        private String name;
        private int age;
        public Student(String name,int age)
        {
                this.name=name;
                this.age=age;
        }
        public String getName()
        {
                return name;
        }
        public int getAge()
        {
                return age;
        }
        public int compareTo(Student s)
        {
                int num=new Integer(this.age).compareTo(new Integer(s.age));
                if(num==0)
                        return new Integer(this.name.length()).compareTo(new Integer(s.name.length()));//名字按字符串长度来判断
                return num;
               
        }
       
       
}
发生了这两个错误The method getKey() is undefined for the type TreeMap<Student,String>
        The method getValue() is undefined for the type TreeMap<Student,String>

评分

参与人数 1技术分 +1 收起 理由
李小然 + 1

查看全部评分

4 个回复

倒序浏览
        public static void main(String args[])
         {
                 TreeMap<Student,String> tm=new TreeMap<Student,String>();
                 tm.put(new Student("lisi",20),"beijing");
                 tm.put(new Student("zhangsan",19),"nanjing");
                 Set<Map.Entry<Student,String>> entryset=tm.entrySet();
                 Iterator<Map.Entry<Student,String>> it=entryset.iterator();
                 while(it.hasNext())
                 {
                         Map.Entry<Student,String> ms=it.next();
////////////////////////////////////////////////////////////////////
                         Student s= ms.getKey();//①用ms②Entry的取出方法和TreeMap的取出方法不同的
                         String add=ms.getValue();//③你现在要搞明白为什么用Entry来迭代Map,感觉你还不明白这个
////////////////////////////////////////////////////////////////////
                         System.out.println(s.getName()+"::"+s.getAge());
                 }
         }

评分

参与人数 1技术分 +1 收起 理由
轻语。 + 1 赞一个!

查看全部评分

回复 使用道具 举报
  1. public class TreeMapDemo {
  2.        
  3.         public static void main(String args[])
  4.     {
  5.             TreeMap<Student,String> tm=new TreeMap<Student,String>();
  6.             tm.put(new Student("lisi",20),"beijing");
  7.             tm.put(new Student("zhangsan",19),"nanjing");
  8.             Set<Map.Entry<Student,String>> entryset=tm.entrySet();
  9.             Iterator<Map.Entry<Student,String>> it=entryset.iterator();
  10.             while(it.hasNext())
  11.             {
  12.                     Map.Entry<Student,String> ms=it.next();
  13.                     Student s= ms.getKey();//此處應該是ms而不是tm
  14.                    // String add=ms.getValue();
  15.                     System.out.println(s.getName()+"::"+s.getAge());
  16.             }
  17.     }
  18.    
  19.    
  20.    
  21. }
复制代码


回复 使用道具 举报
public class TreeMapDemo {
        
        public static void main(String args[])
    {
            TreeMap<Student,String> tm=new TreeMap<Student,String>();
            tm.put(new Student("lisi",20),"beijing");
            tm.put(new Student("zhangsan",19),"nanjing");
            Set<Map.Entry<Student,String>> entryset=tm.entrySet();
            Iterator<Map.Entry<Student,String>> it=entryset.iterator();
            while(it.hasNext())
            {
                    Map.Entry<Student,String> ms=it.next();
                    Student s= ms.getKey();//不应该是tm应该是ms tm是集合的数据,而ms才是迭代器的数据。迭代器是不能直接操作集合的内容的(ListIterator除外),如果直接操作会引起数据安全方面的影响。
                   // String add=ms.getValue();
                    System.out.println(s.getName()+"::"+s.getAge());
            }
    }
   
   
   
}
回复 使用道具 举报
entetyset和keyset虽然都是Set集合,但是内部存放的内容是不一样的,
前者存放的是键值对的关系,后者存放的是键
前者要获取其中的元素使用的是EntrySet特有的方法,(getKey();getValue()),而不是直接使用原MAP集合的方法。
而后者是使用MAP集合的方法通过key来获取值(getValue(key)).
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马