最近在用Java写Spark程序遇到一个问题。
(用Java 写比较恶心~)
data.groupBy() 在Java里面不支持传入Java集合(), 只支持scala.collect.Seq
所以要把 Java 的List 转换成 Scala 的Seq, 官方是没有直接转换的API的。
以下为官方可直接转换列表(单向 =>, 双向<=>):
scala.collection.Iterable <=> .java.lang.Iterable
scala.collection.Iterable <=> java.util.Collection
scala.collection.Iterator <=> java.util.{ Iterator, Enumeration }
scala.collection.mutable.Buffer <=> java.util.List
scala.collection.mutable.Set <=> java.util.Set
scala.collection.mutable.Map <=> java.util.{ Map, Dictionary }
scala.collection.mutable.ConcurrentMap <=> java.util.concurrent.ConcurrentMap
scala.collection.Seq => java.util.List
scala.collection.mutable.Seq => java.util.List
scala.collection.Set => java.util.Set
scala.collection.Map => java.util.Map
java.util.Properties => scala.collection.mutable.Map[String, String]
Java List 转 Scala Seq 方法:
Java List => Java Set => Scala Set => Scala Seq
具体代码:
private Seq<String> javaListToScalaSeq(List<String> list){
Set<String> javaSet= new HashSet<>();
for(String info: list) {
javaSet.add(info);
}
scala.collection.Set<String> scalaSet = JavaConversions.asScalaSet(javaSet);
return scalaSet.toSeq();
}
转换列表转自:
http://blog.csdn.net/ainidong2005/article/details/53121193
网友评论