In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
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)
}
}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.