SAM conversions let you write simple Kotlin code when using Java interfaces with one method. It makes your code shorter and easier to read.
SAM conversions for Java interfaces in Kotlin
val runnable = Runnable { println("Hello from SAM") } Thread(runnable).start()
You write a lambda instead of a full class implementing the interface.
This works only for Java interfaces with exactly one abstract method (SAM = Single Abstract Method).
val runnable = Runnable { println("Run in thread") } Thread(runnable).start()
val comparator = Comparator<String> { a, b -> a.length - b.length } val list = listOf("cat", "elephant", "dog") println(list.sortedWith(comparator))
val action = java.awt.event.ActionListener { e -> println("Button clicked") } button.addActionListener(action)
This program shows how to use SAM conversions for Runnable and Comparator Java interfaces in Kotlin. It runs a thread and sorts a list.
fun main() { val runnable = Runnable { println("Hello from SAM conversion") } Thread(runnable).start() val comparator = Comparator<String> { a, b -> a.length - b.length } val words = listOf("apple", "banana", "kiwi") val sorted = words.sortedWith(comparator) println(sorted) }
SAM conversions only work with Java interfaces, not Kotlin interfaces.
Behind the scenes, Kotlin creates an object implementing the interface for you.
This feature helps keep Kotlin code concise when working with Java libraries.
SAM conversions let you use lambdas for Java interfaces with one method.
This makes Kotlin code shorter and easier when calling Java APIs.
Use it for Runnable, Comparator, listeners, and other single-method interfaces.
Practice
What does SAM conversion allow you to do in Kotlin when working with Java interfaces?
Solution
Step 1: Understand SAM conversion purpose
SAM conversion lets Kotlin treat Java interfaces with a single abstract method as functional interfaces.Step 2: Identify how it simplifies code
This allows using lambda expressions instead of verbose anonymous classes for such interfaces.Final Answer:
Use a lambda expression instead of an anonymous class for interfaces with one method -> Option DQuick Check:
SAM conversion = Lambda for single-method Java interfaces [OK]
- Thinking SAM works for interfaces with multiple methods
- Confusing SAM conversion with Kotlin function to Java class conversion
- Assuming SAM converts Java interfaces to Kotlin classes
Which of the following is the correct Kotlin syntax to use SAM conversion with the Java Runnable interface?
val r = ???Solution
Step 1: Recall SAM conversion syntax for Runnable
Runnable is a Java interface with one method run(), so Kotlin allows lambda syntax: Runnable { ... }Step 2: Check each option's syntax
Runnable { println("Running") } correctly uses Runnable { println("Running") }. Others misuse method calls or lambda syntax.Final Answer:
Runnable { println("Running") } -> Option AQuick Check:
Runnable lambda = Runnable { ... } [OK]
- Trying to call run() method explicitly in lambda
- Using arrow syntax incorrectly inside Runnable
- Confusing method reference with lambda syntax
What will be the output of this Kotlin code using SAM conversion with Comparator?
val list = listOf("apple", "banana", "cherry")
val sorted = list.sortedWith(Comparator { a, b -> b.length - a.length })
println(sorted)Solution
Step 1: Understand the Comparator lambda
The lambda compares strings by length descending: b.length - a.length.Step 2: Sort the list by string length descending
"banana" (6), "apple" (5), "cherry" (6). Since "banana" and "cherry" have same length, their order depends on original order.Step 3: Determine sorted order
Between "banana" and "cherry", "banana" comes first in original list, so it stays first. Then "cherry", then "apple" last.Final Answer:
["banana", "apple", "cherry"] -> Option CQuick Check:
Sorted by length descending = [banana, apple, cherry] [OK]
- Assuming ascending order instead of descending
- Mixing up a and b in comparator lambda
- Ignoring stable sort behavior for equal lengths
Identify the error in this Kotlin code using SAM conversion with a Java interface ActionListener:
val listener = ActionListener { event ->
println("Clicked")
}
button.addActionListener(listener)Solution
Step 1: Check if ActionListener is a Java interface with one method
ActionListener is a Java interface with a single method actionPerformed(ActionEvent).Step 2: Verify SAM conversion applicability
SAM conversion applies only to Java interfaces with exactly one abstract method, so it should work here.Step 3: Identify the actual error
If the code does not compile, it might be because the Kotlin compiler does not recognize ActionListener as a SAM interface due to missing Java interop or import.Final Answer:
SAM conversion works only if the interface is a Java interface with one method -> Option AQuick Check:
SAM conversion requires Java single-method interface [OK]
- Assuming Kotlin interfaces support SAM conversion
- Thinking parameter name must be fixed
- Believing anonymous objects are always required
You want to use SAM conversion to simplify this Java interface usage in Kotlin:
public interface Task {
void execute(int times);
}Which Kotlin lambda expression correctly uses SAM conversion for Task?
Solution
Step 1: Confirm Task is a Java interface with one method
Task has one method execute(int times), so SAM conversion applies.Step 2: Write correct lambda syntax for SAM conversion
The lambda must accept one parameter (times) matching the method signature: Task { times -> ... }Step 3: Check each option
Task { times -> println("Executed $times times") } correctly uses Task { times -> println(...) }. Others misuse method calls or omit parameter.Final Answer:
Task { times -> println("Executed $times times") } -> Option BQuick Check:
SAM lambda matches method parameters = Task { times -> println("Executed $times times") } [OK]
- Calling method name explicitly in lambda
- Omitting lambda parameter when method requires one
- Using wrong method name in lambda expression
Ready to prove it?
Pass the test and this pattern moves unknown
