一、方法定义
public static int compare(boolean x, boolean y)
- 所属类:
java.lang.Boolean - 参数:
x:第一个布尔值y:第二个布尔值
- 返回值:
0:如果x == y1:如果x为true且y为false-1:如果x为false且y为true
- 静态方法:无需创建
Boolean实例即可调用
🔁 排序规则:
false < true
二、功能说明
Boolean.compare() 是一个静态比较方法,用于比较两个 boolean 值的大小关系,常用于:
- 排序逻辑(如
Comparator) - 需要返回
int类型比较结果的场景 - 替代手动写
if-else判断布尔值顺序
它基于布尔值的“自然顺序”:
👉 false 被认为小于 true
三、示例代码
示例 1:基本使用
int result1 = Boolean.compare(true, true); // 0
int result2 = Boolean.compare(true, false); // 1
int result3 = Boolean.compare(false, true); // -1
int result4 = Boolean.compare(false, false); // 0
System.out.println(result1); // 0
System.out.println(result2); // 1
System.out.println(result3); // -1
示例 2:用于排序(Comparator 中)
List<Boolean> flags = Arrays.asList(true, false, true, false);
// 按 false < true 排序
flags.sort((b1, b2) -> Boolean.compare(b1, b2));
System.out.println(flags); // [false, false, true, true]
示例 3:自定义排序逻辑(true 在前)
// 让 true 排在前面
flags.sort((b1, b2) -> Boolean.compare(b2, b1)); // 反向比较
System.out.println(flags); // [true, true, false, false]
示例 4:结合对象列表排序
class User {
String name;
boolean active;
User(String name, boolean active) {
this.name = name;
this.active = active;
}
// getter...
}
List<User> users = Arrays.asList(
new User("Alice", false),
new User("Bob", true),
new User("Charlie", false)
);
// 按 active 字段排序:未激活(false) 在前
users.sort((u1, u2) -> Boolean.compare(u1.active, u2.active));
// 或:激活(true) 在前
users.sort((u1, u2) -> Boolean.compare(u2.active, u1.active));
四、使用技巧
✅ 技巧 1:替代 if-else 写法
❌ 传统写法:
int compare(boolean a, boolean b) {
if (a == b) return 0;
else if (a) return 1;
else return -1;
}
✅ 推荐写法:
int result = Boolean.compare(a, b);
更简洁、可读性强。
✅ 技巧 2:用于三元表达式或条件逻辑
String order = Boolean.compare(flag1, flag2) > 0 ? "first is larger" : "second is larger or equal";
✅ 技巧 3:与 Comparator.comparing() 结合(Java 8+)
users.sort(Comparator.comparing(user -> user.active));
// 等价于
users.sort((u1, u2) -> Boolean.compare(u1.active, u2.active));
⚠️ 注意:
comparing()要求字段是Boolean包装类,boolean基本类型不支持自动装箱用于comparing。
如果 active 是 Boolean 类型:
users.sort(Comparator.comparing(User::getActive));
五、常见错误
| 错误 | 说明 | 修复方式 |
|---|---|---|
误用于 Boolean 对象比较 |
Boolean.compare(b1, b2) 要求 boolean,传 Boolean 会自动拆箱 |
确保对象非 null |
null 的 Boolean 拆箱导致 NPE |
Boolean.compare(flag1, flag2) 中若 flag1 == null,会抛 NullPointerException |
先判空或使用 Objects.equals() |
| 期望字符串比较 | 试图比较 "true" 字符串 |
先转换为 boolean |
六、注意事项
- 参数是
boolean基本类型,不是Boolean包装类(但自动拆箱支持)。 - 不支持
null:如果传入的Boolean对象为null,拆箱会抛NullPointerException。 - 返回值含义固定:
1:第一个更大(即true > false)-1:第二个更大0:相等
- 不可用于
equals判断:如需判断相等,使用==或equals()。 - 性能高:内部是简单条件判断,无对象创建。
七、最佳实践
| 实践 | 说明 |
|---|---|
| ✅ 用于排序逻辑 | 特别是在 Comparator 中 |
| ✅ 替代复杂的 if-else 比较 | 提升代码简洁性 |
✅ 与 boolean 字段配合使用 |
高效、安全 |
✅ 避免对 null 的 Boolean 调用 |
先判空或使用 Optional |
| ✅ 在工具类中封装布尔排序逻辑 | 提高复用性 |
八、性能优化
- 性能极高:
Boolean.compare()是static final方法,内联优化好,执行极快。 - 无对象创建:直接操作
boolean值,无装箱开销。 - 推荐用于循环或高频调用场景,如数据排序、过滤。
对比:
// ❌ 慢且复杂
int cmp = (a && !b) ? 1 : (!a && b) ? -1 : 0;
// ✅ 快且清晰
int cmp = Boolean.compare(a, b);
九、扩展:与 Boolean.compareTo() 的区别
| 方法 | 类型 | 参数类型 | 是否支持 null |
|---|---|---|---|
Boolean.compare(boolean, boolean) |
静态方法 | boolean |
不适用(基本类型) |
booleanValue().compareTo(Boolean) |
实例方法 | Boolean |
支持 null 参数(返回 1 或 -1) |
示例:
Boolean b1 = true;
Boolean b2 = null;
// b1.compareTo(b2) → NullPointerException(拆箱时)
// Boolean.compare(b1, b2) → 同样 NPE
// 安全比较 null 的方式:
// 使用 Objects.compare()
int safe = Objects.compare(b1, b2, Boolean::compare); // 可处理 null
✅ 推荐结合
Objects.compare()处理可能为null的情况。