Map中的HashMap存储对象

来源:互联网 发布:九世之仇 知乎 编辑:程序博客网 时间:2024/06/06 07:29
<pre name="code" class="java">package test;import java.util.HashMap;import java.util.Iterator;public class hashmapdemo {public static void main(String[] args){HashMap<Student,String> hm = new HashMap<Student,String>();hm.put(new Student("wangwu",23),"shanghai");hm.put(new Student("wangwenwen",28),"北京");hm.put(new Student("zhangming",20),"武汉");hm.put(new Student("xiaoqiang",35),"天津");Iterator<Student> it = hm.keySet().iterator();while(it.hasNext()){Student key = it.next();String value = hm.get(key);System.out.println(key.name+":"+key.age+"----"+value);}}}
同一个包下的
package test;public class Student {int age;String name;public Student(String name,int age){this.name = name;this.age = age;}}



0 0
原创粉丝点击