Java:Comparator接口

public interface Comparator<T>

接口里面的方法

int    compare(T o1, T o2)
o1 > o2 返回 1
o1 = o2 返回 0
o1 < o2 返回 -1

boolean    equals(Object obj)
判断是否相等

其他方法:https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html


强行对某个对象 collection 进行整体排序 的比较函数。可以将 Comparator 传递给 sort 方法(如 Collections.sortArrays.sort),从而允许在排序顺序上实现精确控制。还可以使用 Comparator 来控制某些数据结构(如有序 set有序映射)的顺序,或者为那些没有自然顺序的对象 collection 提供排序。

将数组中偶数排的后面

实现Comparator接口

class myComparator implements Comparator<Integer>{
    public int compare(Integer a,Integer b){
        return a%2==0?1:-1;
    }
}

 Comparator 传递给 sort 方法

package arrays1;
import java.util.Arrays;
import java.util.Comparator;
class myComparator implements Comparator<Integer>{
    public int compare(Integer a,Integer b){
        return a%2==0?1:-1;
    }
}
public class myArray {
    public void mySort(){
        Integer[] A = new Integer[]{2,0,345,234,12,34,23};
        System.out.println(Arrays.toString(A));
        Arrays.sort(A,new myComparator());

        System.out.println(Arrays.toString(A));
    }
    public static void main(String[] args){
        new myArray().mySort();
    }
}

输出结果:

[2, 0, 345, 234, 12, 34, 23]
[23, 345, 2, 0, 234, 12, 34]

上面传递的是一个实例类对象

我们可以根据上节讲的内部类实现

package arrays1;
import java.util.Arrays;
import java.util.Comparator;
public class myArray {
    class innerComparator implements Comparator<Integer>{
        public int compare(Integer a,Integer b){
            return a%2==0?1:-1;
        }
    }
    public void mySort(){
        Integer[] A = new Integer[]{2,0,345,234,12,34,23};
        System.out.println(Arrays.toString(A));

        Arrays.sort(A,new innerComparator());
        
        System.out.println(Arrays.toString(A));
    }
    public static void main(String[] args){
        new myArray().mySort();
    }
}

上面是成员内部类

也可以讲内部类写在方法中,

package arrays1;
import java.util.Arrays;
import java.util.Comparator;

public class myArray {

    public void mySort(){
        Integer[] A = new Integer[]{2,0,345,234,12,34,23};
        System.out.println(Arrays.toString(A));

        Arrays.sort(A,new Comparator<Integer>(){
            public int compare(Integer a,Integer b){
                return a%2==0?1:-1;
            }
        });
        System.out.println(Arrays.toString(A));
    }
    public static void main(String[] args){
        new myArray().mySort();
    }
}

上面输出结果都一样

上面全部程序

package arrays1;
import java.util.Arrays;
import java.util.Comparator;
class myComparator implements Comparator<Integer>{
    public int compare(Integer a,Integer b){
        return a%2==0?1:-1;
    }
}
public class myArray {
    class innerComparator implements Comparator<Integer>{
        public int compare(Integer a,Integer b){
            return b - a;
        }
    }
    public void mySort(){
        Integer[] A = new Integer[]{2,0,345,234,12,34,23};
        System.out.println(Arrays.toString(A));
//        Arrays.sort(A,new myComparator());
//        
//        System.out.println(Arrays.toString(A));
//        Arrays.sort(A,new innerComparator());
//        
//        System.out.println(Arrays.toString(A));
        Arrays.sort(A,new Comparator<Integer>(){
            public int compare(Integer a,Integer b){
                return a%2==0?1:-1;
            }
        });
        System.out.println(Arrays.toString(A));
    }
    public static void main(String[] args){
        new myArray().mySort();
    }
}
View Code

对人按照年龄排序

定义People类

class People{
    int sex;
    int age;
    String name;
    People(int sex,int age,String name){
        this.sex = sex;
        this.age = age;
        this.name = name;
    }
    public void setSex(int sex){
        this.sex = sex;
    }
    public void setAge(int age){
        this.age = age;
    }
    public void setName(String name){
        this.name = name;
    }
    public int getSex() {
        return sex;
    }
    public int getAge() {
        return age;
    }
    public String getName() {
        return name;
    }
    public String toString(){
        String p = "sex: "+sex+" age: "+ age+" name: "+ name+"
";
        return p;
    }
}
View Code

实现Comparator接口,按照年龄排序,这里通过内部类实现的

public class myArray {
    class innerComparatorAge implements Comparator<People>{
        public int compare(People a,People b){ // 按照年龄升序 
            return a.age - b.age;
        }
    }
    public void mySort(){
        int N = 10;
        People[] A = new People[N];
        Random rand = new Random();
        for(int i = 0;i< N;i++){
            int sex = rand.nextInt(2);
            int age = 10+ rand.nextInt(40);
            String name = ""+rand.nextInt(2000);
            A[i] = new People(sex,age,name);
        }

        System.out.println(Arrays.toString(A));
        System.out.println("排序后:");
        Arrays.sort(A,new innerComparatorAge());
        System.out.println(Arrays.toString(A));
    }
    public static void main(String[] args){
        new myArray().mySort();
    }
}

输出

[sex: 0 age: 23 name: 1067
, sex: 0 age: 16 name: 416
, sex: 1 age: 37 name: 465
, sex: 0 age: 13 name: 1667
, sex: 0 age: 46 name: 1698
, sex: 0 age: 13 name: 528
, sex: 1 age: 21 name: 1558
, sex: 0 age: 18 name: 1386
, sex: 1 age: 49 name: 249
, sex: 1 age: 34 name: 178
]
排序后:
[sex: 0 age: 13 name: 1667
, sex: 0 age: 13 name: 528
, sex: 0 age: 16 name: 416
, sex: 0 age: 18 name: 1386
, sex: 1 age: 21 name: 1558
, sex: 0 age: 23 name: 1067
, sex: 1 age: 34 name: 178
, sex: 1 age: 37 name: 465
, sex: 0 age: 46 name: 1698
, sex: 1 age: 49 name: 249
]

当然也可以修改按照其他方式排序

按照性别排序,当性别相同的时候按照年龄排序

    class innerComparatorSexAge implements Comparator<People>{
        public int compare(People a,People b){ 
            if(a.sex > b.sex)
                return 1;
            else if(a.sex < b.sex)
                return -1;
            else
                return a.age - b.age;
        }
    }

输出

[sex: 0 age: 43 name: 1284
, sex: 1 age: 11 name: 141
, sex: 0 age: 36 name: 1217
, sex: 0 age: 12 name: 1804
, sex: 0 age: 32 name: 1943
, sex: 0 age: 19 name: 1670
, sex: 1 age: 49 name: 656
, sex: 0 age: 36 name: 1349
, sex: 1 age: 13 name: 1542
, sex: 0 age: 18 name: 612
]
排序后:
[sex: 0 age: 12 name: 1804
, sex: 0 age: 18 name: 612
, sex: 0 age: 19 name: 1670
, sex: 0 age: 32 name: 1943
, sex: 0 age: 36 name: 1217
, sex: 0 age: 36 name: 1349
, sex: 0 age: 43 name: 1284
, sex: 1 age: 11 name: 141
, sex: 1 age: 13 name: 1542
, sex: 1 age: 49 name: 656
]

programcreek 中整理的

实现Comparator 对Arrays , ArrayList , TreeSet ,TreeMap ,HashMap 排序

注意:对Map排序需要新建一个Map,将为排序的Map元素放入到新的Map中,这里是通过Key进行排序的

ArrayList

// Collections.sort
List<ObjectName> list = new ArrayList<ObjectName>();
Collections.sort(list, new Comparator<ObjectName>() {
    public int compare(ObjectName o1, ObjectName o2) {
        return o1.toString().compareTo(o2.toString());
    }
});
Arrays
// Arrays.sort
ObjectName[] arr = new ObjectName[10];
Arrays.sort(arr, new Comparator<ObjectName>() {
    public int compare(ObjectName o1, ObjectName o2) {
        return o1.toString().compareTo(o2.toString());
    }
});

TreeSet

// TreeSet
Set<ObjectName> sortedSet = new TreeSet<ObjectName>(new Comparator<ObjectName>() {
    public int compare(ObjectName o1, ObjectName o2) {
        return o1.toString().compareTo(o2.toString());
    }
});
sortedSet.addAll(unsortedSet);

TreeMap

// TreeMap - using String.CASE_INSENSITIVE_ORDER which is a Comparator that orders Strings by compareToIgnoreCase
Map<String, Integer> sortedMap = new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER);
sortedMap.putAll(unsortedMap);
//TreeMap - In general, defined comparator
Map<ObjectName, String> sortedMap = new TreeMap<ObjectName, String>(new Comparator<ObjectName>() {
    public int compare(ObjectName o1, ObjectName o2) {
        return o1.toString().compareTo(o2.toString());
    }
});
sortedMap.putAll(unsortedMap);

给个题目:

组成最大数 

原文地址:https://www.cnblogs.com/bbbblog/p/5456545.html