Scala 学习笔记之集合(1)
package com.citi.scala
object CollectionDemo {
def main(args: Array[String]): Unit = {
/**
* List
*/
println("--------------------------List-------------------------")
val numbers = List[Int](11, 22, 33, 44, 55)
val numbers1 = List(11, 22, 33, 44, 55)
val colors = List[String]("red", "green", "blue")
println(s"colors has ${colors.size}, $colors")
//取第一个
println(colors.head)
//去掉第一个,取剩下的
println(colors.tail)
//取第二个
println(colors(1))
//遍历
for (s <- colors) { println(s) }
//高阶函数使用
colors.foreach { (s: String) => println(s) }
colors.foreach { s => println(s) }
colors.foreach { println(_) }
val elSize = colors.map((s: String) => s.size)
println(elSize)
println(numbers.reduce((a: Int, b: Int) => a + b))
//初始化空List
val ls: List[Int] = List[Int]()
val lls: List[Int] = Nil
println(ls == lls)
//遍历
var i = numbers
while (!i.isEmpty) {
println(i.head)
i = i.tail
}
i = numbers
while (i != Nil) {
println(i.head)
i = i.tail
}
//cons操作符
val consNumbers = 3 :: 2 :: 1 :: Nil
println(consNumbers)
//List的算数运算
println(3 :: 2 :: 1 :: Nil)
println(List(7, 6, 5) ::: List(4, 3, 2, 1))
println(List(4, 3, 2, 1) ++ List(7, 6, 5))
println(List(1, 2) == List(2, 1))
println(List(2, 1) == List(2, 1))
println(List(1, 2, 3, 2, 1).distinct)
println(List(1, 2, 3, 2, 1) drop (2))
println(List(4, 3, 2, 1) filter { _ >= 3 })
println(List(List(1, 2, 3), List(4, 5, 6)).flatten)
println((List(1, 2, 3, 4, 5).partition(_ >= 3)))
println(List(1, 2, 3, 4, 5) reverse)
println(List(2, 3, 5, 7, 10, 11) slice (1, 4))
println(List("apple", "ben") sortBy { s => s.size })
println(List(2, 3, 5, 7, 10, 11) splitAt (3))
println(List(2, 3, 5, 7, 10, 11) take (3))
println(List(1, 2, 3) zip List("a", "b"))
println(List(1, 2) :+ 3)
/**
* Set
*/
println("--------------------------Set-------------------------")
val numbersets = Set[Int](11, 22, 11, 33, 44, 55)
println(s"numbersets has ${numbersets.size}, $numbersets")
println(numbersets.reduce((a: Int, b: Int) => a + b))
/**
* Map
*/
println("--------------------------Map-------------------------")
val colorMap = Map[String, Int]("red" -> 1, "green" -> 2, "yellow" -> 3)
println(colorMap("red"))
for (pair <- colorMap) { println(pair) }
}
}
运行结果:
--------------------------List-------------------------
colors has 3, List(red, green, blue)
red
List(green, blue)
green
red
green
blue
red
green
blue
red
green
blue
red
green
blue
List(3, 5, 4)
165
true
11
22
33
44
55
11
22
33
44
55
List(3, 2, 1)
List(3, 2, 1)
List(7, 6, 5, 4, 3, 2, 1)
List(4, 3, 2, 1, 7, 6, 5)
false
true
List(1, 2, 3)
List(3, 2, 1)
List(4, 3)
List(1, 2, 3, 4, 5, 6)
(List(3, 4, 5),List(1, 2))
List(5, 4, 3, 2, 1)
List(3, 5, 7)
List(ben, apple)
(List(2, 3, 5),List(7, 10, 11))
List(2, 3, 5)
List((1,a), (2,b))
List(1, 2, 3)
--------------------------Set-------------------------
numbersets has 5, Set(33, 22, 44, 11, 55)
165
--------------------------Map-------------------------
1
(red,1)
(green,2)
(yellow,3)
Scala 学习笔记之集合(1)的更多相关文章
- Scala 学习笔记之集合(3)
建立一个Java类,为了演示Java集合类型向Scala集合的转换: import java.util.ArrayList; import java.util.List; public class S ...
- scala学习笔记:集合
scala> 1 to 10 res9: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9 ...
- Scala 学习笔记之集合(7) Option
object CollectionDemo8 { def main(args: Array[String]): Unit = { //Option集合的使用,可以用来安全的判断null或非null,放 ...
- Scala 学习笔记之集合(6)
object CollectionDemo7 { def main(args: Array[String]): Unit = { //数组使用 val arr = Array("red&qu ...
- Scala 学习笔记之集合(5)
import collection.mutable.Buffer object CollectionDemo6 { def main(args: Array[String]): Unit = { // ...
- Scala 学习笔记之集合(4)
集合的模式匹配操作: object CollectionDemo5 { def main(args: Array[String]): Unit = { //集合模式匹配1 val ls = List( ...
- Scala 学习笔记之集合(9) 集合常用操作汇总
object CollectionDemo10 { def main(args: Array[String]): Unit = { var ls = List[Int](1, 2, 3) //向后增加 ...
- Scala 学习笔记之集合(8) Try和Future
import util._ import concurrent.ExecutionContext.Implicits.global import concurrent.Future import co ...
- Scala 学习笔记之集合(2)
class StudentTT extends StudentT{ def sayBye(name: String, age: Int)(address: String){ println(" ...
随机推荐
- P3469 [POI2008]BLO-Blockade 割点 tarjan
题意 给定一个无向图,问删掉点i,图中相连的有序对数.(pair<x, y> , x != y);求每个点对应的答案 思路 首先我们可以发现,如果这个点不是割点,那么答案就是n-1,如果是 ...
- CodeForces 715B Complete The Graph 特殊的dijkstra
Complete The Graph 题解: 比较特殊的dij的题目. dis[x][y] 代表的是用了x条特殊边, y点的距离是多少. 然后我们通过dij更新dis数组. 然后在跑的时候,把特殊边都 ...
- hdu 4825 Xor Sum(01字典树模版题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题解:一到01字典树的模版题,01字典树就是就是将一些树用二进制放到一个树上这样可以方便对整体异 ...
- 关于ASP.NET中WEBAPI中POST请求中FromBody修饰的string类型的参数服务器端获取不到值FromBody空值的简单解决方法
其实解决办法很简单,就是POST请求的时候,来自实体的参数,content-type:application/x-www-form-urlencoded情况下,是默认按照键值对来解析的,比如param ...
- Vue中如何使用less
最近发现好多小伙伴在面试的过程中会问到vue如何使用less和scss,所以我绝对更新.复习一下less:废话不多说直接进主题: 依赖下载 1.首先使用npm下载依赖: npm install --s ...
- 封装 jsonp请求数据的方法
什么是jsonp : Jsonp(JSON with Padding) 是 json 的一种"使用模式",可以让网页从别的域名(网站)那获取资料,即跨域读取数据. 为什么我们从不 ...
- [中秋]宇宙第一 IDE Visual Studio 了解一下
官网 https://visualstudio.microsoft.com/zh-hans/vs/ 文档 https://docs.microsoft.com/zh-cn/visualstudio/d ...
- Vue2.x-社交网络程序项目的总结
最近几天一直在学习Vue的课程,通过这个项目进行进一步的学习Vue方面的知识.掌握如何使用Vue搭建前端,如何请求Node.js写好的后端接口. 一.实现前后端连载 首先在后端的文件中 vue i ...
- myeclipse一直停留在Loading workbench界面上以及停滞启动页不动的处理办法
找到myeclipse的工作目录,比如我的叫springworkspace(F:\springworkspace\.metadata\.plugins),在.metadata\.plugins中删掉以 ...
- Nginx的架构及工作流程
NGINX是一个免费的,开源的,高性能的HTTP服务器和反向代理,以及IMAP / POP3代理服务器.NGINX以其高性能,稳定性,丰富的功能集,简单的配置和低资源消耗而闻名,也是为解决C10K问题 ...