摘要:Scala
坯苹,Java
,JavaConverters
包括三種場(chǎng)景的轉(zhuǎn)化
- Java腳本中摇天,將Scala集合轉(zhuǎn)化為Java集合
- Scala腳本中粹湃,將Scala集合轉(zhuǎn)化為Java集合
- Scala腳本中,將Java集合轉(zhuǎn)化為Scala集合
JavaConverters隱式轉(zhuǎn)換
Scala提供了大量的方法來隱式轉(zhuǎn)換所有主要的Java和Scala容器類型泉坐。其中提供了如下的雙向類型轉(zhuǎn)換
Iterator
<=>java.util.Iterator
Iterator
<=>java.util.Enumeration
Iterable
<=>java.lang.Iterable
Iterable
<=>java.util.Collection
mutable.Buffer
<=>java.util.List
mutable.Set
<=>java.util.Set
mutable.Map
<=>java.util.Map
mutable.ConcurrentMap
<=>java.util.concurrent.ConcurrentMap
使用這些轉(zhuǎn)換很簡(jiǎn)單再芋,只需從JavaConverters對(duì)象中import它們即可。import之后坚冀,通過擴(kuò)展方法 asScala 和 asJava 就可以在Scala容器和與之對(duì)應(yīng)的Java容器之間進(jìn)行隱式轉(zhuǎn)換了
scala> import collection.JavaConverters._
import collection.JavaConverters._
還有一些Scala容器類型可以轉(zhuǎn)換成對(duì)應(yīng)的Java類型济赎,但是并沒有將相應(yīng)的Java類型轉(zhuǎn)換成Scala類型的能力,它們是:
Seq
=>java.util.List
mutable.Seq
=>java.util.List
Set
=>java.util.Set
Map
=>java.util.Map
因?yàn)镴ava并未區(qū)分可變?nèi)萜鞑豢勺內(nèi)萜黝愋图悄常运狙担m然能將scala.immutable.List轉(zhuǎn)換成java.util.List,但所有的修改操作都會(huì)拋出“UnsupportedOperationException”
scala> val jul = List(1, 2, 3).asJava
jul: java.util.List[Int] = [1, 2, 3]
scala> jul.add(7)
java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
在Java中調(diào)用使用Scala集合
Scala List => Java List
在Scala中定義方法返回List液南,在Java中調(diào)用得到返回值并轉(zhuǎn)化為Java的list壳猜。
object ReturnScalaCollection {
def returnList(): List[String] = {
return List("a", "b", "c")
}
def returnListType(): List[Int] = {
return List(1, 2, 3)
}
}
Scala list調(diào)用apply方法獲得下標(biāo)元素,循環(huán)一遍得到Java list滑凉,如果不是String對(duì)象统扳,Java中得到的返回值泛型是Object
,通過類型強(qiáng)轉(zhuǎn)
得到對(duì)應(yīng)類型
public static void main(String[] args) {
scala.collection.immutable.List<String> res = ReturnScalaCollection.returnList();
List<String> list = new ArrayList<>();
for (int i = 0; i < res.size(); i ++) {
list.add(res.apply(i));
}
System.out.println(list); // [a, b, c]
scala.collection.immutable.List<Object> res2 = ReturnScalaCollection.returnListType();
List<Integer> list2 = new ArrayList<>();
for (int i = 0; i < res2.size(); i ++) {
list2.add((int) res2.apply(i));
}
System.out.println(list2); // [1, 2, 3]
}
Scala Map => Java Map
Scala轉(zhuǎn)List<Tuple2<>>畅姊,使用下標(biāo)訪問填充到Java Map咒钟。
def returnMap(): Map[String, Int] = {
return Map("a" -> 1, "b" -> 2)
}
public static void main(String[] args) {
scala.collection.immutable.Map<String, Object> res3 = ReturnScalaCollection.returnMap();
// scala.collection.immutable.List<Tuple2<String, Object>> res3List = res3.toList();
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < res3.size(); i ++) {
map.put(res3.toList().apply(i)._1(), (int) res3.toList().apply(i)._2()); // {a=1, b=2}
}
System.out.println(map);
}
Scala Set => Java Set
Scala轉(zhuǎn)List,Java通過下標(biāo)apply獲取值填充到Set若未。
def returnSet(): Set[String] = {
return Set("a", "b", "a")
}
public static void main(String[] args) {
scala.collection.immutable.Set<String> res4 = ReturnScalaCollection.returnSet();
Set<String> set = new HashSet<>();
for (int i = 0; i < res4.size(); i ++) {
System.out.println(res4.toList().apply(i));
set.add(res4.toList().apply(i));
}
System.out.println(set); // [a, b]
}
Scala Tuple => Java
在Java中指定Tuple類型和對(duì)應(yīng)泛型朱嘴,如果飛String需要強(qiáng)轉(zhuǎn),使用_1()訪問,不帶()可能報(bào)null
def returnTuple(): (String, String) = {
val a = "a"
val b = "b"
return (a, b)
}
def returnTuple2(): (Int, Boolean) = {
val a = 1
val b = false
return (a, b)
}
public static void main(String[] args) {
scala.Tuple2<String, String> res = TupleTest.returnTuple();
String a = res._1(); // a
String b = res._2(); // b
scala.Tuple2<Object, Object> res2 = TupleTest.returnTuple2();
int aa = (int) res2._1(); // 1
boolean bb = (boolean) res2._2(); // false
}
在Scala中轉(zhuǎn)化Java集合(scala -> Java; Java -> Scala)
分為兩種情況萍嬉,將Scala的集合轉(zhuǎn)化為Java集合乌昔,使得可以調(diào)用Java方法;將Java集合轉(zhuǎn)化為Scala集合
Scala中壤追,將Scala的集合轉(zhuǎn)化為Java集合
在Java中創(chuàng)建傳參分別是Java List磕道,Map,Set的方法行冰,在Scala中創(chuàng)建集合調(diào)用溺蕉,使用JavaConverters
進(jìn)行隱式轉(zhuǎn)換,如果單向轉(zhuǎn) Scala => Java资柔,Scala集合可變不可變都可以焙贷,如果要雙向轉(zhuǎn)Scala集合必須是mutable
可變的撵割,否則Java轉(zhuǎn)Scala不報(bào)錯(cuò)但是不能再執(zhí)行修改增加操作
贿堰。
import java.util
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
object ScalaJavaTest {
def JavaList2ScalaList(): Unit = {
import scala.collection.JavaConverters._
// scala List => java.util.List
val a: java.util.List[String] = List("a", "b", "c").asJava
val b: java.util.List[String] = ListBuffer("a", "b", "c").asJava
val c: java.util.Collection[String] = List("a", "b", "c").asJava
val f: java.util.List[String] = mutable.Buffer[String]("a", "b").asJava
// java.util.List => scala.collection.mutable.Buffer
val aa: scala.collection.mutable.Buffer[String] = b.asScala
val bb: scala.collection.mutable.ListBuffer[String] = a.asScala.to[ListBuffer]
val cc: scala.collection.immutable.List[String] = a.asScala.toList
// java.util.ArrayList => scala
val e = new util.ArrayList[String](util.Arrays.asList("a", "b", "c"))
val ee: scala.collection.mutable.ListBuffer[String] = e.asScala.to[ListBuffer]
}
def JavaMap2ScalaMap(): Unit = {
import scala.collection.JavaConverters._
// scala Map => java.util.Map
val a: java.util.Map[String, Int] = Map("a" -> 1, "b" -> 2).asJava
val c: java.util.Map[String, Int] = mutable.HashMap[String, Int]("a" -> 1, "b" -> 2).asJava
println(a)
println(c)
// java.util.Map => java.util.HashMap
val b = new util.HashMap[String, Int](a)
b.put("c", 3)
// java.util.Map => scala.collection.mutable.Map
val aa: scala.collection.mutable.Map[String, Int] = a.asScala
println(aa.getOrElse("b", -1))
// 報(bào)錯(cuò)
// aa.put("c", 3)
// println(aa)
// 轉(zhuǎn)可變Map
val aaa = mutable.Map(aa.toSeq:_*)
aaa.put("c", 3)
println(aaa)
val bb: scala.collection.mutable.Map[String, Int] = b.asScala
bb.put("c", 3)
println(bb)
// java.util.HashMap => scala
val d = new util.HashMap[String, Int]()
d.put("a", 1)
d.put("b", 2)
val dd: scala.collection.mutable.Map[String, Int] = d.asScala
dd.put("c", 3)
println(dd)
}
def JavaSet2ScalaSet(): Unit = {
import scala.collection.JavaConverters._
// scala Set => java.util.Set
val a: java.util.Set[String] = Set("a", "b").asJava
println(a)
// java.util.Set => java.util.HashSet
val b = new util.HashSet[String](a)
b.add("c")
println(b)
// java.util.Set => scala.collection.mutable.Set
val aa: scala.collection.mutable.Set[String] = a.asScala
println(aa)
// 報(bào)錯(cuò)
// aa.add("c")
// println(aa)
// 轉(zhuǎn)可變Set
val aaa = mutable.Set(aa.toSeq:_*)
aaa.add("c")
val bb: scala.collection.mutable.Set[String] = b.asScala
bb.add("c")
}
}