classOf[XClass]
val b: Boolean = that.isInstanceOf[Date]
val o: Date = that.asInstanceOf[Date]
Thread sleep 1000
override def toString(): String = "xx"
No ternary operator in Scala, instead use an if/else expression:
abs = if (x >= 0) x else -x
String Interpolation
s"${someVariable} ${variable.field}"
f"" - format
raw"some\nAnotherline"
val sb = new StringBuilder
sb ++= "someThing"
Collections
Immutable
Mutable - scala.collection.mutable
ArrayBuffer
- Prepends and removes O(n)
val buf = new ArrayBuffer[Int]()
buf += 12
ListBuffer
- prepend and append O(1)
Convert scala collection to java collection
import collection.JavaConverters._
val m: java.util.Map[String, Int] = HashMap("abc" -> 1, "hello" -> 2).asJava
Exception handling
try {
} catch {
case e: XException => somethin
}
Using Either
Spark ETL: Using Either to handle invalid data
try {
Right(invalidValue)
} catch {
case e: Exception => Left(input)
}
Try{}
Success or Failure
if (result.isSuccess) result.get else defaultValue
result.getOrElse(defaultValue)
Main app
object Main extends App {}
import scala.util.{Left, Right}
import java.math.BigInteger, BigInteger._
Mystery of _
(1 to 10) map { _ * 2 }
(1 to 10).reduceLeft( _ + _ )
Unit - {}
Parallel collections
list.par.map(_ + 1)
Stream.iterate(startValue){i => i-10}.take(limit).foreach(println _)
for(i <- 1 until 5) - iterate 4 times
for(i <- 1 to 5) - iterate 5 times
Check Scala version at runtime (for debug)
scala.util.Properties.versionString
Mix Java/Scala
Use scala-maven-plugin for mixed java/scala projects
Integrate with spring
http://hub.darcs.net/psnively/spring-scala
Autowire Java/Scala Bean
val b: Boolean = that.isInstanceOf[Date]
val o: Date = that.asInstanceOf[Date]
Thread sleep 1000
override def toString(): String = "xx"
No ternary operator in Scala, instead use an if/else expression:
abs = if (x >= 0) x else -x
String Interpolation
s"${someVariable} ${variable.field}"
f"" - format
raw"some\nAnotherline"
val sb = new StringBuilder
sb ++= "someThing"
Collections
Immutable
Mutable - scala.collection.mutable
ArrayBuffer
- Prepends and removes O(n)
val buf = new ArrayBuffer[Int]()
buf += 12
ListBuffer
- prepend and append O(1)
Convert scala collection to java collection
import collection.JavaConverters._
val m: java.util.Map[String, Int] = HashMap("abc" -> 1, "hello" -> 2).asJava
Exception handling
try {
} catch {
case e: XException => somethin
}
Using Either
Spark ETL: Using Either to handle invalid data
try {
Right(invalidValue)
} catch {
case e: Exception => Left(input)
}
Try{}
Success or Failure
if (result.isSuccess) result.get else defaultValue
result.getOrElse(defaultValue)
Main app
object Main extends App {}
import scala.util.{Left, Right}
import java.math.BigInteger, BigInteger._
Mystery of _
(1 to 10) map { _ * 2 }
(1 to 10).reduceLeft( _ + _ )
Unit - {}
Parallel collections
list.par.map(_ + 1)
Stream.iterate(startValue){i => i-10}.take(limit).foreach(println _)
for(i <- 1 until 5) - iterate 4 times
for(i <- 1 to 5) - iterate 5 times
Check Scala version at runtime (for debug)
scala.util.Properties.versionString
Mix Java/Scala
Use scala-maven-plugin for mixed java/scala projects
Integrate with spring
http://hub.darcs.net/psnively/spring-scala
Autowire Java/Scala Bean
@Service
class Sevice{
@Autowired val Repo: Repository = null;
}
@Configuration
class Config {
@Bean def someBean = {}
}
class Sevice{
@Autowired val Repo: Repository = null;
}
@Configuration
class Config {
@Bean def someBean = {}
}
Resources