DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Kotlin Null Safety for the Optional Experience

Kotlin Null Safety for the Optional Experience

Here, we'll go through several of Kotlin's null safety operators to see how you can bring your Optional experience over to Kotlin.

Paweł Szeliga user avatar by
Paweł Szeliga
·
Jan. 30, 18 · Tutorial
Like (11)
Save
Tweet
Share
41.78K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, I will try to map methods of Java’s Optional to Kotlin’s similar, scattered language features and built-in functions. The code in the examples is written in Kotlin because the language has all the JDK classes available.

Representation

Let’s start with the representation. Optional usage requires creating a new object for the wrapper every time some value is wrapped or transformed to another type — with the exclusion of when the Optional is empty (singleton empty Optional is used). In Kotlin, there is no additional overhead. The language uses plain old null. The code below shows both approaches:

val kotlinNullable: String? = "Kotlin way, this can be null"

val javaNullable: Optional<String> = Optional.ofNullable("Java way, can be null as well”)


Map Using The Method of The Inner Value’s Type

To transform the value inside Optional using the inner value’s method we can apply a method reference to map. To do the same in Kotlin, we can use safe call operator (?.) as demonstrated below:

val lengthKotlin: Int? = kotlinNullable?.length

val lengthJava: Optional<Int> = javaNullable.map(String::length)


Map Using an External Mapper

If the transformation cannot be performed by a simple method call, then Optional’s map method is happy to take a lambda as well. Kotlin provides the built-in method let, which we can invoke on any object. In our case, we also need to use the safe call operator to skip the calculation for null values:

val lengthKotlin: Int? = kotlinNullable?.let { external.computeLength(it) }

val lengthJava: Optional<Int> = javaNullable.map { external.computeLength(it) }


Filter

In Optional filter allows us to remove the value inside if the provided predicate test returns false. Kotlin offers two built-in functions with this behavior — takeIf and takeUntil. The first is similar to the Optional’s filter while the second one drops the value if the predicate returns true — the opposite to takeIf. Example code:

val kotlinFiltered: String? = kotlinNullable.takeIf(predicate) 
val kotlinFiltered2: String? = kotlinNullable.takeUnless(predicate) 

val javaFiltered: Optional<String> = javaNullable.filter(predicate)


FlatMap

There is no built-in Kotlin function with the flatMap behavior because it’s actually not necessary. Nullability is not represented by a type and simple mapping turns out to be working fine.

IsPresent

In Kotlin, this check can be performed by a simple null comparison:

val kotlinNotNull: Boolean = kotlinNullable != null     

val javaNotNull: Boolean = javaNullable.isPresent()


It is worth to know that after we do this, the compiler is sure that the value is present and allows us to drop all required null checks in further code (String? becomes String).

if (kotlinNullable != null) {                    // String?     
    val notNullable: String = kotlinNullable     // String from this line 
}


Get

Both the Optional and Kotin approaches discourage users from getting the inside value with a straight call because it may cause an NPE. Kotlin introduces rude (!!) operator, which you should use only as a last resort:

val notSafeGetKotlin: String = kotlinNullable!!

val notSafeGetJava: String = javaNullable.get()


OrElse/OrElseThrow

Kotlin introduces the elvis operator (?:), which allows us to set the default value in case of null or when throwing an exception:

val kotlinValue: String = kotlinNullable?: ""
val javaValue: String = javaNullable.orElse("")

val kotlinExc: String = kotlinNullable ?: throw RuntimeException()
val javaExc: String = javaNullable.orElseThrow { RuntimeException() }


Conclusion

I hope reading this article will help you leverage your Optional experience to quickly learn Kotlin's null safety features. I think it is worth giving Kotlin a try if only to expand your programming horizons.

Kotlin (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Handling Automatic ID Generation in PostgreSQL With Node.js and Sequelize
  • Handling Virtual Threads
  • Efficiently Computing Permissions at Scale: Our Engineering Approach
  • 3 Examples of Address Autofill Forms to Collect Shipping and Payment Addresses

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: