黑马程序员技术交流社区
标题:
使用匿名内部类遍历treemap中的元素对象3
[打印本页]
作者:
Sou男人
时间:
2015-5-20 22:59
标题:
使用匿名内部类遍历treemap中的元素对象3
package Student2;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapDemo {
public static void main(String[] args) {
//建立map对象
TreeMap<Student,String> map = new TreeMap<Student,String>(new MyComparator());
//建立student元素对象
Student s1 = new Student("郭德纲",45);
Student s2 = new Student("于谦",55);
Student s3 = new Student("冯巩",40);
Student s4 = new Student("牛群",42);
Student s5 = new Student("冯巩",43);
Student s6 = new Student("陈佩斯",45);
Student s7 = new Student("朱时茂",35);
Student s8 = new Student("冯巩",40);
//添加元素
map.put(s1,"编号001");
map.put(s2,"编号002");
map.put(s3,"编号003");
map.put(s4,"编号004");
map.put(s5,"编号005");
map.put(s6,"编号006");
map.put(s7,"编号007");
map.put(s8,"编号008");
//遍历map
Set<Student> set = map.keySet();
for (Student key : set) {
String value = map.get(key);
System.out.println(value +"***"+key.getAge()+"***"+key.getName());
}
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2