- // 创建TreeSet对象
- TreeSet<Student> t = new TreeSet<Student>(new Comparator<Student>(){
- @Override
- public int compare(Student s1, Student s2) {
- int num = s1.getAge()-s2.getAge();
- num = (num==0)?(s1.getName().compareTo(s2.getName())):num;
- return num;
- }
- } );
- // 创建Student对象,将对象存入集合中去
- Student s1 = new Student("linqingxia",26);
- Student s2 = new Student("zhouxun",33);
- Student s3 = new Student("zhangzishoi",26);
- Student s4 = new Student("wanglaowu",55);
- Student s5 = new Student("linqingxia",26);
- //使用TreeSet类存储对象
- t.add(s1);
- t.add(s2);
- t.add(s3);
- t.add(s4);
- t.add(s5);
- //这里的for循环里不加操作表达式,也是可以的吗?怎么会这样?
- for(Iterator iter = t.iterator();iter.hasNext();){
- System.out.println((iter.next()));
- }
复制代码 for循环里不加操作表达式,也是可以的吗?怎么会这样?
|