当前位置:网站首页>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(","))
}
}

边栏推荐
- BigDecimal pit summary & Best Practices
- Easy to use shell shortcuts
- 可视化领域 SVG
- ros 相机标定 sensor_msgs/CameraInfo Message 数据类型及含义
- HEAD detached from origin/...导致push失败
- 数据库泰斗王珊:努力创新,精心打磨优质的数据库产品
- xxx is not in the sudoers file. This incident will be reported
- 【day02】数据类型转换、运算符、方法入门
- [final review of software engineering] knowledge points + detailed explanation of major problems (E-R diagram, data flow diagram, N-S box diagram, state diagram, activity diagram, use case diagram...)
- Standard C language 11
猜你喜欢

JS three methods of traversing arrays: map, foreach, filter

Brightcove appoints Dan Freund as chief revenue Officer

e. The difference between target and e.currenttarget

How CentOS installs mysqldump

Practice of microservice in solving Library Download business problems

干货 | 独立站运营怎么提高在线聊天客户服务?

Network knowledge corner | it only takes four steps to teach you to use SecureCRT to connect to ENSP. You must see the operation guide of common tools

Spark practice case (upgraded version)

Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns

Using webmvcconfigurer to intercept interface requests is being enhanced (with source code)
随机推荐
ASP voice notification interface docking demo
redux三大核心
Influxdb basic understanding
Worship the 321 page PDF of the core technology of Internet entrepreneurship that Alibaba is pushing internally. It's really kneeling
Understand kingbasees V9 in one picture
利用JSON类型在mysql中实现数组功能
How to set user-defined display for Jiaming Watch
EVT interface definition file of spicy
Preliminary understanding of NiO
Nacos startup and login
Learning route from junior programmer to architect + complete version of supporting learning resources
结构型模式-外观(Facade)模式
ELS compatibility DC, transfer pictures to window
BSN IPFS(星际文件系统)专网简介、功能、架构及特性、接入说明
Convolution neural network -- convolution of gray image
Why does genericservlet have two init methods
ISG index shows that the it and business service market in the Asia Pacific region fell sharply in the second quarter
【软件工程期末复习】知识点+大题详解(E-R图、数据流图、N-S盒图、状态图、活动图、用例图....)
Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns
华为入局商用市场:趋势使然,挑战颇多