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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黑马连家华 中级黑马   /  2012-5-3 20:53  /  2505 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 黑马连家华 于 2012-8-13 17:24 编辑

/*
存入学生对象(成员有name和age),要求对应所在班级存储
*/
import java.util.*;
class Student implements Comparable<Student>
{
        private String name;
        private int age;
        Student(String name,int age)
        {
                this.name = name;
                this.age = age;
        }
        public String getName()
        {
                return name;
        }
        public int getAge()
        {
                return age;
        }
        public int hashCode()
        {
                return this.name.hashCode()+this.age*13;
        }
        public boolean equals(Object obj)
        {
                if (!(obj instanceof Student))
                {
                        throw new ClassCastException("不是同一类");
                }
                Student stu = (Student)obj;
                return this.name.equals(stu.name) && this.age == stu.age;
        }
        public int compareTo(Student stu)
        {
                int num = this.name.compareTo(stu.name);
                if (num == 0)
                {
                        return new Integer(this.age).compareTo(new Integer(stu.age));
                }
                return num;
        }
}
class Exercise4
{
        public static void main(String[] args)
        {
                //建立集合这个集合里面是教室和学生对象
                TreeMap<String,Student> classes = new TreeMap<String,Student>();
                classes.put("Base",new Student("ZhanSan",18));
                classes.put("Base",new Student("LiSi",22));
                classes.put("Adv",new Student("Wangwu",25));
                classes.put("Adv",new Student("ZhaoLiu",22));
                //取出教室与学生的关系
                Set<Map.Entry<String,Student>> es = classes.entrySet();
                for (Iterator<Map.Entry<String,Student>> it = es.iterator();it.hasNext() ; )
                {
                        Map.Entry<String,Student> me = it.next();
                        //取出教室,并打印
                        System.out.println(me.getKey());
                        //然后如何使用迭代器取出教室里的学生????????????
                        
                        
                }
        }
}

5 个回复

倒序浏览

回帖奖励 +1 黑马币

本帖最后由 魏征 于 2012-5-3 23:04 编辑

TreeMap<String,TreeSet> classes=new TreeMap<String,TreeSet>();
TreeSet<Student> Base=new TreeSet<Student>();
TreeSet<Student> Adv=new TreeSet<Student>();
Base.add(new Student("ZhanSan",18));
Base.add(new Student("LiSi",22));
Adv.add(new Student("Wangwu",25));
Adv.add(new Student("ZhaoLiu",22));
classes.put("base",Base);
classes.put("adv",Adv);
Set<Map.entry<String,TreeSet>> s1=classes.entrySet();
Iterator<Map.entry<String,TreeSet>>  it=s1.iterator();
while(it.hasNext())
{
     Map.entry<String,TreeSet>>  me=it.next();
     String key=me.keySet();
     Treeset ts=me.valueSet();
     System.out.println(key);
      Iterator<Student> it1=ts.iterator();
      while(it1.hasNext())
      {
          Student s=it1.next();
          System.out.println(s.getName()+s.getAge());
       }

}


回复 使用道具 举报
本帖最后由 魏征 于 2012-5-3 22:12 编辑

楼主你这种做法是取不出来的,TreeMap的键是唯一的,
                classes.put("Base",new Student("ZhanSan",18));
                classes.put("Base",new Student("LiSi",22));
这样子的做法就是当键相同时,第二行的值把第一行的值覆盖了。
回复 使用道具 举报
  Set<Map.Entry<String,Integer>> es= classes.entrySet();
  for(Map.Entry<String,Student> me : es) {
   System.out.println(me.getKey() + ":" + me.getValue());
  }
这里用我的是for增强哈,照样行
回复 使用道具 举报

回帖奖励 +1 黑马币

本帖最后由 隋营营 于 2012-5-4 08:26 编辑

我在main中修改了一下,您看行不?
public static void main(String[] args) {
        TreeMap<String, Student[]> classes = new TreeMap<String, Student[]>();
        classes.put("Base", new Student[] { new Student("LiSi", 22),
                new Student("ZhanSan", 18) });

        classes.put("Adv", new Student[] { new Student("ZhaoLiu", 22),
                new Student("Wangwu", 25) });
        Set<Map.Entry<String, Student[]>> es = classes.entrySet();
        for (Iterator<Map.Entry<String, Student[]>> it = es.iterator(); it.hasNext();) {
            Map.Entry<String, Student[]> me = it.next();
            String name = me.getKey();
            Student[] values = me.getValue();
            if (1 == values.length) {
                System.out.println(values[0]);
            } else {
                for (int i = 0; i < values.length; i++) {
                    System.out.println(values);
                }
            }

        }
}

就是把key相同的value值存放到Student[]数组中,然后取值时,就能得到一个Student[]

回复 使用道具 举报
隋营营 发表于 2012-5-4 08:24
我在main中修改了一下,您看行不?
public static void main(String[] args) {
        TreeMap classes =  ...

这么精彩的回答要加分!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马