Groovy for Scala Devs

Learn about the differences between Scala and Java commands.

Java vs. Scala

Feature

Java

Scala

Public Class

public class

class

Loops

for(Type it : c){…}

c.foreach {…}

Lists

List list = asList(1,2,3);

val list = List(1,2,3)

Maps

Map m = …; m.put(x,y);

val m = Map(x -> y)

Function Def.

void method(Type t) {}

def method(t: Type) = {}

Mutable Value

Type t

var t: Type

Immutable Value

final Type t

val t: Type

Null safety

(x == null ? null : x.y)

for (a <- Option(x)) yield a.y

Null replacement

(x == null ? “y” : x)

Option(x) getOrElse “y”

Sort

Collections.sort(list)

list.sort(_ < _)

Wildcard import

import java.util.*;

import scala.collection._

Var-args

(String… args) 

(args: String*)

Type parameters

Class<T>

Class[T]

Concurrency

Fork/Join

Akka

...

Get hands-on with 1400+ tech skills courses.