[Scala04]Scala中的集合和高阶函数
1、集合的类的层次关系
2、List类的常用操作
List类,集合中使用最多的类
List[T],T为集合中的元素的类型,由于scala会自动进行类型推导,所以List中的类型不指定也是可以的
示例:
scala> val a=List(1,2,3,4)
a: List[Int] = List(1, 2, 3, 4)
scala> val b=0::a
b: List[Int] = List(0, 1, 2, 3, 4)
scala> val c=a::2
<console>:12: error: value :: is not a member of Int
val c=a::2
#::是连接操作符,会将左边的元素添加到集合的头部,只能元素::集合,不能集合::元素
scala> val d="a"::"b"::"c"::Nil
d: List[String] = List(a, b, c)
#Nil代表一个空集合
scala> val e=a:::b
e: List[Int] = List(1, 2, 3, 4, 0, 1, 2, 3, 4)
#:::连接操作符,会将左右两边的集合连接放到一个集合内部
scala> a.head
res0: Int = 1
#集合.head会将集合中的第一个元素打印出来
scala> a.tail
res1: List[Int] = List(2, 3, 4)
#集合.tail会将集合中的除第一个元素之外的其他元素打印出来
scala> a.isEmpty
res2: Boolean = false
#集合.isEmpty判断一个集合是否为空集合
利def find(x:List[Int]):Any={用head()、tail()、isEmpty()循环遍历集合中的元素
scala> def find(x:List[Int]):Any={
|if(x.isEmpty){
|println("print is over")
|}else{
|println(x.head)
|find(x.tail)
|}
|}
scala> find(a)
1
2
3
4
print is over
res3: Any = ()
3、List中的高阶函数
1)filter过滤函数
scala> val a =1 to 10
a: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> a.filter(x => x%2== 1)
res4: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 3, 5, 7, 9)
2)toList函数
scala> "asfsgsgqwee".toList
res5: List[Char] = List(a, s, f, s, g, s, g, q, w, e, e)
scala> (1 until 10).toList
res6: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
3)过滤出集合中的数字
scala> "sabkadhsbk1289u1".toList
res7: List[Char] = List(s, a, b, k, a, d, h, s, b, k, 1, 2, 8, 9, u, 1)
scala> res7.filter(x => Character.isDigit(x))
res9: List[Char] = List(1, 2, 8, 9, 1)
4)takeWhile函数
scala> res7.takeWhile(x => x !='h')
res14: List[Char] = List(s, a, b, k, a, d)
4、Map高阶函数
Map中的元素主要是映射用的,一个值进去,一个值出来
scala> val a=List("as","dasd","dadas")
a: List[String] = List(as, dasd, dadas)
scala> a.map(x => x.toUpperCase)
res19: List[String] = List(AS, DASD, DADAS)
scala> a.map(_.toUpperCase)
res20: List[String] = List(AS, DASD, DADAS)
scalscala> val a=List(List(1,1,4,5,8),List(1,0,8,3,4,3))
a: List[List[Int]] = List(List(1, 1, 4, 5, 8), List(1, 0, 8, 3, 4, 3))
scala> a.map(x => x.filter(_ % == 1))
res21: List[List[Int]] = List(List[1,1,5],List[1,3,3])
scala>a.flapMap(x => x.filter(_%2==1))
res21: List[List[Int]] = List(List[1,1,5],List[1,3,3])
5、集合的规约操作
将集合中的元素通过一定的运算或操作将其规约为一个值
以下是reduceLeft示例
scala> val a =List(1,2,3,4)
a: List[Int] = List(1, 2, 3, 4)
scala> a.reduceLeft((x,y)=>x+y)
res27: Int = 10
scala> a.reduce((x,y)=>x+y)
res28: Int = 10
scala> a.reduce(_+_)
res29: Int = 10
scala> a.reduceLeft(_+_)
res30: Int = 10
以下是flodLeft示例
scala> val a = List(2,3,4,5)
a: List[Int] = List(2, 3, 4, 5)
scala> a.foldLeft(0)(_+_)
res37: Int = 14
scala> a.foldLeft(1)(_*_)
res38: Int = 120
#foldLeft比reduceLeft更通用,应为前者的返回值类型取决于第一个括号内初始值的类型,但后者的返回值类型却一定为集合中元素类型
6、Range
scala> 1 to 10
res39: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> Range(1,10)
res40: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> 1 until 10
res41: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> 1 to 10 by 2
res42: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9)
scala> Range(1,10,2)
res43: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9)
scala> 1 until 10 by 2
res44: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9)
scala> (1 until 10 by 2).toList
res45: List[Int] = List(1, 3, 5, 7, 9)
7、Stream
Stream is a lazy List,是一个惰性求值的集合
scala> 2#::3#::4#::Stream.empty
res46: scala.collection.immutable.Stream[Int] = Stream(2, ?)
scala> (1 to 10).toStream
res47: scala.collection.immutable.Stream[Int] = Stream(1, ?)
scala> res46.head
res48: Int = 2
scala> res46.tail
res49: scala.collection.immutable.Stream[Int] = Stream(3, ?)
scala> res46.isEmpty
res50: Boolean = false
#List中有个方法Stream中一般也有
8、Tuple
1)定义方式:
scala> val a=(1,"ALICE","MATH",95.5)
a: (Int, String, String, Double) = (1,ALICE,MATH,95.5)
2)访问方式:
scala> a._1
res51: Int = 1
scala> a._2
res52: String = ALICE
9、Map
scala> val a=Map(1 -> "David",2 -> "Alice")
a: scala.collection.immutable.Map[Int,String] = Map(1 -> David, 2 -> Alice)
scala> a(1)
res0: String = David
scala> a(2)
res1: String = Alice
scala> a.contains(1)
res3: Boolean = true
scala> a.contains(3)
res4: Boolean = false
scala> a.keys
res5: Iterable[Int] = Set(1, 2)
scala> a.values
res7: Iterable[String] = MapLike(David, Alice)
scala> a+(3 -> "Bob")
res8: scala.collection.immutable.Map[Int,String] = Map(1 -> David, 2 -> Alice, 3 -> Bob)
scala> a ++ List(4 -> "Lily",5 -> "Lucy")
res11: scala.collection.immutable.Map[Int,String] = Map(1 -> David, 2 -> Alice, 4 -> Lily, 5 -> Lucy)
scala> a-1
res12: scala.collection.immutable.Map[Int,String] = Map(2 -> Alice)
scala> res11--List(1,2)
res16: scala.collection.immutable.Map[Int,String] = Map(4 -> Lily, 5 -> Lucy)
10、
:paste 启动编辑一大块代码的模式
ctrl+d退出该模式