I have recently stumbled upon SAM (single abstract method) concept in Java.
I program in Scala and to me, they look the same as anonymous functions (or lambdas), however my IntelliJ suggests me to either use SAM or lambdas in different scenarios.
Example 1:
val x = new Function1[Int, Int] { // here usage of lambda is suggested
override def apply(x: Int): Int = x * 2
}
Here lambda usage is suggested.
Example 2:
val event: EventHandler[ActionEvent] = new EventHandler[ActionEvent]() { // here usage of SAM is suggested
override def handle(e: ActionEvent): Unit = {
println("hi")
}
}
I struggle to see the difference in syntax between an anonymous function and a SAM. Are there any conceptual or functional differences between the two?