Recent Changes - Search:

Oktatás

* Programozás 2
  + feladatsor
  + C feladatsor
  + Python feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

* levelezősök
  + Adator. prog.
  + feladatsor
  + quick link

teaching assets


Félévek

* 2024/25/1
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

Java → Scala / Scala → Java

#############################

Say you want to use the ProcessBuilder class from Java and its command function. From the Java doc:

ProcessBuilder(List<String> command)
    This constructs a process builder with the specified operating system program and arguments.

    List<String> command()
        This method returns this process builder's operating system program and arguments.

As can be seen, the constructor needs a List of Strings. However, in Scala we'd like to use an ArrayBuffer[String] and thus we have all the extra methods. Similarly, the command() returns a List of Strings, but it'd be awesome to get its result as an ArrayBuffer[String]. No problem, Scala can do implicit conversion for us.

import scala.collection.JavaConversions.bufferAsJavaList    // buffer TO Java List
import scala.collection.mutable.ArrayBuffer
val cmd = ArrayBuffer("ls", "-al", "/tmp")                  // create your buffer
val pb = new ProcessBuilder(cmd)                            // automatically converted to Java List

Let's see the other direction, i.e. converting a Java List to a Buffer:

import scala.collection.JavaConversions.asScalaBuffer       // (Java List) TO Scala Buffer
import scala.collection.mutable.Buffer
val b: Buffer[String] = pb.command()                        // Can't use ArrayBuffer!
                                                            // the wrapped object is only guaranteed to be a Buffer

// b: scala.collection.mutable.Buffer[String] = ArrayBuffer(ls, -al, /tmp)
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2015 March 15, 11:44