package stack1;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class Test3 {
public static void main(String [] args){
TreeMap<String,ArrayList<Student>> tm = new TreeMap<String,ArrayList<Student>>(
new Comparator<String>(){
@Override
public int compare(String s1, String s2) {
//这里相减正常(下面ArrayList可以正常)
//int num = s1.hashCode()-s2.hashCode();
//相加空指针异常
//return num;
//正数或者负数空指针异常
//return -1;
//为0是遍历方式1只有一半打印的出来
return -1;
}
});
String s = new String("110");
String s1 = new String("111");
ArrayList<Student> ar = new ArrayList<Student>();
ar.add(new Student("赵薇",22));
ar.add(new Student("赵",2));
ar.add(new Student("赵薇",2));
ar.add(new Student("赵薇",20));
tm.put(s, ar);
ArrayList<Student> a = new ArrayList<Student>();
a.add(new Student("赵0",22200));
a.add(new Student("赵薇",22));
a.add(new Student("赵薇",222));
a.add(new Student("赵薇",223));
tm.put(s1,a);
//可以打印出
/* System.out.println(tm);
* 打印为null
System.out.println(tm.get(s));
//打印正常
System.out.println(tm);*/
// 空指针异常
Set<String> set = tm.keySet();
System.out.println(set.size());
//for(String s : set){
//System.out.println(s+tm.get(s));
//ArrayList<Student> at = tm.get(s);
/*for(Student am : at){
System.out.println(am.getName()+" "+am.getAge());*/
//}
//正常情况
/* Set<Map.Entry<String, ArrayList<Student>>> set = tm.entrySet();
for (Map.Entry<String, ArrayList<Student>> entry : set) {
//System.out.println(entry);
System.out.println(entry.getKey()+":");
ArrayList<Student> al = entry.getValue();
for (Student student : al) {
System.out.println(student.getName()+"***"+student.getAge());
}
}*/
标准的学生类不上传了
}
} |
|