From Java to Scala : some examples

Here is little article discussing about how to turn a Java code into a Scala code.

Java code :

import java.util.List;
 
public class Bob {
 
    private final List<String> preferedMatters;
    private final List<String> neutralMatters;
    private final List<String> hatedMatters;
 
    public Bob(List<String> preferedMatters, List<String> neutralMatters, List<String> hatedMatters) {
        this.preferedMatters = preferedMatters;
        this.neutralMatters = neutralMatters;
        this.hatedMatters = hatedMatters;
    }
 
    public String listen(String something) {
 
        if (preferedMatters.stream()
                           .anyMatch(s -> something.toLowerCase()
                                                   .contains(s))) {
            return "Oh Yes";
        } else if (neutralMatters.stream()
                                 .anyMatch(s -> something.toLowerCase()
                                                         .contains(s))) {
            return "Ok";
        } else if (hatedMatters.stream()
                               .anyMatch(s -> something.toLowerCase()
                                                       .contains(s))) {
            return "Terrible";
        }
 
        throw new IllegalArgumentException("No!!!");
    }
 
    public List<String> getPreferedMatters() {
        return preferedMatters;
    }
 
    public List<String> getNeutralMatters() {
        return neutralMatters;
    }
 
    public List<String> getHatedMatters() {
        return hatedMatters;
    }
 
 
}

Scala version using a Java 8 style code :

class Bob(preferedMatters: List[String], neutralMatters: List[String], hatedMatters: List[String]) {
  def listen(something: String) : String = {
 
    if (preferedMatters.exists(something.toLowerCase().contains(_))){
      return "Oh Yes"
    }
 
    else if (neutralMatters.exists(something.toLowerCase().contains(_))){
      return "Ok"
    }
 
    else if (hatedMatters.exists(something.toLowerCase.contains(_))){
      return "Terrible"
    }
 
    throw new IllegalArgumentException("No!!!")
  }
 
  def getPreferedMatters() = preferedMatters
 
  def getNeutralMatters() = neutralMatters
 
  def getHatedMatters = hatedMatters

We see that even with a Java 8 style, Scala is more readable/concise thanks to :
– constructor defined and field inhered from the class declaration
– the special character _ to represent the lambda parameter
– no need to stream the list to use high order level functions, here exists(p : A => Boolean)

And with a Scala style : pattern matching combined with guard/conditional, it is still better :

  def listen(something: String): String = {
 
    something match {
      case x if (preferedMatters.exists(something.toLowerCase().contains(_))) => "Oh Yes"
      case x if (neutralMatters.exists(something.toLowerCase().contains(_))) => "Ok"
      case x if (hatedMatters.exists(something.toLowerCase().contains(_))) => "Terrible"
      case _ => throw new IllegalArgumentException("No!!!")
    }
 
  }
Ce contenu a été publié dans Non classé. Vous pouvez le mettre en favoris avec ce permalien.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *