Scala, how to convert a collection of Java object instance of the object instance Scala (interchangeable)


Currently the development of many languages ​​in development, you may encounter Java, Scala hybrid system, but java, scala inconsistencies in the collection of objects, will cause a lot of problems encountered in the development, may need two languages ​​interchangeable collection. 2.8.1 Scala from Scala been introduced with the Java scala.collection.JavaConverters system conversion for the set, if necessary in the scala set conversion code, first introduced scala.collection.JavaConverters._, further display call asJava complete the transformation method or asScala . On the other identical scala.collection.JavaConversions it has been marked as @Deprecated (since 2.12.0), JavaConversions can do an implicit conversion, that does not require asJava or call asScala, but this might be an obstacle to reading, may What makes it difficult to know what has become of. The following is the Scala source code comments.

scala, java two-way conversion

The following can be arbitrarily set, conversion. However, implicit conversions introduced in some cases incorrect or introduced, still can not be converted correctly, as an example of the 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.concurrent.Map <=> java.util.concurrent.ConcurrentMap

The collection element may specify Scala collation: sort method inherited Comparable
scala.collection.Iterable <=> the java.lang Iterable {,} collection.
Scala.collection.Iterator <=> Classes in java.util the Iterator {,} the Enumeration.

Buffer is corresponding to the scala java.util.List, but not directly, using ListBuffer
scala.collection.mutable.Buffer <=> java.util.List
scala.collection.mutable.Set <=> java.util.Set
scala. collection.mutable.Map <=> java.util. {Map, Dictionary }

Concurrent safe collection
scala.collection.concurrent.Map <=> java.util.concurrent.ConcurrentMap

In all cases, then change after the transition from the original type to the opposite type of words will come back to the same object

scala.collection.mutable.ListBuffer <=> java.util.List in addition require the introduction of a respective transformation class JavaConversions._, there is introduced ListBuffer => java.util.List particular conversion method

package com.fuyun.bigdata.scala.collection

import scala.collection.mutable

/**
  * Scala中如何将Java集合实例对象转换为Scala实例对象(互相转换)
  */

object ConvertorScalaDemo {

  def main(args: Array[String]): Unit = {

    // 定义Java中的Map实力对象
    import java.util
    val hashMap = new util.HashMap[String, Int]()
    hashMap.put("spark", 555)
    hashMap.put("hive", 666)
    hashMap.put("scala", 123)
    hashMap.put("linux", 345)
    hashMap.put("hbase", 234)

    // TODO:将Java中HashMap集合转换为Scala中Map集合实例对象
    import scala.collection.JavaConverters._
    val map: mutable.Map[String, Int] = hashMap.asScala // 隐式转换
    map.foreach(println(_))

    println("======================================")

    // TODO: 将Scala中列表转换为Java中列表
    val list: List[String] = List("hive", "spark", "kafka", "hbase", "java")
    val lst: util.List[String] = list.asJava
    println(lst)

    println("======================================")
    
    /**
      * 在所有情形下,从原始类型转变到对侧类型之后再转变回来的话会是同一个对象
      * Scala中的ListBuffer与Java中List相互转换时,
      * 不仅要导入scala.collection.JavaConverters._,
      * 还有导入以下两个包
      */
    import scala.collection.JavaConversions.bufferAsJavaList
    import scala.collection.JavaConversions.asScalaBuffer

    val slstl = new scala.collection.mutable.ListBuffer[Int]
    val jlstl: java.util.List[Int] = slstl
    val slst2: scala.collection.mutable.Buffer[Int] = jlstl
    println("两者集合是否相等:" + (slstl eq slst2))

  }
}

结果为:
(hive,666)
(spark,555)
(scala,123)
(linux,345)
(hbase,234)
======================================
[hive, spark, kafka, hbase, java]
======================================
两者集合是否相等:true

Process finished with exit code 0

Scala performed by one-way conversion to Java asJava

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-way conversion performed by the asScala to Scala

java.util.Properties  =>  scala.collection.mutable.Map

Specific descriptive display name calling

Scala turn Java

If the conversion java and scala collection object collection object is carried out, the introduction of import scala.collection.JavaConversions._ still does not convert,
then try to introduce specific implicit conversion method set type you want to convert correspondence, as follows:

// 隐式转换一个scala iterable为一个不可变的java.collection
implicit def  asJavaCollection[A](it:iterable[A]):collection[A]

// 隐式转换一个scala iterable 为一个java Dictionary
implicit def  asJavaDictionary[A,B](m:mutable.Map[A,B]):Dictionary[A,B]

// 隐式转换一个scala iterable为一个java Enumeration
implicit def  asJavaEnumeration[A](it:Iterator[A]):java.util.Enumeration

// 隐式转换一个scala iterable为一个java Iterable
implicit def  asJavaIterable[A](i:iterable[A]):java.lang.Iterable[A]

// 隐式转换一个scala iterator 为一个java iterator 
implicit def  asJavaIterator[A](it:Iterator[A]):java.lang.Iterator

 // 隐式一个scalaBuffer(用其实现Buffer接口,AbstractBuffer抽象类的子类作为接受对象)为java.util.list
implicit def  bufferAsJavaList[A](s:java.util.Set[A]):java.util.list

// 隐式转换一个Scala.mutable.concurrent.Map转换为java.concurrent.ConcurrentMap 
implicit def  mapAsJavaConcurrentMap[A,B)(m:concurrent.Map[A,B]):ConcurrentMap[A,B]

// 隐式转换一个scala.Map转换为java map对象
implicit def  mapAsJavaMap[A,B](m:Map[A,B]):java.util.Map[A,B]

// 隐式转换一个 scala.mutable.Map转换为一个 java.util.Map对象
implicit def  mutableMapAsJavaMap[A,B](m:mutable.Map[A,B]):java.util.Map[A,B]

// 隐式转换一个scala.mutable.Seq转换为一个java.util.List对象 
implicit def  mutableSeqAsJavaList[A,B](m:mutable.Seq[A,B]):java.util.List[A,B]

// 隐式将一个 scala.mutable.Seq转换为一个java.util.Set对象
implicit def  mutableSetAsJavaSet[A,B](m:mutable.Set[A,B]):java.util.Set[A,B]

Java turn Scala

// 隐式转换一个java.util.list对象为scalaBuffer(用其实现Buffer接口,AbstractBuffer抽象类的子类作为接受对象)
implicit def  asScalaBuffer[A](l:java.util.List[A]):Buffer[A]

// 隐式转换一个java.util.iterator对象为scala.iterator
implicit def  asScalaIterator[A](it:java.util.Iterator[A]):Iterator[A]

// 隐式转换一个java.util.Set 为mutable.Set
implicit def  asScalaSet[A](s:java.util.Set[A]):mutable.Set 

// 隐式转换一个java.collection对象为scala.iterable
implicit def  collectionAsScalaIterable[A](i:Collection[A]):Iterable[A]

// 隐式转换一个java.Dictionary 对象转换为Scala.mutable.Map[String,String]
implicit def  dictionaryAsScalaMap[A,B](p:Dictionary[A,B]):mutable.Map[A,B]

// 隐式将一个java.Enumeration 对象转换为Scala.iterator对象
implicit def  enumerationAsScalaIterator[A](i:java.lang.Enumeration[A]):Iterator[A]

// 隐式将一个java.Iterable对象转换为Scala.iterable对象
implicit def  iterableAsScalaIterable[A](i:java.lang.Iterable[A]):Iterable[A]

// 隐式将一个java concurrentMap 转换为一个scala.concurrentMap对象
implicit def  mapAsScalaConcurrentMap[A,B](m:ConcurrentMap[A,B]):concurrent.Map[A,B]

// 隐式将一个java.util.Map 转换为一个scala.Map 对象 
implicit def  mapAsScalaMap[A,B](m:java.util.Map[A,B]):mutable.Map[A,B]

// 隐式将一个 java Properties 转换为一个 mutable.Map对象 
implicit def  propertiesAsScalaMap(p:Properties):mutable.Map[String,String]

// 隐式将一个scala.Seq  转换为一个 java.util.List 对象
implicit def  seqAsJavaList[A](seq:Seq[A]):java.util.List[A]

// 隐式将一个scala.Set  转换为一个 java.util.Set 对象  
implicit def  setAsJavaSet[A](set:Set[A]):java.util.Set[A]

Guess you like

Origin blog.csdn.net/lz6363/article/details/88783272