Four, Scala mapping and tuples

1. Construct the mapping

Construct an immutable/variable Map

val scores = Map("a"->10,"b"->11,"c"->12)

val scores = scala.collection.mutable.Map("a"->10,"b"->11,"c"->12)

Construct from an empty map

val scores = scala.collection.mutable.Map[String,Int]()

A mapping is a set of duality. A duality is a group composed of two values. The two values ​​are not necessarily of the same type. "a"->10You can create duality with the value ("a",10)
of another way defined by the mapping.

val scores = Map(("a,10),("dsad",10))

2. Get the value in the map

Get the value can be used directly()

val bobsSocre = scores("a")
//如果映射中不存在该键,则该方法会抛出异常

Check whether the map contains a specified key, then use the containmethod

val bobSocre = if(scores.contains("a") scores("a) else 0

Or use scores.getOrElse("a",0)it directly instead of the above.
You can also set default values ​​for non-existent keys.

scores.withDefaultValue(0)//当搜索的键不存在时,就会交出0

3. Update the value in the map

①The way of updating in the variable mapping is determined by

scores("a") = 11
scores+=("cc",101)
scores-="a"

②For a non-updatable mapping, a new mapping can be generated for its operation

val newScores = scores + ("Bob"->10,"Ffff"->7)
scores += ("Bob"->10,"Ffff"->7)

scores = scores - "Bob"
scores -= "Bob"

4. Iterative mapping

Traversal map

for ((k,v) <- 映射) 处理 k 和 v 

Access only keys or values

scores.keySet

for (v<-scores.values)	println(v)

5. Sorted mapping

There are two ways to implement mapping: hash table and balanced tree.
By default, the mapping is implemented based on the hash table, because it is more efficient, but the order of the elements cannot be guaranteed. If you want to access the keys in the map once in order, Can be used SortedMap, if you want to access the keys in the order they were inserted, useLinkedHashMap

val scores = scal.collection.mutable.SortedMap(……)
val months = scala.collection.mutable.LinkedHashMap(……)

6. Interoperability with Java

① If you get a Java mapping through a Java method call, you may want to convert it to a Scala mapping, you need to add the following statement:, import scala.collection.JavaConversions.mapAsScalaMapand then trigger the conversion by specifying the Scala mapping type:

val scores: scala.collection.mutable.Map[String,Int] = new java.util.TreeMap[String,Int]

②You can also java.util.Propertiesswitch fromMap[String,String]

import scala.collection.JavaConversions.propertiesAsMap
val props:scala.collection.Map[String,String] = System.getProperties()

③There are also methods to pass the mapping in Scala to the expected Java mapping, which is not expanded here

7. Tuples

A map is a collection of key/value pairs. Duality is the simplest form of tuples-tuples are aggregations of different types of values.
Tuple example: val t =(1,3.14,”Fred“)it can be used _1,_2,_3to access the tuple component, but can be t._2written ast _2

You can use pattern matching to get the component parts of the tuple:, val (first,second,third) = tat this time first=1,second=3.14,third="Fred";
if you don't need all the parts in the tuple to be missing, you can _replace it with the parts that don't need to be used val (first,second,_)=t; the
tuple can be used for the function to return more than one worthy situation: "New York".partition(_.isUpper)And the result is("NY","ew ork")

8. Zipper operation

Zip is to tie multiple values ​​together so that they can be processed together

val symbols = Array("<-","-","->")
val count = Array(2,10,2)
val pairs = symbols.zip(count)

The result is: Array(("<-",2),("-",10),("->",2))
In addition, topMapthe duality can be converted into a mapping by using the method:keys.zip(values).toMap

Guess you like

Origin blog.csdn.net/Cxf2018/article/details/109412372