zoukankan      html  css  js  c++  java
  • ComparatorUtil 防止空指针异常

    import com.google.common.collect.Lists;
    import org.apache.commons.lang3.ObjectUtils;

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

    /**
    * @author zad
    * @date 2019/8/29 15:41
    * @description 防止空指针异常的比较器
    */
    public final class ComparatorUtil {
    private ComparatorUtil() {
    throw new UnsupportedOperationException();
    }


    public static <T extends Comparable<? super T>> int comparatorDesc(T c1, T c2) {
    return ObjectUtils.compare(c2, c1);
    }

    public static <T extends Comparable<? super T>> int comparatorDesc(T c1, T c2, boolean nullGreater) {
    return ObjectUtils.compare(c2, c1, nullGreater);
    }


    public static <T extends Comparable<? super T>> int comparatorAsc(T c1, T c2) {
    return ObjectUtils.compare(c1, c2, true);
    }


    public static <T extends Comparable<? super T>> int comparatorAsc(T c1, T c2, boolean nullGreater) {
    return ObjectUtils.compare(c1, c2, nullGreater);
    }

    public static void main(String[] args) {
    List<Integer> list = Lists.newArrayList(null, 1, null, 2, 5, 9, 3);

    Collections.sort(list, new Comparator<Integer>() {
    @Override
    public int compare(Integer o1, Integer o2) {
    return comparatorAsc(o1, o2);
    }
    });

    System.out.println(list);
    }
    }

     
  • 相关阅读:
    HackerRank
    HackerRank
    HackerRank
    LeetCode "Bitwise AND of Numbers Range"
    HackerRank
    HackerRank
    LeetCode "Binary Tree Right Side View"
    HihoCoder
    HihoCoder
    HackerRank
  • 原文地址:https://www.cnblogs.com/zad27/p/11303359.html
Copyright © 2011-2022 走看看