当前位置:网站首页>Scala immutable map, variable map, map conversion to other data types
Scala immutable map, variable map, map conversion to other data types
2022-07-27 04:33:00 【But don't ask about the future】
List of articles
Preface
scala in Map It's an iterative Key value pair (key/value) structure . All values can be obtained through the key .Map The keys in are unique
1. immutable Map
object T14 {
def main(args: Array[String]): Unit = {
//scala in map key Can't repeat disorder
//scala Middle key value pair , Can be used directly K->V The way
//key Phase at the same time , It will cover
val map = Map("a" -> 1, "b" -> 2, "c" -> 3, "a" -> 5)
println(map)
// Add data
val map1 = map + ("d" -> 7)
println(map1)
// Delete data
val map2 = map - "a"
println(map2)
// Modifying data
val map3 = map.updated("c", 9)
println(map3)
// Merge map
val map4 = map ++ Map(1 -> "f", 3 -> "h")
println(map4)
// Traversal data
map.foreach(println)
}
}

2. variable Map
Need to introduce import scala.collection.mutable
import scala.collection.mutable
object T15 {
def main(args: Array[String]): Unit = {
val map = mutable.Map("a" -> 1, "b" -> 2, "c" -> 3)
println(map)
//key non-existent , The insert ; There is , Update
map.put("a", 3)
println(map)
// variable Map + Add data , Generate new map
val map1 = map + ("e" -> 5)
println(map1 eq map)
// variable Map += Add data , Return to the original map Oneself
val map2 = map += ("e" -> 5)
println(map2 eq map)
// Modify the data 2 Ways of planting
map.update("c", 200)
println(map)
map("c") = 300
println(map)
// Delete data
map.remove("c")
// map - ("c")
// map -= ("c")
println(map)
// Empty data
map.clear()
println(map)
}
}

import scala.collection.mutable
object T16 {
def main(args: Array[String]): Unit = {
// java if key There is , Then get value, If it does not exist , Then return to null,hashmap Can store null keys and null values
//scala Can store null values , Empty keys cannot be stored
val map = mutable.Map("a" -> 1, "b" -> 2, "c" -> 3, "d" -> null)
//map according to key Query data , The query results will be packaged
// The type of the returned result is Option[V] type , Yes 2 Objects ,Some( Valuable )、None( No value ), To avoid null pointer exceptions
// about key Occasion of existence , So the return result is Some
val value1 = map.get("a")
println(value1)
val value2 = map.get("d")
println(value2)
// about key Occasions that do not exist , So the return result is None
val value3 = map.get("e")
println(value3)
// obtain key Corresponding specific value
println(map.get("a").get)
println(map.get("d").get)
// from None In the object get value , Exceptions will occur
// println( map.get("e").get)
// First judge whether it is empty or set the default value when it is empty
println(map.get("e").isEmpty)
println(map.get("e").getOrElse(-1))
// More brief way
map.getOrElse("e",-1)
}
}

import scala.collection.mutable
object T17 {
def main(args: Array[String]): Unit = {
val map = mutable.Map("a" -> 1, "b" -> 2, "c" -> 3, "d" -> null)
//mao key iterator
// val keys = map.keys
// val iterator1 = keys.iterator
val iterator1 = map.keysIterator
while (iterator1.hasNext) {
println(map.get(iterator1.next()).get)
}
//mao value iterator
// val values = map.values
// val iterator2 = values.iterator
val iterator2 = map.valuesIterator
while (iterator2.hasNext) {
println(iterator2.next())
}
}
}
3. Map Convert to other data types
object T18 {
def main(args: Array[String]): Unit = {
val map1 = mutable.Map("a" -> 1, "b" -> 2, "c" -> 3)
val set: Set[(String, Int)] = map1.toSet
val list: List[(String, Int)] = map1.toList
val array: Array[(String, Int)] = map1.toArray
println(set.mkString(","))
println(list.mkString(","))
println(array.mkString(","))
}
}

边栏推荐
- Spark practice case (upgraded version)
- js三种遍历数组的方法:map、forEach、filter
- Elastic open source community: Developer Recruitment
- People don't talk much, engineers don't talk much
- [machine learning network] BP neural network and deep learning-6 deep neural networks (DNN)
- From scratch, C language intensive Lecture 4: array
- Remember the major performance problems caused by a TCP packet loss
- Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号
- 2022-07-26: what is the output of the following go language code? A:5; B:hello; C: Compilation error; D: Running error. package main import ( “fmt“ ) type integer in
- Okaleido tiger will log in to binance NFT in the second round, or continue to create sales achievements
猜你喜欢

结构型模式-外观(Facade)模式

Prometheus node exporter common monitoring indicators

IP第十四天笔记

JMeter learning notes 004-csv file line number control cycle times

【独立站建设】跨境电商出海开网店,首选这个网站建设!

spark练习案例(升级版)

BSN IPFS(星际文件系统)专网简介、功能、架构及特性、接入说明
![[day02] Introduction to data type conversion, operators and methods](/img/81/e2c49a4206e5d0d05308a1fc881626.png)
[day02] Introduction to data type conversion, operators and methods

JS modify the key value of the object array

Rust:axum learning notes (1) Hello World
随机推荐
Convolution neural network -- convolution of gray image
BigDecimal pit summary & Best Practices
[day02] Introduction to data type conversion, operators and methods
【动态规划百题强化计划】11~20(持续更新中)
微信小程序轮播图
Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号
The new Internet era has come. What new opportunities will Web 3.0 bring us
法解析的外部符号 “public: virtual __cdecl nvinfer1::YoloLayerPlugin::~YoloLayerPlugin(void)“ “public: virtua
xxx is not in the sudoers file. This incident will be reported
redux三大核心
Convolution neural network -- a detailed introduction to convolution of 24 bit color images
人很话不多,工程师不耍嘴皮子
BSN IPFs (interstellar file system) private network introduction, functions, architecture and characteristics, access instructions
C get UUID
EVT interface definition file of spicy
电商分账系统重要吗,平台应该如何选择分账服务商呢?
Echart柱状图中数据显示在图上方
Ribbon load balancing principle and some source codes
Use the kubesphere graphical interface dashboard to enable the Devops function
F - Pre-order and In-order(Atcoder 255)