CollectionConverters

This object provides extension methods that convert between Scala and Java collections.

When writing Java code, use the explicit conversion methods defined in javaapi.CollectionConverters instead.

Note: to create Java Streams that operate on Scala collections (sequentially or in parallel), use StreamConverters.

import scala.jdk.CollectionConverters._
val s: java.util.Set[String] = Set("one", "two").asJava

The 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.asJava
js: java.util.Set[String] = [one]

scala> js.add("two")

scala> s
res2: scala.collection.mutable.Set[String] = HashSet(two, one)

The following conversions are supported via asScala and asJava:

scala.collection.Iterable       <=> java.lang.Iterable
scala.collection.Iterator       <=> java.util.Iterator
scala.collection.mutable.Buffer <=> java.util.List
scala.collection.mutable.Set    <=> java.util.Set
scala.collection.mutable.Map    <=> java.util.Map
scala.collection.concurrent.Map <=> java.util.concurrent.ConcurrentMap

The following conversions are supported via asScala 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 via 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

The following one way conversion is provided via asScala:

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

In 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.asJava
val other: scala.collection.mutable.Buffer[Int] = target.asScala
assert(source eq other)
Source:
CollectionConverters.scala

Type members

Inherited classlikes

implicit class BufferHasAsJava[A](b: Buffer[A])
implicit class ConcurrentMapHasAsJava[K, V](m: Map[K, V])
implicit class DictionaryHasAsScala[K, V](d: Dictionary[K, V])
implicit class IterableHasAsJava[A](i: Iterable[A])
implicit class IterableHasAsScala[A](i: Iterable[A])
implicit class IteratorHasAsJava[A](i: Iterator[A])
implicit class IteratorHasAsScala[A](i: Iterator[A])
implicit class ListHasAsScala[A](l: List[A])
implicit class MapHasAsJava[K, V](m: Map[K, V])
implicit class MapHasAsScala[K, V](m: Map[K, V])
implicit class MutableMapHasAsJava[K, V](m: Map[K, V])
implicit class MutableSeqHasAsJava[A](s: Seq[A])
implicit class MutableSetHasAsJava[A](s: Set[A])
implicit class SeqHasAsJava[A](s: Seq[A])
implicit class SetHasAsJava[A](s: Set[A])
implicit class SetHasAsScala[A](s: Set[A])