title: Java <-> Scala interop: transparent List and Map conversion
body: <p>I am learning Scala and I have a Java project to migrate to Scala. I want to migrate it by rewriting classes one-by-one and checking that new class didn't break the project.</p>
  
  <p>This Java project uses lots of <code>java.util.List</code> and <code>java.util.Map</code>. In new Scala classes I would like to use Scala’s <code>List</code> and <code>Map</code> to have good-looking Scala code.</p>
  
  <p>The problem is that new classes (those are wtitten in Scala) do not integrate seamelessly with existing Java code: Java needs <code>java.util.List</code>, Scala needs its own <code>scala.List</code>.</p>
  
  <p>Here is a simplified example of the problem. There are classes <em>Main</em>, <em>Logic</em>, <em>Dao</em>. They call each other in a line: <em>Main -> Logic -> Dao</em>.</p>
  
  <pre><code>public class Main {
      public void a() {
          List&lt;Integer&gt; res = new Logic().calculate(Arrays.asList(1, 2, 3, 4, 5));
      }
  }
  
  public class Logic {
      public List&lt;Integer&gt; calculate(List&lt;Integer&gt; ints) {
          List&lt;Integer&gt; together = new Dao().getSomeInts();
          together.addAll(ints);
          return together;
      }
  }
  
  public class Dao {
      public List&lt;Integer&gt; getSomeInts() {
          return Arrays.asList(1, 2, 3);
      }
  }
  </code></pre>
  
  <p>In my situation, classes <em>Main</em> and <em>Dao</em> are framework classes (I don’t need to migrate them). Class <em>Logic</em> is business-logic and will benefit a lot from Scala cool features.</p>
  
  <p>I need to rewrite class <em>Logic</em> in Scala while preserving integrity with classes <em>Main</em> and <em>Dao</em>. The best rewrite would look like (doesn’t work):</p>
  
  <pre><code>class Logic2 {
    def calculate(ints: List[Integer]) : List[Integer] = {
        val together: List[Integer] = new Dao().getSomeInts()
        together ++ ints
    }
  }
  </code></pre>
  
  <p>Ideal behaviour: Lists inside <em>Logic2</em> are native Scala Lists. All in/out <code>java.util.Lists</code> get boxed/unboxed automagically. But this doesn't work.</p>
  
  <p>Instead, this does work (thanks to <a href="http://github.com/jorgeortiz85/scala-javautils" rel="noreferrer">scala-javautils</a> (<a href="http://en.wikipedia.org/wiki/GitHub" rel="noreferrer">GitHub</a>)):</p>
  
  <pre><code>import org.scala_tools.javautils.Implicits._
  
  class Logic3 {
    def calculate(ints: java.util.List[Integer]) : java.util.List[Integer] = {
        val together: List[Integer] = new Dao().getSomeInts().toScala
        (together ++ ints.toScala).toJava
    }
  }
  </code></pre>
  
  <p>But it looks ugly.</p>
  
  <p>How do I achieve transparent magic conversion of Lists and Maps between Java &lt;-> Scala (without need to do toScala/toJava)?</p>
  
  <p>If it is not possible, what are the best practices for migrating Java -> Scala code that uses <code>java.util.List</code> and friends?</p>