Why can't you write it like this?

 Comparator<Item> sort = Comparator.comparing(Item::isOpen);

Underneath Boolean.compareTo is called, which in turn is the same as Boolean.compare

public static int compare(boolean x, boolean y) {
    return (x == y) ? 0 : (x ? 1 : -1);
}

And this: Comparator.comparing returns int and not "Item". make little sense, Comparator.comparing must return a Comparator<T>; in your case it correctly returns a Comparator<Item>.

Answer from Eugene on Stack Overflow
Top answer
1 of 3
9

Why can't you write it like this?

 Comparator<Item> sort = Comparator.comparing(Item::isOpen);

Underneath Boolean.compareTo is called, which in turn is the same as Boolean.compare

public static int compare(boolean x, boolean y) {
    return (x == y) ? 0 : (x ? 1 : -1);
}

And this: Comparator.comparing returns int and not "Item". make little sense, Comparator.comparing must return a Comparator<T>; in your case it correctly returns a Comparator<Item>.

2 of 3
6

The overloads comparingInt, comparingLong, and comparingDouble exist for performance reasons only. They are semantically identical to the unspecialized comparing method, so using comparing instead of comparingXXX has the same outcome, but might having boxing overhead, but the actual implications depend on the particular execution environment.

In case of boolean values, we can predict that the overhead will be negligible, as the method Boolean.valueOf will always return either Boolean.TRUE or Boolean.FALSE and never create new instances, so even if a particular JVM fails to inline the entire code, it does not depend on the presence of Escape Analysis in the optimizer.

As you already figured out, reversing a comparator is implemented by swapping the argument internally, just like you did manually in your lambda expression.

Note that it is still possible to create a comparator fusing the reversal and an unboxed comparison without having to repeat the isOpen() expression:

Comparator<Item> sort = Comparator.comparingInt(i -> i.isOpen()? 0: 1);

but, as said, it’s unlikely to have a significantly higher performance than the Comparator.comparing(Item::isOpen).reversed() approach.


But note that if you have a boolean sort criteria and care for the maximum performance, you may consider replacing the general-purpose sort algorithm with a bucket sort variant. E.g.

If you have a Stream, replace

List<Item> result = /* stream of Item */
    .sorted(Comparator.comparing(Item::isOpen).reversed())
    .collect(Collectors.toList());

with

Map<Boolean,List<Item>> map = /* stream of Item */
    .collect(Collectors.partitioningBy(Item::isOpen,
                                       Collectors.toCollection(ArrayList::new)));
List<Item> result = map.get(true);
result.addAll(map.get(false));

or, if you have a List, replace

list.sort(Comparator.comparing(Item::isOpen).reversed());

with

ArrayList<Item> temp = new ArrayList<>(list.size());
list.removeIf(item -> !item.isOpen() && temp.add(item));
list.addAll(temp);

etc.

🌐
Apache Commons
commons.apache.org › proper › commons-collections › apidocs › org › apache › commons › collections4 › comparators › BooleanComparator.html
BooleanComparator (Apache Commons Collections 4.5.0 API)
java.lang.Object · org.apache... BooleanComparator extends Object implements Comparator<Boolean>, Serializable · A Comparator for Boolean objects that can sort either true or false first....
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Comparator.html
Comparator (Java Platform SE 8 )
October 20, 2025 - The ordering imposed by a comparator c on a set of elements S is said to be consistent with equals if and only if c.compare(e1, e2)==0 has the same boolean value as e1.equals(e2) for every e1 and e2 in S.
🌐
Spring
docs.spring.io › spring-framework › docs › current › javadoc-api › org › springframework › util › comparator › BooleanComparator.html
BooleanComparator (Spring Framework 6.2.11 API)
java.lang.Object · org.spring... BooleanComparator extends Object implements Comparator<Boolean>, Serializable · A Comparator for Boolean objects that can sort either true or false first....
🌐
GeeksforGeeks
geeksforgeeks.org › java › boolean-compare-method-in-java-with-examples
Boolean compare() method in Java with Examples - GeeksforGeeks
July 11, 2025 - Below are programs to illustrate ... method public static void main(String[] args) { // first value boolean a = true; // second value boolean b = true; // compare method System.out.println(a + " comparing with " + b + " = " + Boolean.compare(a, ...
🌐
Spring
docs.spring.io › spring-framework › docs › 5.0.0.M4_to_5.0.0.M5 › Spring Framework 5.0.0.M5 › org › springframework › util › comparator › BooleanComparator.html
BooleanComparator
A Comparator for Boolean objects that can sort either true or false first. ... comparing, comparing, comparingDouble, comparingInt, comparingLong, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, ...
🌐
Javatpoint
javatpoint.com › java-boolean-compare-method
Java Boolean compare() Method with Examples - Javatpoint
booleanValue() compare() compareTo() Java Boolean equals () Method · getBoolean() hashCode() logicalAnd() logicalOr() logicalXor() parseBoolean() toString() valueOf() Java Byte ·
🌐
Tabnine
tabnine.com › home page › code › java › java.lang.boolean
java.lang.Boolean.compare java code examples | Tabnine
/** * 比较 * * @param other 其它 {@link MutableBool} 对象 * @return x==y返回0,x&lt;y返回-1,x&gt;y返回1 */ @Override public int compareTo(final MutableBool other) { return Boolean.compare(this.value, other.value); }
Find elsewhere
🌐
Educative
educative.io › answers › what-is-the-boolean-compare-method-in-java
What is the Boolean compare() method in Java?
The static compare() method is from class Boolean. We use this method to compare two boolean values and check their equality. Class Boolean is imported from the java.lang.Boolean package, which impletmets the Serializable and Comparable<Boolean> ...
🌐
Baeldung
baeldung.com › home › java › guide to java comparator.comparing()
Guide to Java Comparator.comparing() | Baeldung
January 8, 2024 - This article is a guide to several features introduced in Java 8 for the Comparator interface.
🌐
BeginnersBook
beginnersbook.com › 2014 › 07 › compare-boolean-values-in-java-compareto-method
Compare boolean values in java – compareTo() Method
class CompareBooleanValues { public static void main(String[] args) { // Creating Objects of Boolean class Boolean bObj = new Boolean("true"); // Case does not matter Boolean bObj2 = new Boolean("FaLsE"); Boolean bObj3 = new Boolean("true"); // Comparing values using compareTo() method /* public ...
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java boolean compareto method
Java Boolean compareTo Method Explained
September 1, 2008 - The Java Boolean compareTo(Boolean b) compares this Boolean instance with another.
🌐
Educative
educative.io › answers › how-to-compare-two-boolean-values-using-booleanutils-in-java
How to compare two boolean values using BooleanUtils in Java
We use the compare() method of the BooleanUtils class to compare two boolean values. The method takes two values and returns true if both the values are the same.
🌐
Quora
quora.com › Why-cant-we-compare-boolean-objects-in-Java
Why can't we compare boolean objects in Java? - Quora
Answer (1 of 4): Why cant you compare Boolean objects ? if b1 and b2 are two Boolean object references you can always write code like: if (b1 == b2) Also Boolean implements Comparable and hence has to provide implementattion to the compareTo method (Boolean (Java Platform SE 7 ) ) In addition...
🌐
SourceForge
andromda.sourceforge.net › andromda-utils › jacoco › org.andromda.utils.beans.comparators › BooleanComparator.java.html
BooleanComparator.java
* @param objectA * @param objectB * @return compare result */ public int compare( Object objectA, Object objectB) { Boolean aAsBoolean = (Boolean)objectA; Boolean bAsBoolean = (Boolean)objectB; int result = 0; if (aAsBoolean.booleanValue() && !bAsBoolean.booleanValue()) { result = 1; } else if (!aAsBoolean.booleanValue() && bAsBoolean.booleanValue()) { result = -1; } return result; } }
🌐
Apache Commons
commons.apache.org › proper › commons-collections › jacoco › org.apache.commons.collections4.comparators › BooleanComparator.java.html
BooleanComparator.java
* * @param trueFirst when {@code true}, sort * {@code true} boolean values before {@code false} */ public BooleanComparator(final boolean trueFirst) { this.trueFirst = trueFirst; } /** * Compares two non-{@code null} {@code Boolean} objects * according to the value of {@link #sortsTrueFirst()}. * * @param b1 the first boolean to compare * @param b2 the second boolean to compare * @return negative if obj1 is less, positive if greater, zero if equal * @throws NullPointerException when either argument {@code null} */ @Override public int compare(final Boolean b1, final Boolean b2) { final boolean v1 = b1.booleanValue(); final boolean v2 = b2.booleanValue(); return v1 ^ v2 ?
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Check if Two Booleans Are Equal In Java - Java Code Geeks
January 7, 2025 - Use Boolean.compare() when you need a more general-purpose comparison method that handles boolean values with return values. Choosing the right approach depends on the types of variables you’re working with and the requirements of your program. Understanding these methods will allow you to write efficient and readable Java code for boolean value comparison.
🌐
Medium
medium.com › @alskor › comparing-boolean-and-boolean-in-java-fef83ff3ad40
Comparing boolean and Boolean in Java | by Alex Sko | Medium
August 11, 2016 - in other words, a properly implemented JDK will not create object instances for some primitive values, including true/false. and equality operator will unbox Boolean before comparing it to anything.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.boolean.compare
Boolean.Compare(Boolean, Boolean) Method (Java.Lang) | Microsoft Learn
Compares two boolean values. The value returned is identical to what would be returned by: ... Added in 1.7. Java documentation for java.lang.Boolean.compare(boolean, boolean).