一、map

1、创建map

//创建一个不可变的Map
scala> val ages = Map("Leo" -> 30, "Jen" -> 25, "Jack" -> 23)
ages: scala.collection.immutable.Map[String,Int] = Map(Leo -> 30, Jen -> 25, Jack -> 23) //创建一个可变的Map
scala> val ages = scala.collection.mutable.Map("Leo" -> 30, "Jen" -> 25, "Jack" -> 23)
ages: scala.collection.mutable.Map[String,Int] = Map(Jen -> 25, Jack -> 23, Leo -> 30) scala> ages("Leo") = 31 scala> ages
res5: scala.collection.mutable.Map[String,Int] = Map(Jen -> 25, Jack -> 23, Leo -> 31) //使用另外一种方式定义Map元素
scala> val ages = Map(("Leo",30),("Jen",25),("Jack",23))
ages: scala.collection.immutable.Map[String,Int] = Map(Leo -> 30, Jen -> 25, Jack -> 23) //创建一个空的HashMap
scala> val ages = new scala.collection.mutable.HashMap[String, Int]
ages: scala.collection.mutable.HashMap[String,Int] = Map()

2、访问Map的元素

##获取指定key对应的value,如果key不存在,会报错
scala> val ages = Map(("Leo",30),("Jen",25),("Jack",23))
ages: scala.collection.immutable.Map[String,Int] = Map(Leo -> 30, Jen -> 25, Jack -> 23) scala> val age = ages("Leo")
age: Int = 30 ##使用contains判断key是否存在
scala> val age = if (ages.contains("Leo")) ages("Leo") else 0
age: Int = 30 ##getOrElse函数判断key是否存在
scala> val age = ages.getOrElse("Leo",0)
age: Int = 30

3、修改Map的元素

##更新Map的元素
scala> val ages = scala.collection.mutable.Map("Leo" -> 30, "Jen" -> 25, "Jack" -> 23)
ages: scala.collection.mutable.Map[String,Int] = Map(Jen -> 25, Jack -> 23, Leo -> 30) scala> ages("Leo") = 31 scala> ages("Leo")
res3: Int = 31 ##增加多个元素
scala> ages += ("Mike" -> 35, "Tom" -> 40)
res4: ages.type = Map(Jen -> 25, Mike -> 35, Tom -> 40, Jack -> 23, Leo -> 31) scala> ages
res5: scala.collection.mutable.Map[String,Int] = Map(Jen -> 25, Mike -> 35, Tom -> 40, Jack -> 23, Leo -> 31) ##移除元素
scala> ages -= "Mike"
res6: ages.type = Map(Jen -> 25, Tom -> 40, Jack -> 23, Leo -> 31) scala> ages
res7: scala.collection.mutable.Map[String,Int] = Map(Jen -> 25, Tom -> 40, Jack -> 23, Leo -> 31) ##更新不可变的map
scala> val ages2 = ages + ("Mike" -> 36, "Tom" -> 40)
ages2: scala.collection.immutable.Map[String,Int] = Map(Mike -> 36, Tom -> 40, Leo -> 30, Jack -> 23, Jen -> 25) scala> ages2
res0: scala.collection.immutable.Map[String,Int] = Map(Mike -> 36, Tom -> 40, Leo -> 30, Jack -> 23, Jen -> 25) ##移除不可变map的元素
scala> val ages3 = ages - "Tom"
ages3: scala.collection.immutable.Map[String,Int] = Map(Leo -> 30, Jen -> 25, Jack -> 23) scala> ages3
res1: scala.collection.immutable.Map[String,Int] = Map(Leo -> 30, Jen -> 25, Jack -> 23)

4、遍历map

##遍历map的entrySet
scala> val ages = Map("Leo" -> 30, "Jen" -> 25, "Jack" -> 23)
ages: scala.collection.immutable.Map[String,Int] = Map(Leo -> 30, Jen -> 25, Jack -> 23) scala> for ((key, value) <- ages) println(key + ":" + value)
Leo:30
Jen:25
Jack:23 ##遍历map的key
scala> for (key <- ages.keySet) println(key)
Leo
Jen
Jack ##遍历map的value
scala> for (value <- ages.values) println(value)
30
25
23 ##生成新map,反转key和value
scala> for ((key, value) <- ages) yield (value, key)
res8: scala.collection.immutable.Map[Int,String] = Map(30 -> Leo, 25 -> Jen, 23 -> Jack)

5、SortedMap和LinkedHashMap

##SortedMap可以自动对Map的key的排序
scala> val ages = scala.collection.immutable.SortedMap("Leo" -> 30,"Alice" -> 15, "Jen" -> 25)
ages: scala.collection.immutable.SortedMap[String,Int] = Map(Alice -> 15, Jen -> 25, Leo -> 30) ##LinkedHashMap可以记住插入entry的顺序
scala> val ages = new scala.collection.mutable.LinkedHashMap[String, Int]
ages: scala.collection.mutable.LinkedHashMap[String,Int] = Map() scala> ages("Leo") = 30 scala> ages("Alice") = 25 scala> ages("Jen") = 26 scala> ages
res12: scala.collection.mutable.LinkedHashMap[String,Int] = Map(Leo -> 30, Alice -> 25, Jen -> 26)

二、Tuple

map的元素类型Tuple

1、定义、访问Tuple

##定义
scala> val t = ("leo", 30, "Jen")
t: (String, Int, String) = (leo,30,Jen) ##访问
scala> t._1
res15: String = leo scala> t._2
res16: Int = 30 scala> t._3
res17: String = Jen ##zip操作
scala> val names = Array("leo","jack","mike")
names: Array[String] = Array(leo, jack, mike) scala> val ages = Array(30,25,27)
ages: Array[Int] = Array(30, 25, 27) scala> val nameages = names.zip(ages)
nameages: Array[(String, Int)] = Array((leo,30), (jack,25), (mike,27)) scala> for ((name, age) <- nameages) println(name + ": " + age)
leo: 30
jack: 25
mike: 27

4、map和Tuple的更多相关文章

  1. Scala实战高手****第8课:零基础实战Scala最常用数据结构Map和Tuple及Spark源码鉴赏

    本课内容1.Map和Tuple在Spark源码中的鉴赏2.Map和Tuple代码操作实战 ------------------------------------------------------- ...

  2. Map与Tuple

    一:Map 1.创建一个不可变的map 2.创建一个可变的map 3.创建一个空的map 4.可变map添加一个元素 5.查看一个元素和修改一个元素 6.可变map删除元素 7.不可变map添加一个元 ...

  3. Scala编程入门---Map与Tuple

    创建Map //创建一个不可变的Map val ages = Map("Leo" -> 30,"Jen" ->25,"Jack" ...

  4. scala中Map和Tuple

    /** * Created by root * Description : Tuple and Map */ object MapTest { def main(args: Array[String] ...

  5. [Scala]Scala学习笔记三 Map与Tuple

    1. 构造映射 可以使用如下命令构造一个映射: scala> val scores = Map("Alice" -> 90, "Kim" -> ...

  6. 6、scala Map和Tuple

    1.  创建Map 2.访问Map元素 3.修改Map元素的值 4.遍历Map 5.SortedMap和LinkedHashMap 6.Map的元素类型Tuple 1.  创建Map 创建不可变的Ma ...

  7. Scala Map与Tuple

    创建Map // 创建一个不可变的Map val ages = Map("Leo" -> 30, "Jen" -> 25, "Jack&q ...

  8. 3. Map与Tuple

    一. Map 对偶 (1)对偶是一个映射.多个对偶形成map (2)对偶的表示:(k,v)或者k->v Map(哈希类型) (1)map的声明与查询 scala> val map1 = M ...

  9. Scala数据结构(数组,Map和Tuple)

    package com.zy import scala.collection.mutable import scala.collection.mutable.ArrayBuffer object te ...

随机推荐

  1. java设计模式之迭代器模式

    一.迭代器模式简介 迭代器模式提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示. 迭代器模式让我们能游走于聚合内的每一个元素,而又不暴露内部的表示.把游走的任务放在迭代器上,而不是 ...

  2. 九度OJ 1027:欧拉回路 (欧拉回路)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2989 解决:1501 题目描述:     欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条回路.现给定一个图,问是 ...

  3. Consumer Group Example

    面向kafka编程 Consumer Group Example https://cwiki.apache.org/confluence/display/KAFKA/Consumer+Group+Ex ...

  4. 常见寄存器以及常见汇编指令,常见爆破指令 good

    CPU的任务就是执行存放在存储器里的指令序列.为此,除要完成算术逻辑操作外,还需要担负CPU和存储器以及I/O之间的数据传送任务.早期的CPU芯片只包括运算器和控制器两大部分.到了近几年,为了使存储器 ...

  5. 一个比较好用的Socket测试工具——Hercules SETUP

    官网:http://www.hw-group.com/products/hercules/index_en.html 不要再自己傻傻的写socket测试客户端了 Hercules is great u ...

  6. Error: Failed to fetch plugin E:_My_File______workMyCodemyCodecordova-workspaceplugman-testMyMath via registry. Probably this is either a connection problem, or plugin spec is incorrect.

    $ cordova plugin add E:\_My_File_____\_work\MyCode\myCode\cordova-workspace\plugman-test\MyMath --sa ...

  7. VM tools安装错误The path "" is not a valid path to the xx generic kernel headers.

    VMWARE TOOLS安装提示THE PATH IS NOT A VALID PATH TO THE GENERIC KERNEL HEADERSI solved this problem, I g ...

  8. Database: key

    super-key: Any key that has more columns than necessary to uniquely identify each row in the table i ...

  9. 项目发布之后 总提示有一个.DLL找不到或不匹配

    最近发布项目(.net,winform)总提示有一个.dll文件找不到或者不匹配 但是在本地调试是正常的 这个.dll,原来是从.net组件中引用到项目的,后来我将此.dll文件从网上下载,然后在项目 ...

  10. android的GPS代码分析JNI如何HAL之间如何设置回调函数【转】

    本文转载自:http://blog.csdn.net/kmesg/article/details/6531577 本文只关注JNI和HAL的接口部分 在jni的android_location_Gps ...