scala List

方法描述
def +(elem: A): List[A]前置一个元素列表
def ::(x: A): List[A]在这个列表的开头添加的元素。
def :::(prefix: List[A]): List[A]增加了一个给定列表中该列表前面的元素。
def ::(x: A): List[A]增加了一个元素x在列表的开头
def addString(b: StringBuilder): StringBuilder追加列表的一个字符串生成器的所有元素。
def addString(b: StringBuilder, sep: String): StringBuilder追加列表的使用分隔字符串一个字符串生成器的所有元素。
def apply(n: Int): A选择通过其在列表中索引的元素
def contains(elem: Any): Boolean测试该列表中是否包含一个给定值作为元素。
def copyToArray(xs: Array[A], start: Int, len: Int): Unit列表的副本元件阵列。填充给定的数组xs与此列表中最多len个元素,在位置开始。
def distinct: List[A]建立从列表中没有任何重复的元素的新列表。
def drop(n: Int): List[A]返回除了第n个的所有元素。
def dropRight(n: Int): List[A]返回除了最后的n个的元素
def dropWhile(p: (A) => Boolean): List[A]丢弃满足谓词的元素最长前缀。
def endsWith[B](that: Seq[B]): Boolean测试列表是否使用给定序列结束。
def equals(that: Any): Booleanequals方法的任意序列。比较该序列到某些其他对象。
def exists(p: (A) => Boolean): Boolean测试谓词是否持有一些列表的元素。
def filter(p: (A) => Boolean): List[A]返回列表满足谓词的所有元素。
def forall(p: (A) => Boolean): Boolean测试谓词是否持有该列表中的所有元素。
def foreach(f: (A) => Unit): Unit应用一个函数f以列表的所有元素。
def head: A选择列表的第一个元素
def indexOf(elem: A, from: Int): Int经过或在某些起始索引查找列表中的一些值第一次出现的索引。
def init: List[A]返回除了最后的所有元素
def intersect(that: Seq[A]): List[A]计算列表和另一序列之间的多重集交集。
def isEmpty: Boolean测试列表是否为空
def iterator: Iterator[A]创建一个新的迭代器中包含的可迭代对象中的所有元素
def last: A返回最后一个元素
def lastIndexOf(elem: A, end: Int): Int之前或在一个给定的最终指数查找的列表中的一些值最后一次出现的索引
def length: Int返回列表的长度
def map[B](f: (A) => B): List[B]通过应用函数以g这个列表中的所有元素构建一个新的集合
def max: A查找最大的元素
def min: A查找最小元素
def mkString: String显示列表的字符串中的所有元素
def mkString(sep: String): String显示的列表中的字符串中使用分隔串的所有元素
def reverse: List[A]返回新列表,在相反的顺序元素
def sorted[B >: A]: List[A]根据排序对列表进行排序
def startsWith[B](that: Seq[B], offset: Int): Boolean测试该列表中是否包含给定的索引处的给定的序列
def sum: A概括这个集合的元素
def tail: List[A]返回除了第一的所有元素
def take(n: Int): List[A]返回前n个元素
def takeRight(n: Int): List[A]返回最后n个元素
def toArray: Array[A]列表以一个数组变换
def toBuffer[B >: A]: Buffer[B]列表以一个可变缓冲器转换
def toMap[T, U]: Map[T, U]此列表的映射转换
def toSeq: Seq[A]列表的序列转换
def toSet[B >: A]: Set[B]列表到集合变换
def toString(): String列表转换为字符串

参考:
https://www.cnblogs.com/huiandong/articles/9278213.html

scala列表用Nil表示空
:: 操作符是将给定的头和尾创建一个新列表
list.可以查看list所有的方法

9 :: List(1,2)  //List[Int] = List(9, 1, 2)
val lst = List(1,2,3)
lst.head//res1: Int = 1 取第一个元素  
lst.tail // res2: List[Int] = List(2, 3) 取剩余的元素(除第一个元素)

添加元素

//左侧添加元素和添加列表里的元素
0 :: lst  //List[Int] = List(0, 1, 2, 3) 头部append元素
List(0) :: lst  // List[Any] = List(List(0), 1, 2, 3) 头部append 函数
List(0) ::: lst // List[Int] = List(0, 1, 2, 3)  将列表拆分添加

//右侧添加元素和添加列表里的元素
lst :+ 0 // List[Int] = List(1, 2, 3, 0) 将0添加到列表之后
lst :+ List(0) // List[Any] = List(1, 2, 3, List(0)) 
lst ++ List(0) // List[Int] = List(1, 2, 3, 0) 将列表拆分并在后面追加

元素操作

map

final def map[B](f: (A) => B): List[B]
相当于python中的map函数,不过在python中不是list对象的方法,而是一个内置函数

val array = Array[Int](1,2,3,4,5)
val y = array.map(x => x*2) //完全体:val y = array map((x: Int) => x*2) ,这里的`=>`相当于python中lambda中的:
val y = array.map(_ * 2)//`_`表示数组中的每个值,简化的写法,只写右侧的结果

foreach

区别于map,foreach没有返回值

val res = List(1,2,3)
res.foreach(println)

filter

def filter(p: ((String, Int)) => Boolean): List[(String, Int)]

val lst = List(("a",3),("b",1),("c",2))
lst.filter(x=>x._2>1) // List[(String, Int)] = List((a,3), (c,2)) 过滤元素第二个位置大于1的
lst.filter(_._2>1) //和上面是等价的

flatten|faltMap

final defflatMap[B](f: (A) => IterableOnce[B]): List[B]
defflatten[B](implicit toIterableOnce: (A) => IterableOnce[B]): List[B]

//flatten|faltMap|foreach
val words = Array("hello tom hello","aa bb")
val splitWords: Array[Array[String]] = words.map(wd => wd.split(" "))//按空格切分字符串,现在是两层嵌套数组
val flattenWords = splitWords.flatten // 将内层的列表去掉
val res = Array[String] = words.flatMap(wd => wd.split(" ")) //flatMap是map和Flatten的结合

列表操作

sorted/sortBy/sortWith

sorted 是直接按升序排def sorted[B >: (String, Int)](implicit ord: scala.math.Ordering[B]): List[(String, Int)]

lst.sorted //List[(String, Int)] = List((a,3), (b,1), (c,2))

sortBy 根据具体元素指定排序方式 def sortBy[B](f: ((String, Int)) => B)(implicit ord: scala.math.Ordering[B]): List[(String, Int)]

lst.sortBy(x=>x._2) //List[(String, Int)] = List((b,1), (c,2), (a,3))  按元素第2个位置的升序排列
lst.sortBy(x=> - x._2) //List[(String, Int)] = List((a,3), (c,2), (b,1))  按元素的第2个位置降序排列

sortWith
sortWith 传入比较器(x y)分别代表了前后相连的元素def sortWith(lt: ((String, Int), (String, Int)) => Boolean): List[(String, Int)]

lst.sortWith((x,y) => x._2 > y._2) //List[(String, Int)] = List((a,3), (b,1), (c,2))

聚合

reduce和aggregate都是调用的fold

reduce/reduceLeft/reduceRight

传入函数,进行reduce def reduce[A1 >: Int](op: (A1, A1) => A1): A1

lst.reduce((x,y)=>x+y)

aggregate(reduceByKey spark)

defaggregate[B](z: => B)(seqop: (B, A) => B, combop: (B, B) => B): B

val lst = List(1,2,3)
lst.aggregate(0)(_+_,_+_)

fold/foldLeft/foldRight

val lst = List(1,2,3)

lst.fold(0)(_ - _) //Int = -6  从左边减(放在减号左边) ((0-1)-2)-3
lst.foldLeft(0)(_ - _) // 和fold等价
lst.foldRight(0)(_ - _) //Int = 2 从右边减(放在减号右边) (1-(2-(3-0)))
val lst = List(("a",3),("b",1),("c",2))

count

def count(p: ((String, Int)) => Boolean): Int
参数是代表x,代表的是列表中的每一个值

val lst = List(("a",3),("b",1),("c",2))
lst.count(x=>x._2>1)  # 计算元素中第二个位置大于1的个数

union

将两个list和成一个

intersect

交集

diff

差集

zip

mkString

slice

参考文献:
https://www.scala-lang.org/api/current/scala/collection/immutable/List.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值