Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Summary of List, Map and Set operations in Scala

Shulou Source: shulou.com Published: 2022-06-03 07:38:10 09月27日 Update

1. Operation summary of List, Map, Set and other functions in Scala

Package com.scala.study

Import scala.collection.immutable. {Queue, TreeMap}

Import scala.collection.mutable

/ * *

* Created by HP-PC on 2016-5-26.

, /

Object ScalaCaseDemo {

Def main (args: Array [String]): Unit = {

Println (1:: 2:: List (3,4)) / / single element joint List:List (1,2,3,4)

/ / two List are combined to form a List:List (1, 2, 3, 4)

Println (List (1,2): List (3,4))

Println (List ("Spark", "Hadoop", "Hive") .head) / / Spark

Println (List ("Spark", "Hadoop", "Hive"). Tail) / List (Hadoop, Hive)

/ / split the List set into 2 List sets: (List (1meme 2p3), List (4meme 5pc6))

Println (List (1,2,3,4,5,6). Span (_

< 4)) //将List集合中的元素用"-"进行拼接 println(List("a", "b", "c", "d").mkString("_")) //将List集合元素进行拆分,合并成一个大的List集合 println(List("Spark", "Hadoop").flatMap(_.toList)) //exists判断List集合中是否存在元素,forall是判断整行情况 println(List(List(1, 0, 0), List(0, 1, 0), List(0, 0, 0)).exists(row =>

Row.forall (_ = = 0))

/ * *

* foldLeft is the result of each calculation plus the elements in the List collection

* for example: 1, 1, 1, 1, 1, 3, 3, 3, 6, 6, 6, 10, 10, 10, 15, 15, 5, 10, 15, 5, 10, 15, 5050.

, /

Println ((1 to 100) .toList.foldLeft (0) (_ + _)

/ * *

* foldRight is the element in each List collection minus the calculation result.

* for example: 1-100 =-99 ~ (- 99) = 101 ~ (101) =-98 ~ (- 98) = 102 ~ (- 97)

, /

Println (List (1,2,3,4,5) .foldRight (100,100) (_ _))

Println (List.apply (1,2,3,4,5))

Println (List ("b", "e", "a", "f") .sortWith (_

< _)) //List排序输出 println(List.make(3, 5)) //构造List重复元素:List(5,5,5) println(List.range(1, 5)) //List(1,2,3,4) println(List.range(1, 9, 3)) //List中的元素是按间隔生成:List(1,4,7) //拉链操作:List((a,1), (b,2), (c,3), (d,4), (e,5)) val zipped = "abcde".toList zip List(1, 2, 3, 4, 5) println(zipped) println(zipped.unzip) //解拉链:(List(a, b, c, d, e),List(1, 2, 3, 4, 5)) //List集合进行合并:List(1, 2, 3, 4, 5) println(List(List(1, 2), List(3, 4), List(5)).flatten) //List集合进行合并:List(1, 2, 3, 4, 5) println(List.concat(List(1, 2), List(3, 4), List(5))) //两个List按给定的函数进行操作:List(300, 1200) println(List.map2(List(100, 200), (List(3, 6)))(_ * _)) val empty = Queue[Int]() val queue1 = empty.enqueue(1) val queue2 = queue1.enqueue(List(2, 3, 4, 5)) println(queue2) val (element, left) = queue2.dequeue println(element + ":" + left) //创建可变Set val data = mutable.Set.empty[Int] data ++= List(2, 3, 4) data += 4 //Set:重复数据不会添加 println(data) data --= List(2, 3) //移除List集合 println(data) data.clear() //清空Set集合 println(data) //创建可变Map val map = mutable.Map.empty[String, String] map("Java") = "Hadoop" map += { "Scala" ->

"Spark"

}

Map + = ("Scala"-> "Tachyon") / / same Key, overridden by value

Println (map)

Println (map ("Java"))

For ((k, v) "Spark", "Java"-> "Hadoop")

Println (treemap)

}

}

Tags: Element two function that is zipper result variable sort union output type same ascending order single case data merge generate Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Xiaomi Redmi Microsoft Linux