Operating Scala study notes -10- container

  • Traversal
    • List traversal
val list1 = (1 to 100).toList

list1.foreach(i => println(i))

list1 foreach(i => println(i))

list1 foreach println
    •  Map traversal
scala> val mapDogs = Map ( " huahua" -> " schnauzer", "erdan" -> "Akita", "happy" -> "Teddy") 
Scala> mapDogs the foreach {kV => the println (kV. + _1 ":" + kv._2)}
huahua: Schnauzer
erdan: Akita
happy: Teddy

// mapDogs foreach {x => x match {case (k, v) => println (k + ":" + v)}} // The following is a short form
Scala> mapDogs the foreach {Case (K, V) => the println (K + ":" + V)}
huahua: Schnauzer
erdan: Akita
happy: Teddy

  •  map () method-one mapping []

The function is applied to each element of a collection, mapping to a new element. method returns a map of the original container of the same type of a new container size, but may be different element types.

Scala> Val dogList = List ( "Happy", "huahua", "erdan", "hongshao") 
// first written: placeholder written 
scala> dogList.map (. _ toUpperCase) // center also written can 
res41: List [String] = List (HAPPY, huahua, ERDAN, HONGSHAO) 
// second writing: anonymous direct transfer function 
scala> dogList.map (x => x.toUpperCase ) // center may be written 
res42 : List [String] = List (HAPPY, huahua, ERDAN, HONGSHAO) 
// third writing: define anonymous function then passed 
Scala> Val Upper: String => {String = STR => str.toUpperCase} 
Upper: String => String = <function1> 

scala> dogList.map (Upper) may be written in the home // 
res43: List [String] = List (HAPPY, huahua, ERDAN, HONGSHAO) 

// fourth wording: non-anonymous function
scala > DEF Upper (STR: String): String = {} str.toUpperCase
Upper: (STR:String)String

scala> dogList.map (upper) // center wording may
res44: List [String] = List (HAPPY, HUAHUA, ERDAN, HONGSHAO)
  •  flatMap () many mapping

When a function is applied to the container elements, a container return (not an element) of each element will, then these containers flatMa "flat shot" into a container return.

Returns the type of the original container through the box type, but may be different capitalization, it may be different types.

scala> dogList flatMap(s => s.toList)
res46: List[Char] = List(h, a, p, p, y, h, u, a, h, u, a, e, r, d, a, n, h, o, n, g, s, h, a, o)
  •  filter
    • Traverse a container from which to obtain the elements specified conditions are met, it returns a new container
    • The method accepts a return filter Boolean function f as a parameter, and f acting on each element, the element will return a true value f to form a new container return
scala> dogList filter(x=>x.contains("h")) //dogList filter(_.contains("h"))
res48: List[String] = List(happy, huahua, hongshao)

scala> (1 to 100).toList.filter(x => x%5 == 0) // (1 to 100).toList.filter(_%5 == 0)
res51: List[Int] = List(5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100)
  •  filterNot return does not meet the conditions of the elements
scala> dogList filterNot(x=>x.contains("py"))
res54: List[String] = List(huahua, erdan, hongshao)
  •  exists in the collection contains a specified condition element
scala> dogList exists(x=>x.contains("py"))
res55: Boolean = true
  •  Option values ​​find returns packaging (with values: Some, no value: None)
Scala> dogList Find (X => x.contains ( "Py")) 
res56: Option-[String] = s Some (Happy) 

. Scala> dogList.find (X => x.contains ( "Py")) // GET Some class calls the get method to get the element to be encapsulated
res59: String = happy

 

 

-----

Guess you like

Origin www.cnblogs.com/wooluwalker/p/12307940.html