Help us understand the problem. What is going on with this article?

Scala Starter 101 - Convert between Mutable and Immutable

More than 5 years have passed since last update.

Generally, from mutable to immutable, you use the to* series methods in mutable collections, like MutableList and ListBuffer's toList method.

In the other hand, from immutable to mutable, you can just use constructors like this: scala.collection.mutable.ListBuffer(immtableList: _*).

Note that the to* methods like toList, toMap are is performed in constant time.

Here is some examples.

Map

// from mutable to immutable
val mutableMap1 = immutableMap.toMap() // after 2.8
val mutbaleMap2 = collection.immutable.Map(x.toList: _*) // before 2.8
// from immutable to mutable
val immutableMap = scala.collection.immutable.Map(1 -> "1", 2 -> "2")
val mutableMap = scala.collection.mutable.Map(immutableMap: _*)

List

// from mutable to immutable
val immutableList = mutableListBuffer.toList
// from immutable to mutable
val immutableList = scala.collection.immutable.List(1, 2, 3)
val mutableListBuffer = scala.collection.mutable.ListBuffer(immutableList: _*)

Ref

[1] - How can I convert immutable.Map to mutable.Map in Scala?
[2] - Converting mutable to immutable map
[3] - Converting mutable collection to immutable

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした