Skip to content

Instantly share code, notes, and snippets.

@coacoas
Created June 28, 2012 20:22
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save coacoas/3013680 to your computer and use it in GitHub Desktop.
Using Scala collections from Java
package org.example;
import scala.Function1;
import scala.collection.generic.CanBuildFrom;
import scala.collection.immutable.List;
import scala.collection.immutable.List$;
import scala.collection.immutable.Vector;
import scala.collection.immutable.Vector$;
import scala.collection.mutable.Builder;
import scala.runtime.AbstractFunction1;
public class ScalaCollectionTest {
// With proper setup, it doesn't seem too bad.
public static void main(String[] args) {
List<String> strings = list("one", "two", "three");
System.out.println(strings.toString());
strings.map(length, ListBuilder.<String, Integer>newCBF()).foreach(println);
Vector<String> stringv = vector("four", "five", "six");
System.out.println(stringv.toString());
Vector<Integer> outv = stringv.map(length, VectorBuilder.<String, Integer>newCBF());
outv.foreach(println);
}
/******************************************************/
/*** Pay no attention to the man behind the curtain ***/
/******************************************************/
public final static Function1<String, Integer> length = new AbstractFunction1<String, Integer>() {
@Override
public Integer apply(String arg0) {
return arg0.length();
}
};
public final static Function1<Integer, Void> println = new AbstractFunction1<Integer, Void>() {
@Override
public Void apply(Integer arg0) {
System.out.println(arg0);
return null;
}
};
final static class VectorBuilder {
public static <Source, Target> CanBuildFrom<Vector<Source>, Target, Vector<Target>> newCBF() {
return new CanBuildFrom<Vector<Source>, Target, Vector<Target>>() {
Vector<Target> v = Vector$.MODULE$.<Target>empty();
@Override
public Builder<Target, Vector<Target>> apply() {
return v.newBuilder();
}
@Override
public Builder<Target, Vector<Target>> apply(Vector<Source> arg0) {
return v.newBuilder();
}
};
}
}
final static class ListBuilder {
public static <Source, Target> CanBuildFrom<List<Source>, Target, List<Target>> newCBF() {
return new CanBuildFrom<List<Source>, Target, List<Target>>() {
List<Target> v = List$.MODULE$.<Target>empty();
@Override
public Builder<Target, List<Target>> apply() {
return v.newBuilder();
}
@Override
public Builder<Target, List<Target>> apply(List<Source> arg0) {
return v.newBuilder();
}
};
}
}
public final static <A> List<A> list(A... as) {
List<A> l = List$.MODULE$.empty();
for (int i = as.length - 1; i >= 0; i--) {
l = l.$colon$colon(as[i]);
}
return l;
}
public final static <A> Vector<A> vector(A... as) {
Vector<A> v = Vector$.MODULE$.empty();
for (A a : as) {
v = v.appendBack(a);
}
return v;
}
}
List(one, two, three)
3
3
5
Vector(four, five, six)
4
4
3
@glorysdj
Copy link

glorysdj commented Jun 4, 2018

Any example of mutable.Map?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment