Comparator 와 Comparable 예제

3888 단어 자바
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

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

/**
 * @author xiechao01
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ComparableDemo implements Comparable {

    private int compareA = 0;
    private int compareB = 0;

    @Override
    public int compareTo(ComparableDemo o) {
        if (this.compareA > o.compareA) {
            return 1;
        } else if (this.compareA == o.compareA) {
            return Integer.compare(this.compareB, o.compareB);
        } else {
            return -1;
        }
    }

    public static void main(String[] args) {
        ComparableDemo a = new ComparableDemo(1, 2);
        ComparableDemo b = new ComparableDemo(1, 3);
        ComparableDemo c = new ComparableDemo(1, 4);
        ComparableDemo d = new ComparableDemo(2, 3);
        List list = new ArrayList<>();
        list.add(d);
        list.add(b);
        list.add(a);
        list.add(c);
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);
    }

}

출력 은 A 의 크기 를 우선 순위 로 판단 하고 B 의 순 서 를 판단 합 니 다.
[ComparableDemo(compareA=2, compareB=3), ComparableDemo(compareA=1, compareB=3), ComparableDemo(compareA=1, compareB=2), ComparableDemo(compareA=1, compareB=4)] [ComparableDemo(compareA=1, compareB=2), ComparableDemo(compareA=1, compareB=3), ComparableDemo(compareA=1, compareB=4), ComparableDemo(compareA=2, compareB=3)]  
 
package com.tutor.xiechao.javastudy.comparator;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

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

/**
 * @author xiechao01
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ComparatorDemo {

    private int compareA = 0;
    private int compareB = 0;

    private static final ComingEpisodeComparator compareFun = new ComingEpisodeComparator();

    private static class ComingEpisodeComparator implements java.util.Comparator {
        @Override
        public int compare(ComparatorDemo o1, ComparatorDemo o2) {
            if (o1.getCompareA() > o1.getCompareA()) {
                return 1;
            } else if (o1.getCompareA() == o2.getCompareA()) {
                return 0;
            } else {
                return -1;
            }
        }
    }

    public static void main(String[] args) {
        ComparatorDemo a = new ComparatorDemo(1, 2);
        ComparatorDemo b = new ComparatorDemo(1, 3);
        ComparatorDemo c = new ComparatorDemo(1, 4);
        ComparatorDemo d = new ComparatorDemo(2, 3);
        List list = new ArrayList<>();
        list.add(d);
        list.add(b);
        list.add(a);
        list.add(c);
        System.out.println(list);
        list.sort(Comparator.comparingInt(ComparatorDemo::getCompareB));
        System.out.println(list);
        list.sort(Comparator.comparingInt(ComparatorDemo::getCompareA));
        System.out.println(list);
    }
}

[ComparatorDemo(compareA=2, compareB=3), ComparatorDemo(compareA=1, compareB=3), ComparatorDemo(compareA=1, compareB=2), ComparatorDemo(compareA=1, compareB=4)]
처음으로 B 에 따라 작은 것 부터 큰 것 까지 정렬 [Comparator Demo (compare A = 1, compare B = 2), Comparator Demo (compare A = 2, compare B = 3), Comparator Demo (compare A = 1, compare B = 3), Comparator Demo (compare A = 1, compare B = 4)]
두 번 째 는 A 에 따라 작은 것 부터 큰 것 까지 정렬 [Comparator Demo (compare A = 1, compare B = 2), Comparator Demo (compare A = 1, compare B = 3), Comparator Demo (compare A = 1, compare B = 4), Comparator Demo (compare A = 2, compare B = 3)]]

좋은 웹페이지 즐겨찾기