博客
关于我
2. Scala集合
阅读量:798 次
发布时间:2023-04-02

本文共 3828 字,大约阅读时间需要 12 分钟。

Scala集合操作详解

Scala集合提供了丰富的操作方法,涵盖构建、查询、转化等多个方面,可用于Array、Vector、Set、Map等集合类型。本文将详细介绍Scala集合的核心操作特性。


一、通用操作

Scala集合的操作可以分为以下几个方面:

1.1 Builders

Scala的Builder类似于Java中的ArrayList,可以用于创建初始大小未知的数组。与ArrayList不同的是,Builder的操作需要调用result方法才能获取最终数组。需要注意的是,result方法返回的并不是底层存储数据本身的数组,而是对其的拷贝。

val b = Array.newBuilder[Int]b += 1b += 2val result = b.result// 返回 Array(1, 2)

1.2 Factory Methods

Scala提供了一些工厂方法,用于快速创建集合:

@ Array.fill(5)("hello")res20: Array[String] = Array("hello", "hello", "hello", "hello", "hello")@ Array.tabulate(5)(n => s"hello $n")res21: Array[String] = Array("hello 0", "hello 1", "hello 2", "hello 3", "hello 4")@ Array(1, 2, 3) ++ Array(4, 5, 6)res22: Array[Int] = Array(1, 2, 3, 4, 5, 6)

1.3 Transforms

Scala的集合操作可以通过mapfilter等方法对集合进行转换:

@ Array(1, 2, 3, 4, 5).map(i => i * 2)res23: Array[Int] = Array(2, 4, 6, 8, 10)@ Array(1, 2, 3, 4, 5).filter(i => i % 2 == 1)res24: Array[Int] = Array(1, 3, 5)@ Array(1, 2, 3, 4, 5).take(2)res25: Array[Int] = Array(1, 2)@ Array(1, 2, 3, 4, 5).drop(2)res26: Array[Int] = Array(3, 4, 5)@ Array(1, 2, 3, 4, 5).slice(1, 4)res27: Array[Int] = Array(2, 3, 4)@ Array(1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7, 8).distinctres28: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8)

1.4 Querys

Scala的集合操作提供了多种查询方法,如findexists等:

@ Array(1, 2, 3, 4, 5, 6, 7).find(i => i % 2 == 0 && i > 4)res29: Option[Int] = Some(6)@ Array(1, 2, 3, 4, 5, 6, 7).find(i => i % 2 == 0 && i > 10)res30: Option[Int] = None@ Array(1, 2, 3, 4, 5, 6, 7).find(i => i % 2 == 0 && i > 2)res31: Option[Int] = Some(4)@ Array(1, 2, 3, 4, 5, 6, 7).exists(x => x > 1)res32: Boolean = true@ Array(1, 2, 3, 4, 5, 6, 7).exists(_ < 0)res33: Boolean = false

1.5 Aggregations

Scala的集合操作还支持聚合功能,如mkStringfoldLeft等:

@ Array(1, 2, 3, 4, 5, 6, 7).mkString(",")res36: String = "1,2,3,4,5,6,7"@ Array(1, 2, 3, 4, 5, 6, 7).mkString("[", ",", "]")res37: String = "[1,2,3,4,5,6,7]"
@ Array(1, 2, 3, 4, 5, 6, 7).foldLeft(0)((x, y) => x + y)res40: Int = 28@ Array(1, 2, 3, 4, 5, 6, 7).foldLeft(1)((x, y) => x * y)
@ val grouped = Array(1, 2, 3, 4, 5, 6, 7).groupBy(_ % 2)grouped: Map[Int, Array[Int]] = Map(1 -> Array(1, 3, 5, 7), 0 -> Array(2, 4, 6))@ grouped(0)res44: Array[Int] = Array(2, 4, 6)@ grouped(1)res45: Array[Int] = Array(1, 3, 5, 7)

二、不可变集合

Scala的不可变集合通过Structural Shares技术优化了性能,只有发生变化的元素会被复制,其他元素与旧集合共享。

2.1 Immutable Vectors

Vector的实现采用字典树结构,时间复杂度为O(log n),具体复杂度取决于树的深度。以下是Vector的实现细节:

def result() = {    if (capacity != 0 && capacity == size) {        capacity = 0        elems    } else mkArray(size)}private def mkArray(size: Int): Array[Int] = {    val newelems = new Array[Int](size)    if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)    newelems}

三、可变集合

可变集合允许原地修改元素,性能更高,但需要谨慎操作以避免意外修改。

3.1 Mutable ArrayDeques

ArrayDeque是可变的线性集合,支持O(1)复杂度的查找、更新和插入删除操作。

val myArrayDeque = collection.mutable.ArrayDeque(1, 2, 3, 4, 5)myArrayDeque.removeHead()myArrayDeque.removeLast()myArrayDeque.append(6)

3.2 Mutable Sets

Scala的可变Set实现了Set接口,支持快速的插入、删除和查找操作。

@ val s = collection.mutable.Set(1, 2, 3)s: collection.mutable.Set[Int] = Set(1, 2, 3)@ s.contains(2)res82: Boolean = true@ s.contains(4)res83: Boolean = false@ s.add(4)res84: Boolean = true@ sres85: collection.mutable.Set[Int] = Set(1, 2, 3, 4)@ s.remove(1)res86: Boolean = true@ sres87: collection.mutable.Set[Int] = Set(2, 3, 4)

3.3 Mutable Maps

Scala的可变Map支持快速的元素插入、删除和查找操作。

@ val m = collection.mutable.Map("one" => 1, "two" => 2, "three" => 3)m: collection.mutable.Map[String, Int] = Map("one" => 1, "three" => 3, "two" => 2)@ m.getOrElseUpdate("three", -1)res89: Int = 3@ mres90: collection.mutable.Map[String, Int] = Map("one" => 1, "three" => 3, "two" => 2)@ m.getOrElseUpdate("four", -1)res91: Int = -1@ mres92: collection.mutable.Map[String, Int] = Map("one" => 1, "three" => 3, "four" => -1, "two" => 2)

通过以上内容,可以看到Scala集合操作的丰富性和灵活性,同时也理解了其性能优化和设计理念。

转载地址:http://uxefk.baihongyu.com/

你可能感兴趣的文章