Last active
July 21, 2016 14:02
-
-
Save takaki/af8d6a2a23cd3603281551480a78bcca to your computer and use it in GitHub Desktop.
ScalaからJavaのlambdaを簡単に使う ref: http://qiita.com/takaki@github/items/09916abad7fa2ddf09d1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Optional | |
object JavaLambda { | |
def main(args: Array[String]) { | |
val s = Optional.of("Scala") | |
val l = s.map((s:String)=>s.concat("!!!")).get() // Error!! | |
println(l) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Optional | |
import java.util.function.Function | |
object JavaLambda { | |
def main(args: Array[String]) { | |
val s = Optional.of("Scala") | |
val mapper = new Function[String, String] { | |
override def apply(v1: String):String = v1.concat("!!!") | |
} | |
val l = s.map(mapper).get() | |
println(l) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Optional | |
import scala.compat.java8.FunctionConverters._ | |
object Java8Lambda { | |
def main(args: Array[String]) { | |
val s = Optional.of("Scala") | |
val l = s.map(((v: String) => v.concat("!!!")).asJava).get() | |
println(l) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment