In my data class I have
public final data class Customer(
val customerId: String,
val customerName: String,
val email: List<Email>?
)
public final data class Email(
val emailId: String,
val emailAddress: Set<String?>? = null
)
My function
fun process(incomingCustomer: Customer) {
//I want to check if incomingCustomer has the email object in it and I have something like
if(customer.email?.isNullOrEmpty() == true){
prin("no Email found")
}
}
I was curious if there is a better way to check if the incomingCustomer has the email object
fun <T> Collection<T>?.isNullOrEmpty()is an extension function on a nullable type, so you should call it without?..email?.isNullOrEmpty()won't be invoked at all ifemailisnull(thus,email?.isNullOrEmpty() == trueis false asemail?.isNullOrEmpty()is alsonull)So,
if (customer.email.isNullOrEmpty())works good in your case