-
-
Save debasishg/711e386a819f45c879fe71d49fed43f2 to your computer and use it in GitHub Desktop.
Using Scala collections from Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List(one, two, three) | |
3 | |
3 | |
5 | |
Vector(four, five, six) | |
4 | |
4 | |
3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment