本文详解如何为自定义类(如 movie)实现 `comparable` 接口,按多个字段(先 type 后 id)升序排序,并确保 null 值被视为最大值。
要实现题目中期望的排序效果——即优先按 type 升序排列,type 相同时再按 id 升序排列,且所有 null 值排在末尾——关键在于正确重写 compareTo() 方法,并妥善处理 null 安全比较。
✅ 正确实现 compareTo()(推荐使用 Objects.compare())
Java 7+ 提供了 java.util.Objects 工具类,其中 Objects.compare(T a, T b, Comparator super T> c) 可安全处理 null:当任一参数为 null 时,它默认将 null 视为大于非 null 值(符合题设“null is greater”的要求)。结合 Comparator.nullsLast() 或直接利用其内置逻辑,可写出简洁、健壮的比较逻辑:
import java.util.Comparator;
import java.util.Objects;
static class Movie implements Comparable {
Integer id;
Type type;
Movie(Integer id, Type type) {
this.id = id;
this.type = type;
}
@Override
public int compareTo(Movie other) {
// 第一优先级:type(枚举天然可比,null 排最后)
int typeCmp = Objects.compare(this.type, other.type, Comparator.nullsLast(Comparator.naturalOrder()));
if (typeCmp != 0) return typeCmp;
// 第二优先级:id(Integer 自然顺序,null 排最后)
return Objects.compare(this.id, other.id, Comparator.nullsLast(Comparator.naturalOrder()));
}
// getters...
}
? 说明:Comparator.nullsLast(Comparator.naturalOrder()) 明确声明“null 排在非 null 之后”,语义清晰、零空指针风险,是处理 null 的最佳实践。
⚠️ 手动实现(不推荐,仅作理解参考)
若需手动控制逻辑(例如兼容旧 JDK),可显式判断 null:
@Override
public int compareTo(Movie other) {
// type 比较:null > 非null
if (this.type == null && other.type == null) {
// both null → continue to next field
} else if (this.type == null) {
return 1; // this null → greater
} else if (other.type == null) {
return -1; // other null → this is smaller
} else {
int typeCmp = this.type.compareTo(other.type);
if (typeCmp != 0) return typeCmp;
}
// id 比较:同理
if (this.id == null && other.id == null) {
return 0;
} else if (this.id == null) {
return 1;
} else if (other.id == null) {
return -1;
} else {
return this.id.compareTo(other.id);
}
}
该写法冗长易错,应优先使用 Objects.compare(…, nullsLast(…))。
立即学习“Java免费学习笔记(深入)”;