How to simplify a null-safe compareTo() implementation?

前端 未结 17 2117
醉酒成梦
醉酒成梦 2020-11-28 18:03

I\'m implementing compareTo() method for a simple class such as this (to be able to use Collections.sort() and other goodies offered by the Java pl

17条回答
  •  没有蜡笔的小新
    2020-11-28 18:50

    In case anyone using Spring, there is a class org.springframework.util.comparator.NullSafeComparator that does this for you as well. Just decorate your own comparable with it like this

    new NullSafeComparator(new YourComparable(), true)

    https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/comparator/NullSafeComparator.html

提交回复
热议问题