This object provides extension methods that convert between Scala and Java collections.
When writing Java code, use the explicit conversion methods defined injavaapi.CollectionConverters instead.
Note: to createJava Streams that operate on Scala collections (sequentially or in parallel), useStreamConverters.
import scala.jdk.CollectionConverters._val s: java.util.Set[String] = Set("one", "two").asJavaThe conversions return adapters for the corresponding API, i.e., the collections are wrapped, not converted. Changes to the original collection are reflected in the view, and vice versa:
scala> import scala.jdk.CollectionConverters._scala> val s = collection.mutable.Set("one")s: scala.collection.mutable.Set[String] = HashSet(one)scala> val js = s.asJavajs: java.util.Set[String] = [one]scala> js.add("two")scala> sres2: scala.collection.mutable.Set[String] = HashSet(two, one)The following conversions are supported viaasScala andasJava:
scala.collection.Iterable <=> java.lang.Iterablescala.collection.Iterator <=> java.util.Iteratorscala.collection.mutable.Buffer <=> java.util.Listscala.collection.mutable.Set <=> java.util.Setscala.collection.mutable.Map <=> java.util.Mapscala.collection.concurrent.Map <=> java.util.concurrent.ConcurrentMapThe following conversions are supported viaasScala and through specially-named extension methods to convert to Java collections, as shown:
scala.collection.Iterable <=> java.util.Collection (via asJavaCollection)scala.collection.Iterator <=> java.util.Enumeration (via asJavaEnumeration)scala.collection.mutable.Map <=> java.util.Dictionary (via asJavaDictionary)In addition, the following one-way conversions are provided viaasJava:
scala.collection.Seq => java.util.Listscala.collection.mutable.Seq => java.util.Listscala.collection.Set => java.util.Setscala.collection.Map => java.util.MapThe following one way conversion is provided viaasScala:
java.util.Properties => scala.collection.mutable.MapIn all cases, converting from a source type to a target type and back again will return the original source object. For example:
import scala.jdk.CollectionConverters._val source = new scala.collection.mutable.ListBuffer[Int]val target: java.util.List[Int] = source.asJavaval other: scala.collection.mutable.Buffer[Int] = target.asScalaassert(source eq other)