不知道这样合不合你的意思
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Set;
- import java.util.TreeMap;
- class Student {
- String id;
- String name;
- public Student(String id, String name) {
- super();
- this.id = id;
- this.name = name;
- }
- @Override
- public String toString(){
- return name;
- }
- }
- public class ThreadDemo3 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- TreeMap<String,Student> tm = new TreeMap<String,Student>();
- Student st1 = new Student("001", "Jack");
- Student st2 = new Student("002", "Rose");
- Student st3 = new Student("003", "Tom");
- tm.put(st1.id, st1);//把对象编号当作键传入,学生对象当作值传入
- tm.put(st2.id, st2);
- tm.put(st3.id, st3);
- //此处怎么将以上的三个对象穿进去
- Set<Map.Entry<String, Student>> entryset = tm.entrySet();
- Iterator<Entry<String, Student>> it = entryset.iterator();
- while (it.hasNext()) {
- Map.Entry<String,Student> entry = (Entry<String,Student>) (it.next());
- Object key = entry.getKey();
- Object value = entry.getValue();
- System.out.println(key + " " + value);//重写学生类toString方法
- }
- }
- }
复制代码 输出结果是
001 Jack
002 Rose
003 Tom
|