xml地图|网站地图|网站标签 [设为首页] [加入收藏]

Java集合排序,ArrayList等常见集合的排序问题

来源:http://www.ccidsi.com 作者:呼叫中心培训课程 人气:122 发布时间:2020-03-13
摘要:注意:本代码均已在 jdk1.6 本子下通过测量试验 Test测试类: import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;public class Test { public static void main(String[] a

注意:本代码均已在jdk1.6本子下通过测量试验

Test测试类:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test {

    public static void main(String[] args) {

        List<Dog> mDogs = new ArrayList<>();
        Dog dog1 = new Dog("eog4", 4);
        Dog dog2 = new Dog("dog5", 5);
        Dog dog3 = new Dog("bog2", 2);
        Dog dog4 = new Dog("cog3", 3);
        Dog dog5 = new Dog("aog1", 1);

        mDogs.add(dog1);
        mDogs.add(dog2);
        mDogs.add(dog3);
        mDogs.add(dog4);
        mDogs.add(dog5);

        Comparator<Dog> comparator = new Comparator<Dog>() {
            @Override
            public int compare(Dog o1, Dog o2) {
                return o2.getAge() - o1.getAge();
            }
        };

        System.out.println("排序之前:");
        for (Dog dog : mDogs) {
            System.out.println(dog.getName());
        }

        // 排序
        Collections.sort(mDogs,comparator);

        System.out.println("排序之后:");
        for (Dog dog : mDogs) {
            System.out.println(dog.getName());
        }

    }
}
  • Collections.sort(list, comparatorState of Qatar方法,通过comparator准绳,完结对list的特定排序。
  • Collections.sort,list中的对象自己完结comparator接口

一、Collections.sort(List list)

使用此API排序,要求javabean实现Comparable接口

Dog类:
public class Dog implements Comparable<Dog> {
    private String name;
    private int age;

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 如果比较的是字符串,则使用String.CompareTo(String s)方法
     */

    // @Override
    // public int compareTo(Dog dog) {
    // return this.getName().compareTo(dog.getName());
    // }

    /**
     * 比较int型和char型
     */
    @Override
    public int compareTo(Dog dog) {
        return this.age - dog.age;
    }
}

对于ArrayList等常用的群集具体业务类,基本上都落到实处了Comparable接口,就能够以用来相比装载的对象实体。

即使对String属性排序,使用String.CompareTo(String s卡塔尔方法开展排序

@Override
    public int compareTo(Dog o) {
        return this.name.compareTo(o.getName());
    }
Student [age=18, name=lucy, id=9]Student [age=20, name=crane, id=6]Student [age=20, name=jack, id=1]Student [age=20, name=jack, id=2]Student [age=25, name=jack, id=7]Student [age=27, name=rose, id=8]Student [age=29, name=lily, id=3]Student [age=30, name=tom, id=4]Student [age=31, name=rose, id=5]
java.util.Collections.sort(java.util.List)

java.util.Collections.sort(java.util.List, java.util.Comparator)
Dog类:
public class Dog  {
    private String name;
    private int age;

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

图片 1comparator

测验结果:
排序之前:
eog4
dog5
bog2
cog3
aog1
排序之后:
aog1
bog2
cog3
eog4
dog5
  • 测试类

二、Collections.sort(List list,Comparator<T> comparator)

选拔此API达成排序,必要实例化Comparator对象,重写compare(Object o1,Object o2)方法

本文由68399皇家赌场发布于呼叫中心培训课程,转载请注明出处:Java集合排序,ArrayList等常见集合的排序问题

关键词: 68399皇家赌场 Android开发 Android知识 常见 ArrayList

上一篇:持续更新中,区别与用法

下一篇:没有了

最火资讯