Learn scala--Map&Tuple together

Keywords: Scala Java

;

Map is an iterative set of key value pairs. Using maps in scala is very simple.

Construct Map

//Immutable map (immutable map, immutable value)
// With initial value
// In scala, the - > operator is used to create key value pairs of tuples.
val inmutableMap = Map(("yoleen" -> 20), ("hq", 26))
// It does not have an initial value, and it is not recommended to build such an immutable Map. Immutable Map add data all return a new Map.
val stringToInt = new scala.collection.immutable.HashMap[String, Int]
//mutable Map (mutable Map, add, delete, update elements return to themselves)
val mutableMap: mutable.Map[String, Int] = scala.collection.mutable.Map(("yoleen" -> 20), ("hq", 26))
//No initial value
val stringToInt = new scala.collection.mutable.HashMap[String, Int]
//If you have an initial value, you don't need to use new

Get value in map

val mutableMap: mutable.Map[String, Int] = scala.collection.mutable.Map(("yoleen" -> 20), ("hq", 26))
val v: Option[Int] = mutableMap.get("yoleen") //If the key exists and returns some object, the result is encapsulated. There is no None returned
val v1: Int = mutableMap("yoleen")// Use the apply method to get the value without option encapsulation.
val v2: Int = mutableMap.getOrElse("yoleen", 10)//Return the default value if the key does not exist
val v3: Int = mutableMap.getOrElseUpdate("yoleen1",op(5,6))//If the key does not exist, return the default value and add it to the map.

Add & update values in map

val mutableMap: mutable.Map[String, Int] = scala.collection.mutable.HashMap[String,Int]
    mutableMap+=("yoleen"->10)
    mutableMap+=("yoletpig"->10,"alex"->20) //Add multiple key value pairs
    mutableMap++=mutable.Map(("tony" -> 20), ("jskon", 26)) //Add all key value pairs in another map.
    mutableMap.put("kudu",6)//Update value if key exists
    mutableMap("yoleen") = 20//Update value
    mutableMap.transform((s, i) => i * 2)//Perform * 2 operation on the value corresponding to each key

Remove key value pair

mutableMap-="yoleen" 
mutableMap-=("yoletpig","jskon") //Remove multiple key value pairs
val option: Option[Int] = mutableMap.remove("kudu") //Remove key value pair return option object
mutableMap.retain((s,i)=>s.startsWith("y") && i>=10) //Only key value pairs that start with "y" and have a value greater than or equal to 10 are reserved
mutableMap.clear()//Clear all key value pairs

Iterative mapping

for (elem <- mutableMap) {
 elem // tuple type (String,Int)
}
for ((k,v) <- mutableMap) {
  //k. V follow value
}
for (elem <- mutableMap.keySet) {
  //elem is key  
}
for (elem <- mutableMap.values) {
  //elem is value
}

Other mappings

In addition to the commonly used HashMap, there are SortMap, LinkedHashMap, TreeMap and so on.

  • SortMap accesses the keys in the map in order.
  • LinkedHashMap is accessed in the order of insertion.
  • TreeMap key sort.
    If you need to interoperate with java, just add implicit transformation.
import scala.collection.JavaConversions.mapAsScalaMap

Tuple

The value of a tuple is constructed by including a single value in (). For example:

(1,3.14,"hello") //Unlimited types
val t = (1,3.14,"hello")
//We can go through it_ 1 ._ 2 to access elements in tuples
val a = t._1 // 1
val b = t._2 // 3.14
//Note that the tuple subscript starts at 1.
//We can use pattern matching to get values in tuples.
val (first,_,third) = t
// first=1,third="hello"

Zipper operation of tuples

val sy = Array("<","-",">")
val cnt = Array(2,3,2)
val pairs = sy.zip(cnt) //Array(("<",2),("-",3),(">",2))
for((s,n)<-pairs) print(s*n) // <<--->>

It's not easy to code. Please click recommend if you like.

Posted by pklover on Wed, 27 May 2020 08:13:38 -0700