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

第13章:ScalaのMapリファレンス

More than 5 years have passed since last update.

今回は「Map」について、リファレンス風に紹介するよ!

入門編としてこちらも読んでね(^^)v
第8章:Scalaのコレクション(Seq, Set, Map)入門

scala.collection.immutable.Map

キーで値(要素)を特定することができる箱だ。
キーの重複はできないからね。

代表的なメソッドを使ってみたよ。

ルックアップ

get

キーに対応する値をOptionで返す

scala> Map(1 -> "a", 2 -> "b", 3 -> "c") get (1)
res0: Option[String] = Some(a)

(key)

キーに対応する値を返す

scala> Map(1 -> "a", 2 -> "b", 3 -> "c") (1)
res1: String = a

getOrElse

キーに対応する値を返す。なければデフォルト値を返す

scala> Map(1 -> "a", 2 -> "b", 3 -> "c") getOrElse (1, "d")
res2: String = a

scala> Map(1 -> "a", 2 -> "b", 3 -> "c") getOrElse (4, "d")
res3: String = d

isDefinedAt

キーを含んでいるか判定する

scala> Map(1 -> "a", 2 -> "b", 3 -> "c") isDefinedAt (1)
res4: Boolean = true

scala> Map(1 -> "a", 2 -> "b", 3 -> "c") isDefinedAt (4)
res5: Boolean = false

追加と更新

+

新しいキーバリューのペアを含む新しいMapを返す

scala> Map(1 -> "a", 2 -> "b", 3 -> "c") + (4 -> "d")
res6: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b, 3 -> c, 4 -> d)

++

全てのキーバリューのペアを含む新しいMapを返す

scala> Map(1 -> "a", 2 -> "b", 3 -> "c") ++ Map(4 -> "d", 5 -> "e")
res7: scala.collection.immutable.Map[Int,String] = Map(5 -> e, 1 -> a, 2 -> b, 3 -> c, 4 -> d)

updated

+と同じ

scala> Map(1 -> "a", 2 -> "b", 3 -> "c") updated (1, "d")
res8: scala.collection.immutable.Map[Int,String] = Map(1 -> d, 2 -> b, 3 -> c)

削除

-

キーを取り除いた新しいMapを返す

scala> Map(1 -> "a", 2 -> "b", 3 -> "c") - 1
res9: scala.collection.immutable.Map[Int,String] = Map(2 -> b, 3 -> c)

--

キーを取り除いた新しいMapを返す

scala> Map(1 -> "a", 2 -> "b", 3 -> "c") -- Traversable(1, 2)
res10: scala.collection.immutable.Map[Int,String] = Map(3 -> c)

サブコレクション作成

keys

キーのIterableを返す

scala> Map(1 -> "a", 2 -> "b", 3 -> "c") keys
res11: Iterable[Int] = Set(1, 2, 3)

values

値のIterableを返す

scala> Map(1 -> "a", 2 -> "b", 3 -> "c").values
res12: Iterable[String] = MapLike(a, b, c)

変形

filterKeys

関数を満たすものだけのMapを返す

scala> Map(1 -> "a", 2 -> "b", 3 -> "c") filterKeys (_ % 2 == 1)
res13: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 3 -> c)

mapValues

関数を適用したMapを返す

scala> Map(1 -> "a", 2 -> "b", 3 -> "c") mapValues (_ + "d")
res14: scala.collection.immutable.Map[Int,String] = Map(1 -> ad, 2 -> bd, 3 -> cd)

まとめ

今回は、 コレクションのMapトレイトのメソッドについて語ってみた。

ところで、ここ3回で SeqSetMap の代表的なメソッドについて紹介してみたけど
どうだった?

ここのところずっと言っているけど、使いこなすにはソースを書いて動かしてみるのが
一番速いと思うよ。

今回も
体で感じてくれたかな?

Why do not you register as a user and use Qiita more conveniently?
  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
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