看了你的题目,然后我试着做了哈,好像找到了原因
是不是因为get(object key)的方法啊
因为我使用entrySet试了哈没问题,就是keySet有问题,然后就看了哈treemap的get方法的源码
- public V get(Object key) {
- Entry<K,V> p = getEntry(key);
- return (p==null ? null : p.value);
- }
复制代码
我的代码是:
- import java.util.Comparator;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Set;
- import java.util.TreeMap;
- public class Test3 {
- public static void main(String[] args) {
- TreeMap<String, Student> map = new TreeMap<String, Student>(
- new Comparator<String>() {
- @Override
- public int compare(String o1, String o2) {
- // TODO Auto-generated method stub
- return 1;
- }
- });
- map.put("1", new Student());
- map.put("2", new Student());
- map.put("3", new Student());
- map.put("4", new Student());
-
- System.out.println(map);
-
- Set<String> set = map.keySet();
- for (String stu : set) {
- Student student = map.get(stu);
- System.out.println(stu + "---" + student);
- }
- Set<Map.Entry<String, Student>> set1=map.entrySet();
- Iterator<Entry<String, Student>> it=set1.iterator();
- while(it.hasNext()){
- Entry<String, Student> en=it.next();
- String id=en.getKey();
- Student stu=en.getValue();
- System.out.println(id+">>>"+stu);
- }
-
- }
- }
- class Student {
- }
复制代码
写了这么多,求告知答案 |