[comment]: # Scala Collection简介

Traversable vs Iterable

Traversable, Iterable 都是trait。

Iterable 继承 Traversable。

Traversable: 支持foreach.

Iterable: 支持Interator方法。

Immutable vs mutable

Scala的Collection有Immutable和mutable两个大家族。

Immutable: 不可变。初始化后不会发生变化。scala的默认collections。性能更好。

Mutable: 可变。初始化后,可以发生变化。在多线程的访问时,会使用到锁。

可以定义event,来监视数值的变化。

        // Immutable vs mutable
println("--- Immutable vs mutable ---")
val listImm = 1 to 5
println("Immutable list: cannot do: listImm(0) = 100")
println("Immutable list: " + listImm)
val listMutable = scala.collection.mutable.MutableList[Int](1,2,3,4,5)
listMutable(0) = 100
println("Mutable list: " + listMutable)

输出:

--- Immutable vs mutable ---
Immutable list: cannot do: listImm(0) = 100
Immutable list: Range(1, 2, 3, 4, 5)
Mutable list: MutableList(100, 2, 3, 4, 5)

Seq vs Set vs Map

Seq, Set, Map都是trait。

Seq: 对象可以重复。

Set: 对象不能重复。

Map: 是一个key-value实现,key不能重复。

LinearSeq vs IndexedSeq

LinearSeq, IndexedSeq都是trait。

LinearSeq: 提供高效head and tail的分割.

IndexedSeq: 提供高效的随机访问。

        // Support head and tail
println("--- head and tail ---")
val list1 = Seq(1, 2, 3)
println(list1)
println("head and tail: split head and tail.")
list1 match {
case h::t => println("head: " + h + "\ntail: " + t)
}

输出:

--- head and tail ---
List(1, 2, 3)
head and tail: split head and tail.
head: 1
tail: List(2, 3)

TreeSet vs HashSet vs BitSet

TreeSet, HashSet, BitSet都是class。

TreeSet: 一个树的Set实现。通过值的大小判断,需要一个implicit Ordering的实现。

HashSet: 一个Set实现, 使用Hash值来确定对象的唯一性。

BitSet: 一个只存储Long的Set实现,返回boolean值。用于管理大量的标志位。

        // BitSet
println("--- BitSet ---")
println("BitSet: Used to store and access a large amount of flags.")
val bitSet = scala.collection.mutable.BitSet(1,3,4)
bitSet.remove(4)
bitSet.add(5)
println(bitSet)
for(i <- (1 to 5)) println("BitSet: " + i + ": " + bitSet(i))

输出:

--- BitSet ---
BitSet: Used to store and access a large amount of flags.
BitSet(1, 3, 5)
BitSet: 1: true
BitSet: 2: false
BitSet: 3: true
BitSet: 4: false
BitSet: 5: true

TreeMaps vs HashMaps

TreeMaps, HashMaps都是class。

TreeMaps: 一个Tree的Map实现。

HashMaps: 一个Hash key的Map实现。

Vector vs List vs Stream

Vector, List, Stream都是immutable。

Vector: 对于随机访问性能最好。推荐使用。

List: 对于head/tail的访问性能最好。

Stream: lazy估值,主要用于无限数列(infinite sequences)。

        // Stream
println("--- Stream ---")
val fibs: Stream[Int] = {
def f(a: Int, b: Int): Stream[Int] = a #:: f(b, a + b)
f(0, 1)
}
println("Stream: is lazy: " + fibs)
fibs(5)
println("Stream: after get 5: " + fibs)
println("--- ")
println("Stream: an infinite stream of incrementing numbers starting from 1 ")
println(List("a", "b", "c") zip (Stream from 1))

输出:

--- Stream ---
Stream: is lazy: Stream(0, ?)
Stream: after get 5: Stream(0, 1, 1, 2, 3, 5, ?)
---
Stream: an infinite stream of incrementing numbers starting from 1
List((a,1), (b,2), (c,3))

Views

Views类似于数据库的view,lasy,性能很好。

map vs zip vs drop/take vs filter vs group vs sliding

map 每个元素到一个函数,把所有函数的结果组成一个新的collection

println("Map: " + listMap + " to: " + listMap.map(x => x * x))
println(listMap map (x => x * x))

输出:

--- map ---
Map: Range(1, 2, 3, 4, 5) to: Vector(1, 4, 9, 16, 25)
Vector(1, 4, 9, 16, 25)

filter:生成一个条件过滤后的Range

        // filter
println("--- filter ---")
val listFilter = (1 to 10)
println("filter: " + listFilter + " to: " + listFilter.filter(x => x % 2 == 0))
println(listFilter filter (x => x % 2 == 0))

输出:

--- filter ---
filter: Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) to: Vector(2, 4, 6, 8, 10)
Vector(2, 4, 6, 8, 10)

drop & filter:从当前的List中选取一段,生成一个Range

        // drop and take
println("--- drop and take ---")
val listTake = (1 to 10)
println("take: " + listTake + " to: " + listTake.drop(5).take(3))
println(listTake drop(5) take(3))

输出:

--- drop and take ---
take: Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) to: Range(6, 7, 8)
Range(6, 7, 8)

zip: 将两个List做列合并。

        // zip
println("--- zip ---")
val listZip1 = (1 to 5)
val listZip2 = List("A", "B", "C", "D", "E")
println("Zip: " + listZip1 + " and " + listZip1 + " to: " + listZip1.zip(listZip2))
println(listZip1 zip listZip2)

输出:

--- zip ---
Zip: Range(1, 2, 3, 4, 5) and Range(1, 2, 3, 4, 5) to: Vector((1,A), (2,B), (3,C), (4,D), (5,E))
Vector((1,A), (2,B), (3,C), (4,D), (5,E))

grouped: 将collection按照指定的size组合成多个Vector,返回这个List的iterator。

        // grouped
println("--- grouped ---")
val listgroup = (1 to 10)
val listgroupIterator = listgroup.grouped(3)
println("grouped: " + listgroup + " with size 3 to: " + listgroupIterator)
listgroupIterator foreach println

输出:

--- grouped ---
grouped: Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) with size 3 to: non-empty iterator
Vector(1, 2, 3)
Vector(4, 5, 6)
Vector(7, 8, 9)
Vector(10)

sliding: 将collection按照指定的size组合成一个滑动的块,返回这个块的iterator。

        // sliding
println("--- sliding ---")
val listSliding = (2 to 10 by 2)
val listSlidingIterator = listSliding.sliding(3)
println("sliding: " + listSliding + " with size 3 to: " + listSlidingIterator)
listSlidingIterator foreach println
println("----")
println("sliding: tails")
listSliding.tails foreach println

输出:

--- sliding ---
sliding: Vector(2, 4, 6, 8, 10) with size 3 to: non-empty iterator
Vector(2, 4, 6)
Vector(4, 6, 8)
Vector(6, 8, 10)
----
sliding: tails
Vector(2, 4, 6, 8, 10)
Vector(4, 6, 8, 10)
Vector(6, 8, 10)
Vector(8, 10)
Vector(10)
Vector()

reduce vs reduceLeft vs reduceRigh vs fold vs foldLeft vs foldRight vs scan vs scanLeft vs scanRight

都适用于cumulate计算。

reduce, reduceLeft, reduceRight: 计算一个单独的累计结果。

fold, foldLeft, foldRight: 计算一个单独的累计结果,带一个起始值。

scan, scanLeft, scanRight: 得到的是一个List。List的长度和以前一样,分别是对应的单步累计结果。有一个起始种子。

reduce, fold, scan: 用于并行计算。

reduceLeft, foldLeft, scanLeft: 从前到后线性计算。

reduceRight, foldRight, scanRight: 从后到前线性计算。

foldLeft是线性计算。

        // fold & reduce & scan
println("--- fold & reduce & scan ---")
val listAdd = List(1,2,3,4,5)
def addOp(a: Int, b: Int): Int = {
println(a + ":" + b)
a + b
}
println("--- reduce")
println("reduce: " + listAdd + " to: " + listAdd.par.reduce(addOp(_, _)))
println("--- reduceLeft")
println("reduceLeft: " + listAdd + " to: " + listAdd.reduceLeft(addOp(_, _)))
println("--- reduceRight")
println("reduceRight: " + listAdd + " to: " + listAdd.reduceRight(addOp(_, _)))

输出:

--- fold & reduce & scan ---
--- reduce
1:2
4:5
3:9
3:12
reduce: List(1, 2, 3, 4, 5) to: 15
--- reduceLeft
1:2
3:3
6:4
10:5
reduceLeft: List(1, 2, 3, 4, 5) to: 15
--- reduceRight
4:5
3:9
2:12
1:14
reduceRight: List(1, 2, 3, 4, 5) to: 15
        println("--- fold")
println("fold: " + listAdd + " to: " + listAdd.par.fold(100)(addOp(_, _)))
println("--- foldLeft")
println("foldLeft: " + listAdd + " to: " + listAdd.foldLeft(100)(addOp(_, _)))
println("--- foldRight")
println("foldRight: " + listAdd + " to: " + listAdd.foldRight(100)(addOp(_, _)))

输出:

--- fold
100:1
100:2
100:5
100:3
100:4
101:102
104:105
103:209
203:312
fold: List(1, 2, 3, 4, 5) to: 515
--- foldLeft
100:1
101:2
103:3
106:4
110:5
foldLeft: List(1, 2, 3, 4, 5) to: 115
--- foldRight
5:100
4:105
3:109
2:112
1:114
foldRight: List(1, 2, 3, 4, 5) to: 115
        println("--- scan")
println("scan: " + listAdd + " to: " + listAdd.par.scan(100)(addOp(_, _)))
println("--- scanLeft")
println("scanLeft: " + listAdd + " to: " + listAdd.scanLeft(100)(addOp(_, _)))
println("--- scanRight")
println("scanRight: " + listAdd + " to: " + listAdd.scanRight(100)(addOp(_, _)))

输出:

--- scan
4:5
1:2
3:4
3:9
3:3
3:7
3:12
100:1
3:3
1:2
10:5
6:4
scan: List(1, 2, 3, 4, 5) to: ParVector(100, 101, 3, 6, 10, 15)
--- scanLeft
100:1
101:2
103:3
106:4
110:5
scanLeft: List(1, 2, 3, 4, 5) to: List(100, 101, 103, 106, 110, 115)
--- scanRight
5:100
4:105
3:109
2:112
1:114
scanRight: List(1, 2, 3, 4, 5) to: List(115, 114, 112, 109, 105, 100)

参照

Scala Collection简介的更多相关文章

  1. spark1.5 scala.collection.mutable.WrappedArray$ofRef cannot be cast to ...解决办法

    下面是我在spark user list的求助贴,很快就得到了正确回答,有遇到问题的同学解决不了也可以去上面提问. I can use it under spark1.4.1,but error on ...

  2. idea中使用scala运行spark出现Exception in thread "main" java.lang.NoClassDefFoundError: scala/collection/GenTraversableOnce$class

    idea中使用scala运行spark出现: Exception in thread "main" java.lang.NoClassDefFoundError: scala/co ...

  3. spark提示Caused by: java.lang.ClassCastException: scala.collection.mutable.WrappedArray$ofRef cannot be cast to [Lscala.collection.immutable.Map;

    spark提示Caused by: java.lang.ClassCastException: scala.collection.mutable.WrappedArray$ofRef cannot b ...

  4. scala语言简介及其环境安装

    scala语言简介及其环境安装 简介: 1.运行在JVM 上,兼容java语言 Scala的代码,都需要经过编译为字节码,然后交由Java虚拟机来运行.所以Scala和Java是可以无缝互操作的.Sc ...

  5. Apache Spark Exception in thread “main” java.lang.NoClassDefFoundError: scala/collection/GenTraversableOnce$class

    问题: 今天用Maven搭建了一个Spark的Scala项目,运行后遇到下面异常: Apache Spark Exception in thread “main” java.lang.NoClassD ...

  6. Scala语言简介和开发环境配置

    Scala语言的简介和开发环境搭建 Scala是一门结合了面向对象特征和函数式编程特征的语言,它是一个创新的编程语言产品.Scala可以做脚本(就像shell脚本一样),可以做服务端编程语言,可以写数 ...

  7. 机器学习的Spark与Scala开发简介

     一.机器学习常用开发软件:Spark.Scala 1. Spark简介: MLlib包含的库文件有: 分类 降维 回归 聚类 推荐系统 自然语言处理 在线学习 统计学习方法:偏向理论性,数理统计的方 ...

  8. Scala Collection Method

    接收一元函数 map 转换元素,主要应用于不可变集合 (1 to 10).map(i => i * i) (1 to 10).flatMap(i => (1 to i).map(j =&g ...

  9. Scala学习——简介

    一.Scala简介 Scala 是 Scalable Language 的简写,是一门多范式的编程语言,设计初衷是实现可伸缩的语言并集成面向对象编程和函数式编程的各种特性. 二.Scala 环境搭建 ...

随机推荐

  1. C#中yield return用法分析

    这篇文章主要介绍了C#中yield return用法,对比使用yield return与不使用yield return的流程,更直观的分析了yield return的用法,需要的朋友可以参考下. 本文 ...

  2. 解决HP打印机错误:Couldn't open fifo

    我的是因为选错了打印机协议,一开始选成了“互联网打印协议 - IPP”. 解决方案:删除原有打印机配置,重新选择协议为“HP Jetdirect-Socket”即可.

  3. Jquery中使用setInterval和setTimeout会提示缺少对象的错误,解决方法如下:

    直接在ready中调用其他方法,会提示缺少对象的错误,解决方法如下: 方法1. 应用jQuery的扩展可以解决这个问题. $(document).ready(function(){ $.extend( ...

  4. WebDriver兼容SeleniumRC(基于C#)

    WebDriver兼容SeleniumRC(基于C#)http://www.automationqa.com/forum.php?mod=viewthread&tid=3535&fro ...

  5. 【Android】如何写一个JsBridge

    JsBridge 简介 Android JsBridge 就是用来在 Android app的原生 java 代码与 javascript 代码中架设通信(调用)桥梁的辅助工具. 原文地址点这里 gi ...

  6. windows下配置启动多个mysql服务

    查找配置做下记录 先安装mysql5.6,安装不在介绍 接下来配置启动另一个mysql服务, 1:先到服务里停止在运行的mysql服务 2:到mysql的安装目录下(默认安装目录在c:\Program ...

  7. ODAC (V9.5.15) 学习笔记(二十)大数据量获取处理

    ODAC获取数据的效率比较高,在Web程序中希望能够更快获取第一页的数据时,可以有几种方式: 1.在数据库中进行分页处理: 2.获取所有数据,只是快速返回第一页数据. 第一种方案对应用服务器资源消耗最 ...

  8. 转:php park、unpark、ord 函数使用方法(二进制流接口应用实例)

    在工作中,我也逐渐了解到park,unpark,ord对于二进制字节处理的强大. 下面我逐一介绍它们.     park,unpark,ord这3个函数,在我们工作中,用到它们的估计不多. 我在最近一 ...

  9. 安卓开发笔记——探索EventBus

    1.关于EventBus: 组件通讯在Android开发中是不可避免的,随着业务需求的复杂化,代码中需要我们去处理的业务逻辑难度也不断增大.例如多个Fragment之间的数据传递,Service与Ac ...

  10. Careercup 论坛上较有意思的题目整理

    # 数据结构类 ### 线段树 segment tree http://www.careercup.com/question?id=5165570324430848 找区间内的value的个数 二维线 ...