Conversion between Kotlin nullable and Java Optional

nullable => Optional

If you're writing code in Kotlin, you'll need to convert it to Java Optional. In such cases, use ʻOptional.ofNullable to convert to ʻOptional. You can also use ʻOptional.of` if it is not nullable.

sample

    class SecurityAuditorAware: AuditorAware<Long> {
        override fun getCurrentAuditor(): Optional<Long> {
            val authentication = SecurityContextHolder.getContext().authentication

            if (authentication == null || !authentication.isAuthenticated) {
                return Optional.ofNullable(null)
            }

            return Optional.ofNullable((authentication.principal as UserDetail?)?.user?.id)
        }
    }

From Java's point of view, Kotlin's nullable looks the same as it isn't, so ʻOptional <Long?>, ʻOptional <Long>, ʻOptional ?, And ʻOptional <Long?>? Is the same. You can use those four patterns for the return type of getCurrentAuditor () in the sample.

Optional => nullable / not-nullable

Using findById in a Spring 5 repository will return ʻOptional . In such cases, you may want to extract the contents from ʻOptional to pass a value to Kotlin.

optionalValue.get() => not-nullable
optionalValue.orElse(null) => nullable

Other

There are many other methods available for values of type ʻOptional`.

It was described in detail in the next article. https://qiita.com/tasogarei/items/18488e450f080a7f87f3

Recommended Posts

Conversion between Kotlin nullable and Java Optional
Differences between "beginner" Java and Kotlin
Relationship between kotlin and java access modifiers
[For beginners] Difference between Java and Kotlin
[Java] Difference between == and equals
Mutual conversion between Java objects and JSON using Moshi
Mutual conversion between Function and Consumer
[Java] Difference between Hashmap and HashTable
[JAVA] Difference between abstract and interface
[Java] Relationship between H2DB and JDBC
[Java] Difference between array and ArrayList
Differences between Java and .NET Framework
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
Difference between final and Immutable in Java
[Swift] Mutual conversion between Int and Data
Java study # 3 (type conversion and instruction execution)
[Java] Differences between instance variables and class variables
A Java engineer compared Swift, Kotlin, and Java.
[Note] Cooperation between Java and DB (basic)
[Java] Difference between Intstream range and rangeClosed
[Java] Calculation mechanism, operators and type conversion
Difference between int and Integer in Java
HashMap # putAll () behaves differently between Java 7 and Java 8
[Java] Understand the difference between List and Set
Java type conversion
Kotlin functions and lambdas to send to Java developers
Java switch statement and break, Kotlin when expression ...
Optional Type conversion and return value (null support)
Java Optional type
Difference between next () and nextLine () in Java Scanner
Studying Java 8 (Optional)
Summarize the differences between C # and Java writing
Java and JavaScript
XXE and Java
Distinguish between positive and negative numbers in Java
[Java] Difference between "final variable" and "immutable object"
[Java] Optional memorandum
Java 9 Optional :: stream
Organize your own differences in writing comfort between Java lambda expressions and Kotlin lambda expressions.
Please note the division (division) of java kotlin Int and Int
Java language from the perspective of Kotlin and C #
Write a class in Kotlin and call it in Java
[Java] Difference between static final and final in member variables
[JAVA] What is the difference between interface and abstract? ?? ??
I want to transition screens with kotlin and java!
[Java] [Kotlin] Generically call valueOf and values of Enum
About synchronized and Reentrant Lock (Java & Kotlin implementation example)
What is the difference between Java EE and Jakarta EE?
[Java beginner] Difference between length and length () ~ I don't know ~
[Java] Full-width ⇔ half-width conversion
Getters and setters (Java)
[Java] Thread and Runnable
Java true and false
[Java] String comparison and && and ||
View Java Optional Javadoc
[Java, Kotlin] Type Variance
Java --Serialization and Deserialization
[Java] Arguments and parameters
Switch between JDK 7 and JDK 8