views:

2132

answers:

16

I need some code samples (and I also really curious about them) of Scala and Java code which show that Scala code is more simple and concise then code written in Java (of course both samples should solve the same problem).

If there is only Scala sample with comment like "this is abstract factory in Scala, in Java it will look much more cumbersome" then this is also acceptable.

Thanks!

I like most of all accepted and this answers

+19  A: 

I found this one impressive

Java

public class Person {
    private final String firstName;
    private final String lastName;
    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    public String getFirstName() {
        return firstName;
    }
    public String getLastName() {
        return lastName;
    }
}

Scala

class Person(val firstName: String, val lastName: String)

As well as these ones (sorry for not pasting, I didn't want to steal the code)

stacker
+1. Very nice one!
Roman
This scala code will not generate `getFirstName` and `getLastName` methods. You have to annotate the parameters with `scala.reflect.BeanProperty` annotation to do that.
abhin4v
@abhin4v: Yes, but the code convention in Scala is to not have accessors prefixed with `get`. Idiomatic Java code is different from idiomatic Scala code. Sometimes the `is` prefix used for booleans. http://davetron5000.github.com/scala-style/naming_conventions/methods/accessors_mutators.html
Esko Luontola
You could make that a `case class` and get the `toString`, `equals` and `hashCode` for free (and you also don't have to make the arguments `val` explicitly): `case class Person(firstName: String, lastName: String)`
Jesper
+1  A: 

Lazily evaluated infinite streams are a good example:

object Main extends Application {

   def from(n: Int): Stream[Int] = Stream.cons(n, from(n + 1))

   def sieve(s: Stream[Int]): Stream[Int] =
     Stream.cons(s.head, sieve(s.tail filter { _ % s.head != 0 }))

   def primes = sieve(from(2))

   primes take 10 print

}

Here is a question addressing infinite streams in Java: Is an infinite iterator bad design?

Another good example are first class functions and closures:

scala> def f1(w:Double) = (d:Double) => math.sin(d) * w
f1: (w: Double)(Double) => Double

scala> def f2(w:Double, q:Double) = (d:Double) => d * q * w
f2: (w: Double,q: Double)(Double) => Double

scala> val l = List(f1(3.0), f2(4.0, 0.5))
l: List[(Double) => Double] = List(<function1>, <function1>)

scala> l.map(_(2))
res0: List[Double] = List(2.727892280477045, 4.0)

Java doesn't support first class functions, and mimicking closures with anonymous inner classes isn't very elegant. Another thing this example shows that java can't do is running code from an interpreter/REPL. I find this immensely useful for quickly testing code snippets.

dbyrne
Please note that the sieve is too slow to be practical.
Elazar Leibovich
Where is the Java?
oxbow_lakes
@oxbow_lakes there is no equivalent java for these examples.
dbyrne
@dbyme Not true. You can easily subclass Java's `Iterable` and `Iterator` to produce infinite streams.
Daniel
@Daniel I stand corrected. Answer has been updated.
dbyrne
+31  A: 

Let's improve stacker's example and use Scala's case classes:

case class Person(firstName: String, lastName: String)

The above Scala class contains all features of the below Java class, and some more - for example it supports pattern matching (which Java doesn't have). Scala 2.8 adds named and default arguments, which are used to generate a copy method for case classes, which gives the same ability as the with* methods of the following Java class.

public class Person implements Serializable {
    private final String firstName;
    private final String lastName;

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public Person withFirstName(String firstName) {
        return new Person(firstName, lastName);
    }

    public Person withLastName(String lastName) {
        return new Person(firstName, lastName);
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Person person = (Person) o;
        if (firstName != null ? !firstName.equals(person.firstName) : person.firstName != null) {
            return false;
        }
        if (lastName != null ? !lastName.equals(person.lastName) : person.lastName != null) {
            return false;
        }
        return true;
    }

    public int hashCode() {
        int result = firstName != null ? firstName.hashCode() : 0;
        result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
        return result;
    }

    public String toString() {
        return "Person(" + firstName + "," + lastName + ")";
    }
}

Then, in usage we have (of course):

Person mr = new Person("Bob", "Dobbelina");
Person miss = new Person("Roberta", "MacSweeney");
Person mrs = miss.withLastName(mr.getLastName());

Against

val mr = Person("Bob", "Dobbelina")
val miss = Person("Roberta", "MacSweeney")
val mrs = miss copy (lastName = mr.lastName)
Esko Luontola
You have to be careful though - case classes inherit from `Product` and hence primitives will be boxed (in scala 2.7) or probably-boxed (2.8 using specialization)
oxbow_lakes
In 2.7.x and 2.8.0 the only boxing is in `productElements` and `unapply`, not in the constructor, field, or accessor: http://gist.github.com/424375
retronym
Encourages all sorts of getter/setter badness. Setters should only be added with extreme reluctance, getters should be only added if necessary. Good example of how adding "Simplicity" leads to bad habits.
Bill K
@Bill K: OK, then we will have `case class Person(val firstName: String, val lastName: String)`So what?Making that thing private would be possible too, but doesn't make any sense, because of unapply etc.
soc
+2  A: 

This is a very simple example: Square integers and then add them


    public int sumSquare(int[] list) {
        int s = 0;
        for(int i = 0; i < list.length; i++) {
            s += list[i] * list[i]; 
        }
        return s;
    }

In scala:


val ar = Array(1,2,3)
def square(x:Int) = x * x
def add(s:Int,i:Int) = s+i

ar.map(square).foldLeft(0)(add)

Compact map applies the function to all elements of the array, so:

Array(1,2,3).map(square)
Array[Int] = Array(1, 4, 9)

Fold left is will start with 0 as the accumulator (s) and apply add(s,i) to all the elements (i) of the array, so that:

 Array(1,4,9).foldLeft(0)(add)  // return 14 form 0 + 1 + 4 + 9

Now this can be further compacted to:

Array(1,2,3).map(x => x * x ).foldLeft(0)((s,i) => s + i )

This one I will not try in Java (to much work), turn XML to a Map:


<a>
   <b id="a10">Scala</b>
   <b id="b20">rules</b>
</a>

Another one liner to get the map from the XML:


val xml = <a><b id="a10">Scala</b><b id="b20">rules</b></a>

val map = xml.child.map( n => (n \ "@id").text -> n.child.text).toMap
// Just to dump it.
for( (k,v) <- map) println(k + " --> " + v)
Thomas
The problem with your `sumSquare` in Scala is that it looks very cryptic to a Java developer, which will give them ammo against you to complain that Scala is obscure and complicated...
Jesper
I reformatted a bit to improved the example. Hope that this does not hurt Scala.
Thomas
scala> 1 to 10 map (x => x*x) sumres0: Int = 385Let's see the java developer call that cryptic. At that point it's fingers in the ears saying nah-nah-nah.
extempore
@Jesper To a non-Java developer, Java looks like vast amounts of boilerplate and line noise. That doesn't mean you can't get real work done in the language.
James Moore
You could use reduceLeft(add) instead of foldLeft(0)(add). I think it’s easier to read when your start element is the group’s zero/identity element.
Debilski
@James I agree, but if you want to convince Java developers that Scala is better (as is probably the case with the poster of the question) then it might be better to not start with something that might scare them...
Jesper
+3  A: 

How about Quicksort?


Java

The following is a java example found via a google search,

the URL is http://www.mycstutorials.com/articles/sorting/quicksort

public void quickSort(int array[]) 
// pre: array is full, all elements are non-null integers
// post: the array is sorted in ascending order
{
   quickSort(array, 0, array.length - 1);   // quicksort all the elements in the array
}


public void quickSort(int array[], int start, int end)
{
   int i = start;      // index of left-to-right scan
   int k = end;        // index of right-to-left scan

   if (end - start >= 1)               // check that there are at least two elements to sort
   {
       int pivot = array[start];       // set the pivot as the first element in the partition

       while (k > i)                   // while the scan indices from left and right have not met,
       {
           while (array[i] <= pivot && i <= end && k > i) // from the left, look for the first
              i++;                                        // element greater than the pivot
           while (array[k] > pivot && k >= start && k >= i) // from the right, look for the first
              k--;                                          // element not greater than the pivot
           if (k > i)                  // if the left seekindex is still smaller than
               swap(array, i, k);      // the right index, swap the corresponding elements
       }
       swap(array, start, k);          // after the indices have crossed, swap the last element in
                                       // the left partition with the pivot 
       quickSort(array, start, k - 1); // quicksort the left partition
       quickSort(array, k + 1, end);   // quicksort the right partition
    }
    else // if there is only one element in the partition, do not do any sorting
    {
        return;                        // the array is sorted, so exit
    }
}

public void swap(int array[], int index1, int index2) 
// pre: array is full and index1, index2 < array.length
// post: the values at indices 1 and 2 have been swapped
{
   int temp      = array[index1];      // store the first value in a temp
   array[index1] = array[index2];      // copy the value of the second into the first
   array[index2] = temp;               // copy the value of the temp into the second
}

Scala

A quick attempt at a Scala version. Open season for code improvers ;@)

def qsort(l: List[Int]): List[Int] = {
  l match {
    case Nil         => Nil
    case pivot::tail => qsort(tail.filter(_ < pivot)) ::: pivot :: qsort(tail.filter(_ >= pivot))
  }
}
Don Mackenzie
Does that quicksort on linked lists have O(n^2) time complexity or not? Usually mergesort or similar is used for linked lists.
Esko Luontola
It's also not tail recursive and hence unsuitable as a performant algorithm (or one which will not overflow the stack)
oxbow_lakes
Thanks for the useful comments. I had seen quicksort written like this somewhere and was impressed by it's compactness, clearly I didn't give it much consideration. I got carried away by the LOC comparison, which is always a seductive thing with Scala v Java.
Don Mackenzie
Quicksort is not *O(n^2)* on functional lists, but it certainly has that danger. Asymptotically, it's still average *O(n log n)*, but there is a higher statistical probability of hitting the worst case *O(n^2)* because we always select the pivot point at the head of the list, rather than choosing one at random.
Daniel Spiewak
Filtering twice is bad. See in my answer to your question the use of `partition` to avoid that.
Daniel
+12  A: 

Task: Write a program to index a list of keywords (like books).

Explanation:

  • Input: List<String>
  • Output: Map<Character, List<String>>
  • The key of map is 'A' to 'Z'
  • Each list in the map are sorted.

Java:

import java.util.*;

class Main {
  public static void main(String[] args) {
    List<String> keywords = Arrays.asList("Apple", "Ananas", "Mango", "Banana", "Beer"); 
    Map<Character, List<String>> result = new HashMap<Character, List<String>>(); 
    for(String k : keywords) {   
      char firstChar = k.charAt(0);     
      if(!result.containsKey(firstChar)) {     
        result.put(firstChar, new  ArrayList<String>());   
      }     
      result.get(firstChar).add(k); 
    } 
    for(List<String> list : result.values()) {   
      Collections.sort(list); 
    }
    System.out.println(result);         
  }
}

Scala:

object Main {
  def main(args: Array[String]) {
    val keywords = List("Apple", "Ananas", "Mango", "Banana", "Beer")
    val result = keywords.groupBy(_(0)).mapValues(_.sorted)
    println(result)
  }
}
missingfaktor
You can use v.sorted instead of (v sortBy identity).
Eastsun
@Eastsun, Thanks, edited.
missingfaktor
And with Scala 2.8, you can use mapValues (_.sorted) instead of map { case ... }
Alex Boisvert
+2  A: 

I liked this simple example of sorting and transformation, taken from David Pollak's 'Beginning Scala' book:

In Scala:

def validByAge(in: List[Person]) = in.filter(_.valid).sortWith(_.age < _.age).map(_.first)
case class Person(val first: String, val last: String, val age: Int) {def valid: Boolean = age > 18}
validByAge(List(Person("John", "Valid", 32), Person("John", "Invalid", 17), Person("OtherJohn", "Valid", 19)))

In Java:

public static List<String> validByAge(List<Person> in) {
   List<Person> valid = new ArrayList<Person>();
   for (Person p: in) {
     if (p.valid()) valid.add(p);
   }
   Collections.sort(people, new Comparator<Person>() {
      public int compare(Person a, Person b) {
        return a.age() - b.age();
      } 
   } );
   List<String> ret = new ArrayList<String>();
     for (Person p: people) {
       ret.add(p.first);
     }
   return ret;
}

public class Person {
    private final String firstName;
    private final String lastName;
    private final Integer age;
    public Person(String firstName, String lastName, Integer age) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }
    public String getFirst() {
        return firstName;
    }
    public String getLast() {
        return lastName;
    }
    public Integer getAge() {
       return age;
    }
    public Boolean valid() {
       return age > 18;
    }
}

List<Person> input = new ArrayList<Person>();
input.add(new Person("John", "Valid", 32));
input.add(new Person("John", "InValid", 17));
input.add(new Person("OtherJohn", "Valid", 19));

List<Person> output = validByAge(input)

Arjan Blokzijl
+4  A: 

Problem: you need to design a method that will execute any given code asynchronously.

Solution in Java:

/**
* This method fires runnables asynchronously
*/
void execAsync(Runnable runnable){
    Executor executor = new Executor() {
        public void execute(Runnable r) {
            new Thread(r).start();
        }
    };
    executor.execute(runnable);
}

...

execAsync(new Runnable() {
            public void run() {
                ...   // put here the code, that need to be executed asynchronously
            }
});

The same thing in Scala (using actors):

def execAsync(body: => Unit): Unit = {
  case object ExecAsync    
  actor {
    start; this ! ExecAsync
    loop {
      react {           
        case ExecAsync => body; stop
      }
    }
  }    
}

...

execAsync{  // expressive syntax - don't need to create anonymous classes
  ...  // put here the code, that need to be executed asynchronously    
}
Vasil Remeniuk
As of 2.8, this can be written asFutures.future{body}and is actually more powerful since the future returned by this can be joined to get the value it eventually evaluates to.
Dave Griffith
+1  A: 

This Scala code...

def partition[T](items: List[T], p: (T, T) => Boolean): List[List[T]] = {
  items.foldRight[List[List[T]]](Nil)((item: T, items: List[List[T]]) => items match {
    case (first :: rest) :: last if p (first, item) =>
      (List(item)) :: (first :: rest) :: last
    case (first :: rest) :: last =>
      (item :: first :: rest) :: last
    case _ => List(List(item))
  })
}

...would be completely unreadable in Java, if possible at all.

JUST MY correct OPINION
@JUST MY correct OPINIO: thanks for the answer! but could you please explain what happens there? I'm not familiar with Scala syntax yet, and (that's the possible reason why) it looks completely unreadable even now for me.
Roman
It's partitioning a generic list of type T using a provided partitioning function as a guard in the pattern matching clauses of the case statement.
JUST MY correct OPINION
Weird. I'm not even remotely a Scala expert and can figure that out.
JUST MY correct OPINION
+2  A: 

Why nobody posted this before:

Java

class Hello {
     public static void main( String [] args ) {
          System.out.println("Hello world");
     }
 }

118 characters.

Scala

object Hello extends Application {
     println("Hello world")
 }

66 characters.

OscarRyz
`Application` trait considered harmful... http://www.scala-blogs.org/2008/07/application-trait-considered-harmful.html
missingfaktor
+4  A: 

A map of actions to perform depending on a string.

Java:

// strategy pattern = syntactic cruft resulting from lack of closures
public interface Todo {   
  public void perform();
}

final Map<String, Todo> todos = new HashMap<String,Todo>();
todos.put("hi", new Todo() { 
    public void perform() { 
        System.out.println("Good morning!");
    } 
} );

final Todo todo = todos.get("hi");
if (todo != null)
    todo.perform();
else
    System.out.println("task not found")

Scala:

val todos = Map( "hi" -> { () => println("Good morning!") } )
val defaultFun = {() => println("task not found")}; 
todos.getOrElse("hi", defaultFun).apply()

And it's all done in the best possible taste!

Ben Hardy
@Rahul G, I think your edit is incorrect. `todos.get("hi")` returns `Option[()=>Unit]` which is needed to match properly.
huynhjl
@huynhjl, My bad. Rolled it back.
missingfaktor
Can be even shorter: `val defaultFun = {() => println("task not found")}; todos.getOrElse("hi", defaultFun).apply()`
Geoff Reedy
awesome, thanks Geoff. Adding your suggestion.
Ben Hardy
+6  A: 

Task:

You have got an XML file "company.xml" that looks like this:

<?xml version="1.0"?>
<company>
    <employee>
        <firstname>Tom</firstname>
        <lastname>Cruise</lastname>
    </employee>
    <employee>
        <firstname>Paul</firstname>
        <lastname>Enderson</lastname>
    </employee>
    <employee>
        <firstname>George</firstname>
        <lastname>Bush</lastname>
    </employee>
</company>

You have to read this file and print the firstName and lastName fields of all the employees.


Java: [ taken from here ]

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlReader {
  public static void main(String[] args) {   
    try {
      File file = new File("company.xml");
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.parse(file);
      doc.getDocumentElement().normalize();
      NodeList nodeLst = doc.getElementsByTagName("employee");
      for (int s = 0; s < nodeLst.getLength(); s++) {  
        Node fstNode = nodeLst.item(s); 
        if (fstNode.getNodeType() == Node.ELEMENT_NODE) {         
          Element fstElmnt = (Element) fstNode;
          NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("firstname");
          Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
          NodeList fstNm = fstNmElmnt.getChildNodes();
          System.out.println("First Name: "  + ((Node) fstNm.item(0)).getNodeValue());
          NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("lastname");
          Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
          NodeList lstNm = lstNmElmnt.getChildNodes();
          System.out.println("Last Name: " + ((Node) lstNm.item(0)).getNodeValue());
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}



Scala: [ taken from here, slide #19 ]

import xml.XML

object XmlReader {
  def main(args: Array[String]): Unit = {
    XML.loadFile("company.xml") match {
      case <employee> { employees @ _* } </employee> => {
        for(e <- employees) {
          println("First Name: " + (e \ "firstname").text)
          println("Last Name: " + (e \ "lastname").text)
        } 
      }
    }
  }
}

[ EDIT by Bill; Check comments for the discussion ] --

Hmm, how to do it without replying in an unformatted reply section... Hmph. I guess I'll edit your answer and let you delete it if it bugs you.

This is how I would do it in Java with better libraries:

public scanForEmployees(String filename) {
    GoodXMLLib source=new GoodXMLLib(filename);
    while( String[] employee: source.scanFor("employee", "firstname", "lastname") )
    {
          System.out.println("First Name: " + employee[0]);
          System.out.println("Last Name: " + employee[1]);
    }
} 

This is just a quick hack involving no magic and all reusable components. If I wanted to add some magic I could do something better than returning an array of string arrays, but even as is this GoodXMLLib would be completely reusable. The first parameter of scanFor is the section, all future parameters would be the items to find which is limited, but the interface could be buffed slightly to add multiple levels of matching with no real problem.

I will admit that Java has some pretty poor library support in general, but come on--to compare a horrible usage of Java's decade(?) old XML library to an implementation done based on being terse is just not fair--and is far from a comparison of the languages!

missingfaktor
hmm, the Java example would be shorter and better looking with a SAX or StAX parser. But still the SCALA one is really nice
oluies
The Java code is written exactly to parse that particular XML file with no attempt at reuse and a lot of duplicated code. Whoever wrote it was either trying to deliberately look like he didn't understand coding or didn't understand coding.
Bill K
@Bill K: I have never done XML parsing in Java so I picked this example from some random site. Feel free to edit the Java part of the answer, I don't mind.
missingfaktor
Well, let's assume that you are talking language differences and not library differences--in that case, the two would be nearly identical. The only language difference in the second example is the match/case thing which could be done in a single line as a for loop if implemented that way by the library.
Bill K
@Bill K: No, you are completely wrong. There are two very powerful Scala features at work here: 1. XML Literals 2. Pattern Matching. Java doesn't have either of these. So the equivalent Java code written in some hypothetical library will sure NOT be identical. (Try writing, you'll know.)
missingfaktor
@Bill K: By the way, match-case is like a switch-case put on steroids. It's used for decision making, not looping.
missingfaktor
@Rahul G Updated your answer so I could get the text formatting. I admit the pattern matching COULD be more powerful, but it wasn't being used in this case--even if it was I could match it with an intelligently formed library for the most part using nested match statements and regular expressions. For me the biggest burden in languages is syntax--the less unique operators are part of a language, the easier it is to learn and fully understand. Case in point--a lot of syntax was added to this Scala solution for no reduction in code size--why?
Bill K
@Bill K: That's a very broad question and I'd suggest you post it as a separate question if you are really interested in knowing the answer. There are people on this forum who can answer it better than I can.
missingfaktor
@Bill: your hypothetical code is broken. How do you imagine `scanFor` to work, while being reusable at the same time (i.e. not just working for this *particular* use-case)? Scala has the edge because it does **not** use any specialized features or methods here, just plain Scala. Granted, XPath (or CSS selectors) *can* make the above Java code *much* easier but nowhere near as elegant as your hypothetical code, and XPath is as good as it gets.
Konrad Rudolph
@Konrad Rudolph it takes a filter showing a section to look in, scans for elements in that section, creates either an iterator or an array of string arrays and returns it. After being returned, the iterator/array is iterated over by the for loop returning an array of strings with each array entry [0] or [1] corresponds to one of the search terms. Additional search terms would make a longer return array-variable args. Could be more generic by allowing additional levels of filtering, but this more than solves the existing problem in a form that is very reusable. What do you think won't work?
Bill K
To make it more generic, try syntax such as: source.filter() .inGroup("company", "My Company") .inGroup("employee") .getFields( "firstname", "lastname"); That could gather all employees from "My Company" and return their names. I'm not saying this is the same as the Scala solution, I know Scala does part of this in the language--but it solves the same problem--and that was my point... that this particular Java solution was poor.
Bill K
+3  A: 

The Circuit Breaker pattern from Michael Nygard's Release It in FaKods (link to code)

implementation looks like this in Scala:

. . .
addCircuitBreaker("test", CircuitBreakerConfiguration(100,10))
. . .


class Test extends UsingCircuitBreaker {
  def myMethodWorkingFine = {
    withCircuitBreaker("test") {
      . . .
    }
  }

  def myMethodDoingWrong = {
    withCircuitBreaker("test") {
      require(false,"FUBAR!!!")
    }
  }
}

Which I think is super nice. It looks just as a pice of the language but it is a simple mixin in the CircuitBreaker Object doing all work.

/**
 * Basic MixIn for using CircuitBreaker Scope method
 *
 * @author Christopher Schmidt
 */
trait UsingCircuitBreaker {
  def withCircuitBreaker[T](name: String)(f: => T): T = {
    CircuitBreaker(name).invoke(f)
  }
}

Reference in other languages Google for "Circuit breaker" + your language.

oluies
+3  A: 

I like much the method getOrElseUpdate, found in mutableMap and shown here, first Java, without:

public static Map <String, Integer> wordCount (Scanner sc, String delimiters) {
    Map <String, Integer> dict = new HashMap <String, Integer> ();
            while (sc.hasNextLine ()) {
                    String[] words = sc.nextLine ().split (delimiters);
                    for (String word: words) {
                        if (dict.containsKey (word)) {
                            int count = dict.get (word);
                            dict.put (word, count + 1);
                        } else
                            dict.put (word, 1);
                    }
            }       
    return dict;
}

yes - a WordCount, and here in scala:

def wordCount (sc: Scanner, delimiter: String) = {
        val dict = new scala.collection.mutable.HashMap [String, Int]()
        while (sc.hasNextLine ()) {
                val words = sc.nextLine.split (delimiter)
                words.foreach (word =>
                      dict.update (word, dict.getOrElseUpdate (word, 0) + 1))
        }
        dict
}

filter and sort have already been shown, but look how easy they are integrated with the map:

    def filterKeywords (sc: Scanner, keywords: List[String]) = {
            val dict = wordCount (sc, "[^A-Za-z]")
            dict.filter (e => keywords.contains (e._1)).toList . sort (_._2 < _._2)
    } 
user unknown
I like this example a lot. It avoids the easy route of comparing case classes, and doesn't make the mistake of showing Scala code and not the Java equivalent.
Daniel
+4  A: 

I liked user unknown's answer so much I'm going to try to improve upon it. The code below is not a direct translation of the Java example, but it accomplishes the same task with the same API.

def wordCount (sc: Scanner, delimiter: String) = {
  val it = new Iterator[String] {
    def next = sc.nextLine()
    def hasNext = sc.hasNextLine()
  }
  val words = it flatMap (_ split delimiter iterator)
  words.toTraversable groupBy identity mapValues (_.size)
}
Daniel
I don't have scala-2.8 installed by now, to test this snippet, but I guess I can see what is intendet - just 'keywords' aren't used at all. It produces a map of all Strings and their frequencies, doesn't it?
user unknown
@user Yes, that's what it does. Isn't that what's accomplished by your code? Oh, I see. I copied the wrong line. I'm gonna fix it right now. :-)
Daniel
+2  A: 

Task:

You have a list people of objects of class Person that has fields name and age. Your task is to sort this list first by name, and then by age.

Java:

Collections.sort(people, new Comparator<Person>() {
  public int compare(Person a, Person b) {
    return a.getName().compare(b.getName());
  }
});
Collections.sort(people, new Comparator<Person>() {
  public int compare(Person a, Person b) {
    return Integer.valueOf(a.getAge()).compare(b.getAge());
  }
});

Scala:

val sortedPeople = people.sortBy(p => (p.name, p.age))
missingfaktor