Java中的各种集合排序 - 极悦
专注Java教育14年 全国咨询/投诉热线:444-1124-454
极悦LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 Java中的各种集合排序

Java中的各种集合排序

更新时间:2021-05-06 11:39:30 来源:极悦 浏览664次

1.背景

(1)软件开发过程中集合排序是比较强大的功能,会使用集合Map、Set、List实现排序功能,知道匿名内部类Comparator很关键,搞清楚集合排序的性能开销,排序遇到的坑以及解决的方法,注意下面的例子都是JDK1.8的用法。

2.LIst集合排序

(1)UML类图

(2)重点分析下ArrayList的排序,毕竟实战开发用的最频繁的就是它了

3.第一种做法

介绍:这种叫定制排序,或自定义排序,需编写匿名内部类,先new一个Comparator接口的比较器对象c,同时实现compare()其方法;

然后将比较器对象c传给Collections.sort()方法的参数列表中,实现排序功能;一般用这种的比较多。

(1)实体类(private int id)

public class Person{
    private int id;
    private String name;
    private String address; 
    public Person(int id, String name, String address) {
        this.id = id;
        this.name = name;
        this.address = address;
    } 
    public int getId() {
        return id;
    } 
    public void setId(int id) {
        this.id = id;
    } 
    public String getName() {
        return name;
    } 
    public void setName(String name) {
        this.name = name;
    } 
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    } 
    @Override
    public String toString() {
        return "Person{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

(2)测试类

public class ArrayListTest {
    public static void main(String[] args) {
        List<Person> list=Lists.newArrayList();
        //产生10以内的随机数
        int num = (int)(Math.random()*1000+1);
        for(int i=num;i>0;i--){
            list.add(new Person(i,"张三","河南"));
        }
        for(Object o : list){
            System.out.println(o);
        }
        System.out.println("++++++++++++++++++++++++++++++++++++");
        Collections.sort(list, new Comparator<Person>() {
           @Override
           public int compare(Person o1, Person o2) {
               if (o1.getId() > o2.getId()) {
                   return 1;
               } else if (o1.getId() < o2.getId()) {
                   return -1;
               }
               return 0;
           }
       });
        for(Object o : list){
            System.out.println(o);
        }
    }
}

(3)结果(升序)

Person{id='10', name='张三', address='河南'}
Person{id='9', name='张三', address='河南'}
Person{id='8', name='张三', address='河南'}
Person{id='7', name='张三', address='河南'}
Person{id='6', name='张三', address='河南'}
Person{id='5', name='张三', address='河南'}
Person{id='4', name='张三', address='河南'}
Person{id='3', name='张三', address='河南'}
Person{id='2', name='张三', address='河南'}
Person{id='1', name='张三', address='河南'}
++++++++++++++++++++++++++++++++++++
Person{id='1', name='张三', address='河南'}
Person{id='2', name='张三', address='河南'}
Person{id='3', name='张三', address='河南'}
Person{id='4', name='张三', address='河南'}
Person{id='5', name='张三', address='河南'}
Person{id='6', name='张三', address='河南'}
Person{id='7', name='张三', address='河南'}
Person{id='8', name='张三', address='河南'}
Person{id='9', name='张三', address='河南'}
Person{id='10', name='张三', address='河南'}

4.第二中做法

(1)实体类(private String id)

public class Person{
    private String id;
    private String name;
    private String address; 
    public Person(String id, String name, String address) {
        this.id = id;
        this.name = name;
        this.address = address;
    } 
    public String getId() {
        return id;
    } 
    public void setId(String id) {
        this.id = id;
    } 
    public String getName() {
        return name;
    } 
    public void setName(String name) {
        this.name = name;
    } 
    public String getAddress() {
        return address;
    } 
    public void setAddress(String address) {
        this.address = address;
    } 
    @Override
    public String toString() {
        return "Person{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

(2)测试类

public class ArrayListTest {
    public static void main(String[] args) {
        List<Person> list=Lists.newArrayList();
        //产生10以内的随机数
        int num = (int)(Math.random()*100+1);
        for(int i=num;i>0;i--){
            list.add(new Person(""+i,"张三","河南"));
        }
        for(Object o : list){
            System.out.println(o);
        }
        System.out.println("++++++++++++++++++++++++++++++++++++");
       Collections.sort(list, new Comparator<Person>() {
           @Override
           public int compare(Person o1, Person o2) {
               return  new Double(o1.getId()).compareTo(new Double(o2.getId()));
           } 
       });
        for(Object o : list){
            System.out.println(o);
        }
    }
}

(3)结果

Person{id='10', name='张三', address='河南'}
Person{id='9', name='张三', address='河南'}
Person{id='8', name='张三', address='河南'}
Person{id='7', name='张三', address='河南'}
Person{id='6', name='张三', address='河南'}
Person{id='5', name='张三', address='河南'}
Person{id='4', name='张三', address='河南'}
Person{id='3', name='张三', address='河南'}
Person{id='2', name='张三', address='河南'}
Person{id='1', name='张三', address='河南'}
++++++++++++++++++++++++++++++++++++
Person{id='1', name='张三', address='河南'}
Person{id='2', name='张三', address='河南'}
Person{id='3', name='张三', address='河南'}
Person{id='4', name='张三', address='河南'}
Person{id='5', name='张三', address='河南'}
Person{id='6', name='张三', address='河南'}
Person{id='7', name='张三', address='河南'}
Person{id='8', name='张三', address='河南'}
Person{id='9', name='张三', address='河南'}
Person{id='10', name='张三', address='河南'}

5.第三种做法

另外一种称为自然排序,参与排序的对象需实现comparable接口,重写其compareTo()方法,方法体中实现对象的比较大小规则。

(1)实体类

public class Person implements Comparable{
    private String id;
    private String name;
    private String address; 
    public Person(String id, String name, String address) {
        this.id = id;
        this.name = name;
        this.address = address;
    } 
    public String getId() {
        return id;
    } 
    public void setId(String id) {
        this.id = id;
    } 
    public String getName() {
        return name;
    } 
    public void setName(String name) {
        this.name = name;
    } 
    public String getAddress() {
        return address;
    } 
    public void setAddress(String address) {
        this.address = address;
    } 
    @Override
    public String toString() {
        return "Person{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    } 
    @Override
    public int compareTo(Object o) {
        if (o instanceof Person){
            Person o1 = (Person)o;
            return  new Double(this.getId()).compareTo(new Double(o1.getId()));
        }
        throw new ClassCastException("不能转换为Person类型的对象...");
    }
}

(2)测试类

public class ArrayListTest {
    public static void main(String[] args) {
        List<Person> list=Lists.newArrayList();
        //产生10以内的随机数
        int num = (int)(Math.random()*100+1);
        for(int i=num;i>0;i--){
            list.add(new Person(""+i,"张三","河南"));
        }
        for(Object o : list){
            System.out.println(o);
        }
        System.out.println("++++++++++++++++++++++++++++++++++++");
        Collections.sort(list);
        for(Object o : list){
            System.out.println(o);
        }
    }
}

(3)结果

Person{id='10', name='张三', address='河南'}
Person{id='9', name='张三', address='河南'}
Person{id='8', name='张三', address='河南'}
Person{id='7', name='张三', address='河南'}
Person{id='6', name='张三', address='河南'}
Person{id='5', name='张三', address='河南'}
Person{id='4', name='张三', address='河南'}
Person{id='3', name='张三', address='河南'}
Person{id='2', name='张三', address='河南'}
Person{id='1', name='张三', address='河南'}
++++++++++++++++++++++++++++++++++++
Person{id='1', name='张三', address='河南'}
Person{id='2', name='张三', address='河南'}
Person{id='3', name='张三', address='河南'}
Person{id='4', name='张三', address='河南'}
Person{id='5', name='张三', address='河南'}
Person{id='6', name='张三', address='河南'}
Person{id='7', name='张三', address='河南'}
Person{id='8', name='张三', address='河南'}
Person{id='9', name='张三', address='河南'}
Person{id='10', name='张三', address='河南'}

以上就是极悦小编介绍的"Java中的各种集合排序"的内容,希望对大家有帮助,如有疑问,请在线咨询,有专业老师随时为您服务。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>