1. Introduction
In this article, we will be go through the new methods added in Boolean class under package java.lang. Boolean class wraps up the primitive type boolean in an object. It class represents or contains one and only one field whose type is boolean.
Embed from Getty Images2. Content
We will look at 4 new methods added to class Boolean which are logicalOr, logicalAnd, logicalXor and hashCode and how to use these methods.
2.1. logicalOr
logicalOr method accepts two boolean operands and returns its logical or result. Below is the truth table for logical or operation.
p | q | p ∨ q |
T | T | T |
T | F | T |
F | T | T |
F | F | F |
Let’s look at the method.
The method is incredibly simple to use:
boolean b = Boolean.logicalOr(true, false); Assert.assertTrue(b);
But if you look it closely logicalOr is same as the result for a || b. So why was a method added for this? Well, the answer is we can use this method as a convenience method to make the code look more readable using method references. We are here using lambda expression and operating on two boolean parameters:
Boolean b = Stream .of(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) .reduce(Boolean.FALSE, (val1, val2) -> val1 || val2); Assert.assertTrue(b.booleanValue());
Using method references to replace (val1, val2) -> val1 || val2:
Boolean b = Stream .of(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) .reduce(Boolean.FALSE, Boolean::logicalOr); Assert.assertTrue(b.booleanValue());
Method references are quite cleaner than using the boolean operators.
Using method reference and Collectors:
Boolean b = Stream .of(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) .collect( Collectors.reducing( Boolean.FALSE, Boolean::logicalOr)); Assert.assertTrue(b.booleanValue());
Boolean::logicalOr represents an operation of two operands of the same type and returns result of same type as operand. There is a functional interface for this scenario called the BinaryOperator. Boolean::logicalOr operation can be expressed in the BinaryOperator as below:
BinaryOperator binOp = (val1, val2) -> Boolean.logicalOr(val1,val2); BinaryOperator binOp = Boolean::logicalOr;
2.2. logicalAnd
logicalAnd method accepts two boolean operands and returns its logical and result. Below is the truth table for logical and operation.
p | q | p ∧ q |
T | T | T |
T | F | F |
F | T | F |
F | F | F |
Method Usage
boolean b = Boolean.logicalAnd(true, false); Assert.assertTrue(b);
Using method references:
Boolean b = Stream .of(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) .reduce(Boolean.TRUE, Boolean::logicalAnd); Assert.assertTrue(b.booleanValue());
Using method reference and Collectors:
Boolean b = Stream .of(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) .collect( Collectors.reducing( Boolean.TRUE, Boolean::logicalAnd)); Assert.assertTrue(b.booleanValue());
Boolean::logicalAnd operation can be expressed in BinaryOperator as below:
BinaryOperator<Boolean> binOp = (val1, val2) -> Boolean.logicalAnd(val1, val2); BinaryOperator<Boolean> binOp = Boolean::logicalAnd;
2.3. logicalXor
logicalXor method accepts two boolean operands and returns its logical xor result. Below is the truth table for logical xor operation taken from Wikipedia.
p | q | p ⊕ q |
T | T | F |
T | F | T |
F | T | T |
F | F | F |
Method Usage
boolean b = Boolean.logicalXor(true, false); Assert.assertTrue(b);
Using method references:
Boolean b = Stream .of(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) .reduce(Boolean.FALSE, Boolean::logicalXor); Assert.assertFalse(b.booleanValue());
Using method reference and Collectors:
Boolean b = Stream .of(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) .collect( Collectors.reducing( Boolean.FALSE, Boolean::logicalXor)); Assert.assertFalse(b.booleanValue());
Boolean::logicalXor operation can be expressed in BinaryOperator as below:
BinaryOperator<Boolean> binOp = (val1, val2) -> Boolean.logicalXor(val1,val2); BinaryOperator<Boolean> binOp = Boolean::logicalAnd;
2.4. hashCode
The new hashCode method added is a public static method which returns the same results as hashCode() which is if value is true then it returns 1231 else 1237.
Assert.assertEquals(1231, Boolean.hashCode(true)); Assert.assertEquals(1237, Boolean.hashCode(false));
3. Conclusion
We saw how to use the new methods added Boolean class and how to use them in terms of Stream and a Collector. Using method reference for this boolean operations is quite a clean way rather than using &&, ||, ^ operators.