学习scala笔记--23 scala集合

本文深入探讨了Scala中的List、LinkedList、Set、LinkedHashSet和SortedSet等集合类型,详细讲解了它们的特点、操作方法及函数编程应用,如map、flatMap、foreach、zip等,展示了Scala集合的强大功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

集合

 

List 是不可变的

 

scala> List

res26: scala.collection.immutable.List.type = scala.collection.immutable.List$@6ada0261

 

scala> val list = List(1,2,3,4)

list: List[Int] = List(1, 2, 3, 4)

 

scala> list.head

res27: Int = 1

 

scala> list.tail

res28: List[Int] = List(2, 3, 4)

 

注意这里的  ::

scala> val list2=0::list 

list2: List[Int] = List(0, 1, 2, 3, 4)

 

 

 

scala> def decorator(l:List[Int],prefix :String){ 

     | if(l != Nil ){

     |    println(prefix+l.head)

     |    decorator(l.tail,prefix)

     |  }

     | }

decorator: (l: List[Int], prefix: String)Unit

 

 

scala> decorator(List(1,2,3,4,5),"--")

--1

--2

--3

--4

--5

 

----------------------------------

 

 

LinkedList

 

首个元素   .elem

之后的元素 .next

 

scala> var linkedList = scala.collection.mutable.LinkedList(1, 2, 3, 4, 5)

<console>:11: warning: object LinkedList in package mutable is deprecated (since 2.11.0): low-level linked lists are deprecated

       var linkedList = scala.collection.mutable.LinkedList(1, 2, 3, 4, 5)

                                                 ^

linkedList: scala.collection.mutable.LinkedList[Int] = LinkedList(1, 2, 3, 4, 5)

 

scala> linkedList

res0: scala.collection.mutable.LinkedList[Int] = LinkedList(1, 2, 3, 4, 5)

 

scala> linkedList.elem

res1: Int = 1

 

scala> linkedList.head

res2: Int = 1

 

scala> linkedList.next

res3: scala.collection.mutable.LinkedList[Int] = LinkedList(2, 3, 4, 5)

 

scala> linkedList.tail

res4: scala.collection.mutable.LinkedList[Int] = LinkedList(2, 3, 4, 5)

 

 

 

 

while 循环  对每个元素 放大2倍

 

<console>:11: warning: object LinkedList in package mutable is deprecated (since 2.11.0): low-level linked lists are deprecated

        var linkedList = scala.collection.mutable.LinkedList(1, 2, 3, 4, 5)

                                                  ^

linkedList: scala.collection.mutable.LinkedList[Int] = LinkedList(1, 2, 3, 4, 5)

 

scala> var curr = linkedList

curr: scala.collection.mutable.LinkedList[Int] = LinkedList(1, 2, 3, 4, 5)

 

scala> while(curr!= Nil){

     |  curr.elem = curr.elem *2

     |   curr= curr.next

     | }

 

scala> curr

res6: scala.collection.mutable.LinkedList[Int] = LinkedList()

 

scala> linkedList

res7: scala.collection.mutable.LinkedList[Int] = LinkedList(2, 4, 6, 8, 10)

 

 

 

while 循环  对每隔元素 放大2倍

 

scala>  var linkedList = scala.collection.mutable.LinkedList(1, 2, 3, 4, 5,6,7,8,9,10)

<console>:11: warning: object LinkedList in package mutable is deprecated (since 2.11.0): low-level linked lists are deprecated

       var linkedList = scala.collection.mutable.LinkedList(1, 2, 3, 4, 5,6,7,8,9,10)

                                                 ^

linkedList: scala.collection.mutable.LinkedList[Int] = LinkedList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

 

scala> var curr = linkedList

curr: scala.collection.mutable.LinkedList[Int] = LinkedList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

 

scala> while(curr!= Nil){

     |  curr.elem = curr.elem *2

     |  curr= curr.next.next

     | }

 

scala> linkedList

res1: scala.collection.mutable.LinkedList[Int] = LinkedList(2, 2, 6, 4, 10, 6, 14, 8, 18, 10)

 

 

 

----------------------------------

 

Set 是不可变的

 

 

scala> Set

res2: scala.collection.immutable.Set.type = scala.collection.immutable.Set$@4264beb8

 

scala> val set = Set(1,1,2,2,2,2,3,4)

set: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)

 

 

 

LinkedHashSet  会维护添加的顺序

 

scala> val s = scala.collection.mutable.LinkedHashSet[Int]()

s: scala.collection.mutable.LinkedHashSet[Int] = Set()

 

scala> s += 1

res6: s.type = Set(1)

 

scala> s += 3

res7: s.type = Set(1, 3)

 

scala> s += 5

res8: s.type = Set(1, 3, 5)

 

scala> s += 4

res9: s.type = Set(1, 3, 5, 4)

 

 

SortedSet 会自动给元素排序

 

scala> val s= scala.collection.mutable.SortedSet[String]()

s: scala.collection.mutable.SortedSet[String] = TreeSet()

 

scala> s += "d"

res10: s.type = TreeSet(d)

 

scala> s += "c"

res11: s.type = TreeSet(c, d)

 

scala> s += "b"

res12: s.type = TreeSet(b, c, d)

 

scala> s += "a"

res13: s.type = TreeSet(a, b, c, d)

 

----------------------------------

 

 

集合的函数编程

 

 

使用map 给list 每个元素添加前缀

 

scala> val list = List("a","b","c","d")

list: List[String] = List(a, b, c, d)

 

scala> list.map("+"+ _)

res14: List[String] = List(+a, +b, +c, +d)

 

 

 

 

flatMap 拆分 打平 为多个元素的集合 

 

scala> val list = List("hello world","hello java","hello scala","hello hadoop")

list: List[String] = List(hello world, hello java, hello scala, hello hadoop)

 

scala> val list2 = list.flatMap(_.split(" "))

list2: List[String] = List(hello, world, hello, java, hello, scala, hello, hadoop)

 

scala>  list2 

res19: List[String] = List(hello, world, hello, java, hello, scala, hello, hadoop)

 

scala>  list

res20: List[String] = List(hello world, hello java, hello scala, hello hadoop)

 

 

 

 

 

foreach遍历

 

scala> list.foreach(println(_))

hello world

hello java

hello scala

hello hadoop

 

 

 

zip 拉链操作

 

scala> val list1 = List(1,2,3,4)

list1: List[Int] = List(1, 2, 3, 4)

 

scala> val list2 = List("a","b","c","d")

list2: List[String] = List(a, b, c, d)

 

scala> list1.zip(list2)

res21: List[(Int, String)] = List((1,a), (2,b), (3,c), (4,d))

 

 

scala> val aa = Array(10,2,3)

aa: Array[Int] = Array(10, 2, 3)

 

scala> aa.reduceLeft(_ - _)

res14: Int = 5

 

注意这个 reduceRight  相当于 10 - ( 2 - 3)

scala> aa.reduceRight(_ - _)

res15: Int = 11

 

 

 

----------------------------------

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值