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 CollectionHasAsScala[A](c: Collection[A])
implicit class ConcurrentMapHasAsJava[K, V](m: Map[K, V])
implicit class ConcurrentMapHasAsScala[K, V](m: ConcurrentMap[K, V])
implicit class DictionaryHasAsScala[K, V](d: Dictionary[K, V])
implicit class EnumerationHasAsScala[A](e: Enumeration[A])
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])

Implicits

Inherited implicits

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