零からの

ゆるーく綴るブログ

Java Comparatorを使用したソート処理

ビジネスロジックでリストのソート処理を実装しなければならないことってあると思います。
そういう時に、いちいち調べなおしたりしないよう、ここにメモしておきます。

Comparatorとは

オブジェクトのコレクションに対して任意のソート順を設定するUtilクラスのこと。
詳しくはJavadoc参照。

Comparator (Java Platform SE 8)

サンプルコード

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

// 身長の低い順にソートを行うサンプル
public class Main {
    public static void main(String[] args) throws Exception {
        // JDK1.7以前
        // --------------------------------------------------
        List<Human> humanList = Arrays.asList(//
                new Human("一郎", 180, 75), new Human("二郎", 170, 65), new Human("三郎", 175, 65), new Human("四郎", 165, 55));

        Collections.sort(humanList, new Comparator<Human>() {
            @Override
            public int compare(Human o1, Human o2) {
                return o1.getHeight() - o2.getHeight();
            }
        });

        for (Human human : humanList) {
            System.out.println(human.getName());
        }

        // JDK1.8 - その1
        // --------------------------------------------------
        humanList = Arrays.asList(//
                new Human("一郎", 180, 75), new Human("二郎", 170, 65), new Human("三郎", 175, 65), new Human("四郎", 165, 55));

        // 上記のものをラムダ式で置き換えたもの
        Collections.sort(humanList, (o1, o2) -> o1.getHeight() - o2.getHeight());

        for (Human human : humanList) {
            System.out.println(human.getName());
        }

        // JDK1.8 - その2
        // --------------------------------------------------
        humanList = Arrays.asList(//
                new Human("一郎", 180, 75), new Human("二郎", 170, 65), new Human("三郎", 175, 65), new Human("四郎", 165, 55));

        // 1.8からListインタフェースに導入されたsortメソッドを使用した場合
        humanList.sort((o1, o2) -> o1.getHeight() - o2.getHeight());

        for (Human human : humanList) {
            System.out.println(human.getName());
        }

    }
}

/**
 * ソート用のオブジェクトサンプル
 */
class Human {

    /** 名前 */
    private String name;
    /** 身長 */
    private int height;
    /** 体重 */
    private int weight;

    /**
     * コンストラクタ
     * 
     * @param name 名前
     * @param height 身長
     * @param weight 体重
     */
    public Human(String name, int height, int weight) {
        this.name = name;
        this.height = height;
        this.weight = weight;
    }

    /**
     * @return 名前
     */
    public String getName() {
        return name;
    }

    /**
     * @param name 名前
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return 身長
     */
    public int getHeight() {
        return height;
    }

    /**
     * @param height 身長
     */
    public void setHeight(int height) {
        this.height = height;
    }

    /**
     * @return 体重
     */
    public int getWeight() {
        return weight;
    }

    /**
     * @param weight 体重
     */
    public void setWeight(int weight) {
        this.weight = weight;
    }
}

実行結果(結果はすべて同じです)

四郎
二郎
三郎
一郎

終わりに

Javadocを眺めて思ったんですが、JDK1.8からcomparingなどいろいろなメソッドが追加されているようです。
これらについても後々整理しようと思います。