how to check if two Boolean? values are true in Kotlin

kotlin boolean example
kotlin elvis operator
kotlin int to boolean
kotlin function return boolean
kotlin boolean not
kotlin convert boolean to string
kotlin if true then
kotlin boolean primitive

Sample code:

val todayCount = keyValue.value.filter {
        val after = it.expectedArrivalDate?.after(today)
        val before = it.expectedArrivalDate?.before(tomorrow)
        after != null && before != null && after && before
    }.size

If it.expectedArrivalDate were not nullable I would write something like that:

val todayCount = keyValue.value.filter {
    it.expectedArrivalDate.after(today) && it.expectedArrivalDate.before(tomorrow)
}.size

Is it possible to simplify my code?

You can create an extension functions to simplify that check.

Assuming that the type of expectedArrivalDate is Date, you could add after and before functions for the nullable Date type Date? that would return false if the instance is null, or call the original after/before functions if not null:

fun Date?.after(anotherDate: Date): Boolean = this?.after(anotherDate) ?: false
fun Date?.before(anotherDate: Date): Boolean = this?.before(anotherDate) ?: false

and leave your code as it is:

val todayCount = keyValue.value.filter {
    it.expectedArrivalDate.after(today) && it.expectedArrivalDate.before(tomorrow)
}.size

or you could just use the implementation directly in your code:

val todayCount = keyValue.value.filter {
    (it.expectedArrivalDate?.after(today) ?: false) && (it.expectedArrivalDate?.before(tomorrow) ?: false)
}.size

How to check if two boolean values are equal?, The == operator works with booleans. boolean isEqual = (bool1 == bool2);. (The parentheses are unnecessary, but help make it easier to read.). t.and(t): true t.and(f): false f.and(t): false f.and(f): false t.or(t): true t.or(f): true f.or(t): true f.or(f): false not(t): false not(f): true xor : It performs the logical xor operation and returns the result boolean value.

An alternative to extension functions or Elvis operators is simply == true.

In Java, where == compares references, that would be dangerous, as it would fail for any new Boolean instances.  But in Kotlin, == compares values using .equals() (after making the necessary null checks), so would handle that case perfectly well.

So your code could be:

val todayCount = keyValue.value.filter {
    it.expectedArrivalDate?.after(today) == true
    && it.expectedArrivalDate?.before(tomorrow) == true
}.size

Similarly, if you wanted null to count as true, you could use != false.

It's arguably not the most elegant solution, but it's probably the simplest!

Boolean, Represents a value which is either true or false . On the JVM, non-nullable values of this type are represented as values of the primitive type boolean . class Boolean : Comparable < Boolean > Represents a value which is either true or false . On the JVM, non-nullable values of this type are represented as values of the primitive type boolean .

Consider using variable re-declaration using ?:

variable re-declaration (and shadowing).

fun leftPad(value: String, length: Int? = null, char: Char? = null): String {
    val length = length ?: 4
    val char = char ?: ' '

Is the idiom for checking nullable booleans helpful?, In the official documentation, we find this idiom: val b: Boolean? because b == true wouldn't even type check there: can't compare null to a values of a primitve type. 2 Likes. reitzig November 19, 2019, 9:03pm #4. Good thought about the� anyMatch () method takes a predicate, an expression or a function which returns a boolean value. In our case, the predicate compares each element n in the stream to toFind and returns true or false. If any of the element n returns true, found is set to true as well. Example 3: Check if array contains a given value for non-primitive types

Not sure if this can be considered a simplification but as another option you can:

val todayCount = keyValue.value.filter {
    it.expectedArrivalDate?.takeIf{d -> d.after(today)}?.takeIf{d -> d.before(tomorrow)}?.let{true}?:false
}.size

BooleanArray, Opt-in Requirements. Core Libraries. Standard Library � kotlin.test. Reference operator fun set(index: Int, value: Boolean) Returns true if at least one element matches the given predicate. Returns true if element is found in the array. Returns a set containing all elements that are contained by both this array and the� In Kotlin, we have different ways to do that. Let’s check them one by one : Using any : any can be used to check one condition as a predicate in an array. It takes one predicate and returns one Boolean value based on the predicate. It is defined as below : fun <t> Array<t>.any(): Boolean. We can call it directly on an array. For example :

An alternative without an extension function: as all comparisons are based on the same field, you can actually map it while removing all nulls.

Here's a sample code with a dummy travel class so that you can see it compiles:

data class Travel(var expectedArrivalDate: Date? = null);

lateinit var keyValue: Map.Entry<String, List<Travel>>;
val today = Date.from(Instant.now())
val tomorrow = Date.from(Instant.now().plusSeconds(86400))

val todayCount = keyValue.value.mapNotNull(Travel::expectedArrivalDate).count {
    it.after(today) && it.before(tomorrow)
}

Gets the solution down to 125 characters and gets close to the code you had in mind.

Equality, Referential equality (two references point to the same object); To provide a custom equals check implementation, override the equals(other: Any?): Boolean function. a === b evaluates to true if and only if a and b point to the same object . For values which are represented as primitive types at runtime (for example, Int ) � There are two things to notice when using when as an expression in Kotlin. First, the value that is returned to the caller is the value of the matching case block or in other words the last defined value in the block.

Boolean compare() method in Java with Examples, JQuery � SQL � PHP � Scala � Perl � Go Language � HTML � CSS � Kotlin The compare() method of Boolean class is a built in method in Java which is used to Parameters: It takes two boolean values a and b in the parameter which are to be 0 if 'a' is equal to 'b',; a negative value if 'a'is false and 'b' is true,; a positive� val check = true val result = when (check) { true -> println ("it's true") false -> println ("it's false") } Given that check has a Boolean type it can only have to possible values, so the two branches cover all cases and this when expression is guaranteed to assign a valid value to result.

Boolean, kotlin.Any. ↳, java.lang.Boolean Allocates a Boolean object representing the value true if the string argument is not null and is (Beginning with version 1.0.2 of the JavaTM platform, the test of this string is case insensitive.)� How to use when statements to choose an option based on a given value. What the Boolean data type is and how to use its true and false values for decision making. What you'll build. A Kotlin-based dice game, Lucky Dice Roll, which allows you to specify a lucky number. A player will win if they roll the lucky number. What you need

Equality in Kotlin ('==', '===' and 'equals'), === operator is used to compare the reference of two variable or object. It will only be true if both the objects or variables pointing to the same object. The negated� the value of the bool variable is true the value of the a variable is false Code logic: The code is to show the use of a Boolean variable. There are two boolean variables defined a and bool. bool is created without specifying the data type and a is created with data type and two if statements check for their value that leads to the generation

Comments
  • What results do you get for a date that is after today and before tomorrow?
  • smth like this (it.expectedArrivalDate?.after(today) ?? false) && (it.expectedArrivalDate?.before(tomorrow) ?? false) ?
  • Use count instead of filter {...}.size (kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/…)
  • The solution with Elvis operator is perfect. Thank you.
  • I personally was surprised that this priority is correct. I'd really prefer to write (it.expectedArrivalDate?.after(today) ?: false) && ....
  • This is the most elegant and easy-to-read solution, in my opinion. By the way, in the OP, expectedArrivalDate is nullable.
  • @Tenfour04 Thanks; now corrected. I hesitate to recommend this because it's not entirely obvious. Here ?. is a strong hint, but it's not always so clear that the LHS could be null, and that the apparently-unnecessary comparison is to handle that case. (I've spent so long removing genuinely unnecessary such comparisons from other people's dodgy code, that it still seems like a code smell!) — And even then, you still have to stop and think which way the null case will go. So while it's clearly the shortest solution, it's not ideal. Though probably none are!
  • @gidds thanks for your answer. Your code actually returns Boolean? instead of Boolean that is required by filter