LoginSignup
1
3

More than 5 years have passed since last update.

【commons-lang3】 ObjectUtils のよく利用する(しそう)メソッド

Posted at

メモ
おもに null safe ( java 8で使わなくなる可能性高いなぁ)


import org.apache.commons.lang3.ObjectUtils;

public class Main {

    public static void main(String[] args) {
        String a = null;
        String b = null;
        String c = "null";

        // 複数のオブジェクトで null ではない一番左の Object を取得する。
        System.out.println(ObjectUtils.firstNonNull(a, b, c));
        ;
        // null safe な object#hashCode
        System.out.println(ObjectUtils.hashCode(c));
        // null safe な object#hashCode ( 複数ハッシュ計算 )
        System.out.println(ObjectUtils.hashCodeMulti(a, b, c));
        // null safe な object#toString
        System.out.println(ObjectUtils.toString(c));
        // null safe な object#toString ( null の場合の文字列指定 )
        System.out.println(ObjectUtils.toString(c, "-"));
        // null safe な compare
        System.out.println(ObjectUtils.compare(a, b));

        // StringUtils#defaultIfNull の Object版
        System.out.println(ObjectUtils.defaultIfNull(b, c));

        // 一致しない
        System.out.println(ObjectUtils.notEqual(b, c));
        // 一致する
        System.out.println(ObjectUtils.equals(a, b));

    }
}
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3