扩展技术Android知识Android开发

Java集合排序

2016-11-21  本文已影响321人  RaphetS

Java API提供了两种方法来排序:

java.util.Collections.sort(java.util.List)
java.util.Collections.sort(java.util.List, java.util.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;
    }
}

测试类:
import java.util.ArrayList;
import java.util.Collections;
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);

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

        // 排序
        Collections.sort(mDogs);

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

    }
}
测试结果:
排序之前:
eog4
dog5
bog2
cog3
aog1
排序之后:
aog1
bog2
cog3
eog4
dog5

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

使用此API实现排序,需要实例化Comparator对象,重写compare(Object o1,Object o2)方法

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;
    }

}

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());
        }

    }
}

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

如果对String属性排序,使用String.CompareTo(String s)方法进行排序

@Override
    public int compareTo(Dog o) {
        return this.name.compareTo(o.getName());
    }

三、对数组进行排序

如果数组或集合元素是String类型,则可以利用Java API实现的Comparator<String>对象String.CASE_INSENSITIVE_ORDER为容器元素排序。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test {

    public static void main(String[] args) {

        String[] array=new String[]{"c","b","d","a","e"};
        List<String> list=Arrays.asList(array);
        
        System.out.println("排序之前:");
        for (String string : list) {
            System.out.println(string);
        }
        
        Collections.sort(list,String.CASE_INSENSITIVE_ORDER);
        
        System.out.println("排序之后:");
        for (String string : list) {
            System.out.println(string);
        }
        
        Collections.sort(list,Collections.reverseOrder());
        System.out.println("反序:");
        for (String string : list) {
            System.out.println(string);
        }
        
    }
}

测试结果:

排序之前:
c
b
d
a
e
排序之后:
a
b
c
d
e
反序:
e
d
c
b
a

上一篇下一篇

猜你喜欢

热点阅读