scala集合和Java集合对应转换操作

用Scala编码的时候,经常会遇到scala集合和Java集合互相转换的case,

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的list

import scala.collection.JavaConversions

val scalaList = JavaConversions.asScalaBuffer(javaList).toList

把scala的Map转换成java.util.Map

import scala.collection.JavaConverters._
val act_max:Map[String,String] = Map("time"->"100","favorite"->"1")
val actionTime_maxs:java.util.Map[String, String] = act_max.asJava

 

posted @ 2019-10-08 17:50  糖醋排骨加辣椒  阅读(1837)  评论(0编辑  收藏  举报