Scala 2.11 to Java 8 : Cheat sheet

It's very uncommon to migrate from Scala to another language. Yet this happens. It happened to me.

During a second job interview, I was asked in advance not to code in Scala but only Java or Python. Certainly because this is what the interviewers understood. Even if I would push a team to start using Scala if I were hired, I cannot push my interviewers to use Scala ...

"Hey please make my interview easier, learn scala, and come back to me".

Instead I had to prove them that I was able to code in Java. That way, It would be much easier to defend switching to Scala later.

Below is the cheat sheet that  I developed in order to be more productive during my interview. It really helped me a lot. Hoping it saves you time too !

Happy coding !

 

In scala In Java 8
Int, Long, Short, Char,
Byte, Boolean, Double
int, long, short, char,
byte, boolean, double
(a: A, b: B) => f(a)
(A a, B b) -> f(a)
(a: A, b: B) => {
   F
   f(a)
}
(A a, B b) -> {
  F;
  return f(a);
}
var x: A = ...
A x = ...;
val x: A = ...
final A x = ...;
var x = new Array[Int](15)
x(0) = 2
val a = x[2]
int[] x = new int[15]
x[0] = 2;
int a = x[2];
var x = Array(1, 2, 3)
int[] x = { 1, 2, 3 };
var m = List(4, 2, 3)

m =  m.sortBy(i => i*i-4*i)
List<Int> m = Arrays.asList(4, 2, 3;
Collections.sort(m, (Int i) -> i*i-4*i);
def theMethod(a: String, b: A => Unit) {
  val theA: A = convert(a)
  b(theA)
}
void theMethod(String a, Block

b) { A theA = convert(a) b.apply(theA) }

theMethod(“test”,
  (a: A) => println(“true”))
theMethod(“test”,
  (A a) -> println(“true”))
for(i &lt collection) {

}
for(Elem i: collection) {

}
val a: Photo => Unit
a(photo)
Consumer<Photo> a
a.accept(photo)
val  a: Photo => Boolean
a(photo)
Predicate<Photo> a = … ;
a.test(photo)
val a: X => Y
a(x)
Function<X, Y> a = …;
a.apply(x)
val photos = List(...)
val output = photos.filter(p => p.sizeKb < 10)
List<Photo> photos = 
  Arrays.asList(...)
List<Photo> output = photos.filter(
  (Photo p)-> p.getSizeInKb()<10);
val photos = photos.filter(p => p.sizeKb > 10000)
            .map(p => p.name)
List<Photo> photos = Arrays.asList(...)
List<Photo> output = photos.filter(
  (Photo p) -> p.getSizeInKb() > 10000)
  .map(p -> p.name);
def heavyComputation = (0 to 10).toList.par.foreach(
  i => heavyComputation(i))
IntStream.range(0,500).boxed()
  .collect(Collectors.toList())
  .parallel().forEach(
  (int i) -> heavyComputation(i))
object Sex extends Enumeration {
  val MALE, FEMALE = Value
}
public enum Sex {
    MALE, FEMALE
}
case class Person(age: Int, name: String)
class Person {
  int age;
  String name;

  Person(int _age, String _name) {
    age = _age
    name = _name
  }
}
trait A {
  def methodToOverride(b: B): C
}
public interface A {
  C methodToOverride(B b);
}
def methodTest(a: A) = ???
methodTest(new A {
  def methodToOverride(b: B) = convertToC(b)
})
methodTest((B b) -> convertToC(b));
.toStream
.stream()
.foreach(lambda)
.forEach(lambda)
def m() = {
  var x = 2
  x = 3
  val consume = (i: Int) => println(i + x)
  consume(5)
}
void m() {
  int x = 2
  int y = 3 // Different variable !
  val consume = (i: Int) => println(i + y)
  consume(5)
}
def m(a: A*) { }
def m(A... a) {}
trait A {
  val log: String => Unit
  def debug(msg: String) = { 
    log(msg)
  }
}
public interface A {
  Consumer<String> log
  void debug(String msg) default { 
    log.accept(msg);
  }
}
month match {
  case 1 => “January”
  case _ => “Invalid month”
}
switch (month) {
    case 1:  monthString = "January";
         break;
    default: monthString = "Invalid month";
                     break;
}
object A {

}
class A {
  static A instance = new A
  private A() {
  }
}
val P= “a(b*)”.r
P.findFirstIn(“abbacabbbbd”).map{
  m =>
   println(s”I found the text${m.group} starting at ${m.start} and ending at index ${m.end} \n”)
}.getOrElse(println(“No match found”))
Pattern P = Pattern.compile(“a(b*)”)
Matcher matcher = P.matcher(“abbacabbbbd”);
boolean found = false;
while (matcher.find()) {
    console.format("I found the text" +
        " \"%s\" starting at " +
        "index %d and ending at index %d.%n",
        matcher.group(),
        matcher.start(),
        matcher.end());
    found = true;
}
val res = for(i 
List<CT> res = A.filter(pred)
  .flatMap((int j) -> B(j))
  .map((K j) -> C(j));
val myNums: ArrayBuffer[T forSome {T 
List<? extends Number> myNums = new ArrayList<Integer>();
val myNums: ArrayBuffer[T forSome {T >: Number}] = ArrayBuffer()
List<? super Number> myNums = new ArrayList()
def copy[U : Number](source: ArrayBuffer[U],
               destiny ArrayBuffer[V]) {
   for(number 
public void copy(List<? extends Number> source,
                        List<? super Number> destiny) {
   for(Number number : source) {
      destiny.add(number);
   }
}
var s = Set[A]()
s += new A
s(a)
s.isEmpty
for(i 
HashSet<A>

s = new HashSet<A>(); s.add(new A); s.contains(a) s.isEmpty() for(A i: ()->s.iterator())

var m = Map[A, B]()
m += a -> b
HashMap<A, B> m = new HashMap<A, B>()
m[a] = b
val m = new concurrent.collection.HashMap()
Map m = Collections.synchronizedMap(new HashMap(...));
val s = m.toSet
Set<Map.Entry<A, B>> s = m.entrySet
val t = (“test”, 2)
t._1
t._2
java.util.Map.Entry t = new java.util.AbstractMap.SimpleEntry<>(“test”, 2)
t.getKey()
t.getValue()