From 65bbc1b173f414cd1676c73750ae0c332f5818a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E7=A5=A5?= <1366971433@qq.com> Date: Tue, 7 May 2019 16:44:02 +0800 Subject: [PATCH] =?UTF-8?q?scala=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 13 ++- .../{Scala列表.md => Scala列表和集.md} | 100 +++++++++++++++++- 2 files changed, 105 insertions(+), 8 deletions(-) rename notes/{Scala列表.md => Scala列表和集.md} (84%) diff --git a/README.md b/README.md index 0933826..6fa07bc 100644 --- a/README.md +++ b/README.md @@ -143,10 +143,9 @@ TODO 1. [Scala简介及开发环境配置](https://github.com/heibaiying/BigData-Notes/blob/master/notes/Scala简介及开发环境配置.md) 2. [基本数据类型和运算符](https://github.com/heibaiying/BigData-Notes/blob/master/notes/Scala基本数据类型和运算符.md) 3. [流程控制语句](https://github.com/heibaiying/BigData-Notes/blob/master/notes/Scala流程控制语句.md) -4. [数组Array](https://github.com/heibaiying/BigData-Notes/blob/master/notes/Scala数组.md) -5. [集合综述](https://github.com/heibaiying/BigData-Notes/blob/master/notes/Scala集合.md) -6. [常用集合类型之——List](https://github.com/heibaiying/BigData-Notes/blob/master/notes/Scala列表.md) -7. [常用集合类型之——Map & Tuple](https://github.com/heibaiying/BigData-Notes/blob/master/notes/Scala映射和元组.md) -8. 其他常用集合类型 -9. 类和对象 -10. 函数和闭包 \ No newline at end of file +4. [数组——Array](https://github.com/heibaiying/BigData-Notes/blob/master/notes/Scala数组.md) +5. [常用集合类型之——List & Set](https://github.com/heibaiying/BigData-Notes/blob/master/notes/Scala列表和集.md) +6. [常用集合类型之——Map & Tuple](https://github.com/heibaiying/BigData-Notes/blob/master/notes/Scala映射和元组.md) +7. 集合综述与总结 +8. 类和对象 +9. 函数和闭包 \ No newline at end of file diff --git a/notes/Scala列表.md b/notes/Scala列表和集.md similarity index 84% rename from notes/Scala列表.md rename to notes/Scala列表和集.md index 587c102..960ad34 100644 --- a/notes/Scala列表.md +++ b/notes/Scala列表和集.md @@ -1,4 +1,4 @@ -# 列表(List) +# List & Set + + ## 一、List字面量 List是Scala中非常重要的一个数据结构,其与Array(数组)非常类似,但是List是不可变的,和Java中的List一样,其底层实现是链表。 @@ -432,6 +436,100 @@ res44: Boolean = true +## 九、缓冲列表ListBuffer + +上面介绍的List,由于其底层实现是链表,这意味着能快速访问List头部元素,但对尾部元素的访问则比较低效,这时候可以采用`ListBuffer`,ListBuffer提供了在常量时间内往头部和尾部追加元素。 + +```scala +object ScalaApp extends App { + + val buffer = new ListBuffer[Int] + // 1.在尾部追加元素 + buffer += 1 + buffer += 2 + // 2.在头部追加元素 + 3 +=: buffer + // 3. ListBuffer转List + val list: List[Int] = buffer.toList + println(list) +} +``` + + + +## 十、集(Set) + +Set是不重复元素的集合。分为可变Set和不可变Set。 + +### 10.1 可变Set + +```scala +object ScalaApp extends App { + + // 可变Set + val mutableSet = new collection.mutable.HashSet[Int] + + // 1.添加元素 + mutableSet.add(1) + mutableSet.add(2) + mutableSet.add(3) + mutableSet.add(3) + mutableSet.add(4) + + // 2.移除元素 + mutableSet.remove(2) + + // 3.调用mkString方法 输出1,3,4 + println(mutableSet.mkString(",")) + + // 4. 获取Set中最小元素 + println(mutableSet.min) + + // 5. 获取Set中最大元素 + println(mutableSet.max) + +} +``` + +### 10.2 不可变Set + +不可变Set没有add方法,可以使用`+`添加元素,但是此时会返回一个新的不可变Set,原来的Set不变。 + +```scala +object ScalaApp extends App { + + // 不可变Set + val immutableSet = new collection.immutable.HashSet[Int] + + val ints: HashSet[Int] = immutableSet+1 + + println(ints) + +} + +// 输出 Set(1) +``` + +### 10.3 Set间操作 + +多个Set之间可以进行求交集或者合集等操作。 + +```scala +object ScalaApp extends App { + + // 声明有序Set + val mutableSet = collection.mutable.SortedSet(1, 2, 3, 4, 5) + val immutableSet = collection.immutable.SortedSet(3, 4, 5, 6, 7) + + // 两个Set的合集 输出:TreeSet(1, 2, 3, 4, 5, 6, 7) + println(mutableSet ++ immutableSet) + + // 两个Set的交集 输出:TreeSet(3, 4, 5) + println(mutableSet intersect immutableSet) + +} +``` + ## 参考资料