-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Null Safety
-
Section 4: Object-Oriented Kotlin
-
Section 5: Functional Kotlin
-
Section 6: Coroutines
-
Section 7: Collections Deep Dive
-
Section 8: Type System Deep Dive
-
Section 9: Interop and Tooling
-
Section 10: Kotlin DSLs and Patterns
-
Section 11: Testing and Quality
-
Section 12: Server-Side Kotlin
-
Section 13: Practical Projects
-
Section 14: Interview Practice
-
Section 15: More Practice Exercises
-
Section 16: More Standard Library
-
Section 17: Multiplatform Kotlin
-
Section 18: kotlin.collections In Depth
-
Section 19: kotlin.text In Depth
-
Section 20: kotlin.ranges and kotlin.sequences
-
Section 21: kotlin.io and File Handling
-
Section 22: kotlinx.coroutines Deep Dive
-
Section 23: kotlin.reflect
-
Section 24: Android Development with Kotlin Overview
-
Section 25: Kotlin Multiplatform Deep Dive
-
Section 26: Kotlin for Backend Deep Dive
-
Section 27: Kotlin Design Patterns
-
Section 28: Advanced Language Features
-
Section 29: More Practice Exercises
-
Section 30: More Interview Practice
-
Section 31: Kotlin Type System Deep Dive
-
Section 32: Kotlin Null Safety Advanced
-
Section 33: Kotlin Testing Deep Dive
-
Section 34: Kotlin Build Tooling Deep Dive
-
Section 35: Kotlin Serialization
-
Section 36: Kotlin Performance Considerations
-
Section 37: Kotlin Native Overview
-
Section 38: Kotlin for Data and Scripting
-
Section 39: More Coroutines Practice
-
Section 40: More Android-Adjacent Patterns
-
Section 41: More Practical Projects
-
Section 42: More Design and Architecture Practice
-
Section 43: Kotlin Language Evolution
-
Section 44: More Interview and Review
-
Section 45: Kotlin Delegation Patterns Deep Dive
-
Section 46: Kotlin Annotations Deep Dive
-
Section 47: Kotlin for Gradle Plugin Development
-
Section 48: Kotlin Concurrency Beyond Coroutines
-
Section 49: Kotlin Compiler Internals
-
Section 50: Real-World Kotlin Case Studies
-
Section 51: Final Practice Projects
-
Section 52: Kotlin for Server-Side Reactive Programming
-
Section 53: More Practice and Drills
-
Section 54: Kotlin Security Practices
74: Practice Exercise: Refactoring Java Code Idiomatically into Kotlin
I've seen this happen in almost every professional transition from Java to Kotlin: a developer discovers the "Convert Java File to Kotlin File" shortcut in IntelliJ, hits the button, and assumes the job is done. They see the code is now in a .kt file, it compiles, and the tests pass, so they check it into the repo.
The Converter Button is the Finish Line
The misconception here is that the automated converter produces idiomatic Kotlin. It doesn't. It produces Java written with Kotlin syntax. The converter is designed to ensure the code is functionally identical and safe, which means it often preserves Java's verbosity, manual null checks, and imperative loop structures because that's the safest way to migrate without breaking logic.
// A typical Java method
public List<User> getActiveAdmins(List<User> users) {
List<User> admins = new ArrayList<>();
if (users != null) {
for (User user : users) {
if (user != null && user.isAdmin() && user.isActive()) {
admins.add(user);
}
}
}
return admins;
}
If you run this through the converter, you'll get something like this:
fun getActiveAdmins(users: List<User>?): List<User> {
val admins = ArrayList<User>()
if (users != null) {
for (user in users) {
if (user != null && user.isAdmin && user.isActive) {
admins.add(user)
}
}
}
return admins
}
It's valid Kotlin, sure. But it's boring. It's clunky. It ignores everything that makes Kotlin powerful. I call this "Java-ish" code, and it's a smell that tells me the developer hasn't actually embraced the language yet.
Hunting for Expressions and Functional Pipelines
To move from "Java-ish" to "Idiomatic," you have to stop thinking in terms of instructions (do this, then do that) and start thinking in terms of transformations. In the example above, we aren't actually "managing a list"—we are filtering a stream of data.
When I refactor this, I look for three things: the ability to turn the function into a single expression, the removal of explicit null-check blocks, and the replacement of for loops with collection functions. Here is how that same method looks when written by someone who actually wants to use Kotlin:
fun getActiveAdmins(users: List<User>?): List<User> =
users?.filterNotNull()
?.filter { it.isAdmin && it.isActive }
?: emptyList()
Notice the difference? We've collapsed ten lines of boilerplate into a single expression. We used the safe call operator (?.) and the Elvis operator (?:) to handle the nullability of the list, and we used filter to handle the logic. It's more declarative; it tells the reader what is happening rather than how the computer should move pointers around in memory.
As you go through this practice exercise, don't just look for syntax errors. Look for "Java-isms." If you see a for loop, ask yourself if map, filter, or any could replace it. If you see an if (x != null) block, try to use ?.let { ... } or a safe call. That's where the real learning happens.
📋 Practical Task
Refactoring the Legacy OrderProcessor
You have been handed a legacy Java class used for processing customer orders. It works, but it is verbose and follows old Java patterns. Your task is to rewrite the processOrders method in the OrderProcessor class to be idiomatic Kotlin.
The Legacy Java Code:
public class OrderProcessor {
public List<Order> getHighValuePaidOrders(List<Order> orders, double minAmount) {
List<Order> result = new ArrayList<>();
if (orders != null) {
for (Order order : orders) {
if (order != null) {
if (order.isPaid() && order.getAmount() >= minAmount) {
result.add(order);
}
}
}
}
return result;
}
}
Requirements for your refactor:
- Convert the method into a single-expression function (remove the
{ return ... }block). - Eliminate the explicit
if (orders != null)check using a safe call and the Elvis operator. - Replace the
forloop and innerifchecks with a functional collection pipeline (e.g.,filter). - Ensure the method returns an empty list if the input
orderslist is null.
There are no comments for now.