A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© SmallRooker 中级黑马   /  2014-8-26 08:36  /  997 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

集合中存储的对象能否排序?如果能排序 谁给个例子?帮忙解答下大神们。

10 个回复

倒序浏览
本帖最后由 依然超级赛亚人 于 2014-8-26 09:02 编辑
  1. <blockquote>/*
复制代码
回复 使用道具 举报
上面出现了问题,不知怎么回事看不到,不好意思,只能再发一次,请别误解为刷帖,见谅!
  1. /*
  2. Collection的TreeSet可以排序,默认的自然排序是按照英文字母A-Z-a-z的顺序排,而且没有重复元素。
  3. 自己试一下最好!
  4. */
  5. import java.util.*;
  6. class SetTest00
  7. {
  8.         public static void main(String[] args)
  9.         {
  10.                 Collection<String> hs = new TreeSet<String>();
  11.                 hs.add("java");
  12.                 hs.add("cba");
  13.                 hs.add("NBA");
  14.                 hs.add("CBA");
  15.                 hs.add("HelloWorld");
  16.                 hs.add("hellojava");
  17.                 hs.add("hellojava");
  18.                 hs.add("hellojava");
  19.                 Iterator<String> it = hs.iterator();
  20.                 while(it.hasNext()){
  21.                         System.out.println(it.next());
  22.                 }
  23.                
  24.         }
  25. }
复制代码
回复 使用道具 举报
TreeSet是SortedSet接口的实现类,正如SortedSet名字所暗示的,TreeSet可以确保集合元素处于排序状态。但是
集合 元素需实现Comparable接口或者创建TreeSet对象时传入一个Comparator的实现类的对象
回复 使用道具 举报
Student类会写吧,先定义它,然后看我的代码


import java.util.Comparator;
import java.util.Set;
import java.util.TreeMap;

import cn.itcast_02.Student;

/*
* TreeMap存储自定义对象并遍历。
* 键:Student
* 值:String
*
* 如果一个自定义对象做键,用TreeMap集合。。
* 两种方式:
* A:让自定义对象所属的类去实现Comparable接口
* B:使用带参构造方法,创建TreeMap,接收Comparator接口参数。
*/
public class TreeMapDemo2 {
        public static void main(String[] args) {
                // 创建集合对象
                TreeMap<Student, String> tm = new TreeMap<Student, String>(
                                new Comparator<Student>() {     //匿名内部类
                                        @Override
                                        public int compare(Student s1, Student s2) {
                                        int num = s2.getAge() - s1.getAge();       //实现年龄从大到小排序
                                        int num2 = (num == 0) ? s1.getName().compareTo( //年龄相等就比姓名
                                                                s2.getName()) : num;
                                                return num2;
                                        }
                                });

                // 创建元素对象
                Student s1 = new Student("张三", 30);
                Student s2 = new Student("李四", 40);
                Student s3 = new Student("小六", 50);
                Student s4 = new Student("王五", 20);
                Student s5 = new Student("小三", 30);

                // 添加元素
                tm.put(s1, "it001");
                tm.put(s2, "it002");
                tm.put(s3, "it003");
                tm.put(s4, "it004");
                tm.put(s5, "it005");

                // 遍历
                Set<Student> set = tm.keySet();
                for (Student key : set) {
                        String value = tm.get(key);
                        System.out.println(key.getName() + "***" + key.getAge() + "***"
                                        + value);
                }
        }
}
回复 使用道具 举报
hsy 发表于 2014-8-26 10:52
Student类会写吧,先定义它,然后看我的代码

这是采用比较器比较的,也就是匿名内部类。第一种方式需要Student实现Comparable接口,重COmparaTo方法,里面自定义比较规则就行了
回复 使用道具 举报
第几天的啦。。。。。
回复 使用道具 举报
七弟 中级黑马 2014-8-26 15:53:33
8#
学习一下
回复 使用道具 举报
lhtwm1 中级黑马 2014-8-26 17:00:46
9#
你进度很快的说
回复 使用道具 举报
Set子类可以排序
回复 使用道具 举报
了解了,先记下了,还没有看完集合,不过应该快了。受教!顶  各位哥们
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马