黑马程序员技术交流社区
标题:
map集合练习
[打印本页]
作者:
kemeng
时间:
2015-3-20 11:59
标题:
map集合练习
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 int compareTo(Student s)
{
int num=new Integer(this.age).compareTo(new Integer(s.age));
if(num==0)
return this.name.compareTo(s.name);
return num;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String toString()
{
return name+":"+age;
}
}
class Demo3
{
public static void main(String[] args)
{
HashMap<Student,String> hs=new HashMap<Student,String>();
hs.put(new Student("zhangsan",23),"beijing");
hs.put(new Student("zhangsan",23),"tianjin");
hs.put(new Student("lisi",24),"tianjin");
hs.put(new Student("wangwu",21),"chongqing");
hs.put(new Student("liusan",25),"sichuan");
Set<Student> stu=hs.keySet();
Iterator<Student> it=stu.iterator();
while(it.hasNext())
{
Student s=it.next();
String addr=hs.get(s);
System.out.println(s+"..."+addr);
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2