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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Adualtbird 中级黑马   /  2015-9-15 13:17  /  285 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. /*
  2. 需求:将学生对象和地址存入到Map集合中,姓名和年龄相同视为同一个学生
  3. */
  4. import java.util.*;
  5. class  HashMapTest
  6. {
  7.         public static void main(String[] args)
  8.         {
  9.                 HashMap<Student,String> hm=new HashMap<Student,String>();
  10.                 hm.put(new Student("张三",18),"武汉");
  11.                 hm.put(new Student("李四",19),"天津");
  12.                 hm.put(new Student("王五",18),"西安");
  13.                 hm.put(new Student("赵六",18),"北京");
  14.                 hm.put(new Student("张三",18),"武汉");
  15.                 /*
  16.                 HashMap集合获取元素的第一种方式,迭代键的集合
  17.                 Set<Student> set=hm.keySet();
  18.                 Iterator<Student> it=set.iterator();
  19.                 while(it.hasNext()){
  20.                         Student stu=it.next();
  21.                         String homeTown=hm.get(stu);
  22.                         System.out.println("Student:"+stu+",homTown:"+homeTown);
  23.                 }
  24.                 */
  25.                 //HashMap集合获取元素的第二种方式,迭代映射关系
  26.                 Set<Map.Entry<Student,String>> entry=hm.entrySet();
  27.                 Iterator<Map.Entry<Student,String>> it=entry.iterator();
  28.                 while(it.hasNext()){
  29.                         Map.Entry<Student,String> me=it.next();
  30.                         Student stu=me.getKey();
  31.                         String homeTown=me.getValue();
  32.                         System.out.println(stu+"来自"+homeTown);
  33.                 }

  34.         }
  35. }
  36. class Student implements Comparable<Student>
  37. {
  38.         private String name;
  39.         private int age;
  40.         Student(String name,int age){
  41.                 this.name=name;
  42.                 this.age=age;
  43.         }
  44.         public String getName(){
  45.                 return name;
  46.         }
  47.         public int getAge(){
  48.                 return age;
  49.         }
  50.         public int hashCode(){
  51.                 return name.hashCode()+age*7;
  52.         }
  53.         //复写compareTo()方法,定义元素的比较方式
  54.         public int compareTo(Student stu){
  55.                 int num=this.name.compareTo(stu.name);
  56.                 if(num==0)
  57.                         return new Integer(this.age).compareTo(stu.age);
  58.                 return num;

  59.         }
  60.         //自定义equals方法,判断两个Person对象是否相同
  61.         public boolean equals(Object obj){
  62.                 if(!(obj instanceof Student))
  63.                         return false;
  64.                 Student stu=(Student)obj;
  65.                 return this.getName().equals(stu.getName())&&this.age==stu.age;
  66.         }
  67.         public String toString(){
  68.                 return this.name+":"+this.age;
  69.         }
  70. }
复制代码


1.png (4.41 KB, 下载次数: 5)

第一种方式结果

第一种方式结果

2.png (3.2 KB, 下载次数: 4)

第二种运行结果

第二种运行结果

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马