当前位置:   article > 正文

scala学习--集合中List的常用方法及示例_scala 获取list 值

scala 获取list 值

文章目录

集合的可变与不可变

scala中集合默认是不可变的,在包scala.collection.immutable下,
任何对集合的修改,添加,删除操作都是生成一个新的集合返回
如果想对集合进行修改,添加,删除操作,需要使用scala.collection.mutable
包下得集合Buffer

scala> var list=List(1,2,3)
list: List[Int] = List(1, 2, 3)

scala> list(1)=10
<console>:17: error: value update is not a member of List[Int]
       list(1)=10
       ^

scala> import scala.collection.mutable._
import scala.collection.mutable._

scala> var list2=ListBuffer(1,2,3)
list2: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)

scala> list2(1)=10

scala> list2
res13: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 10, 3)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

List

List:元素以线性方式存储,集合中可以存放重复对象

定义方法

scala> var a = List(1,2,3)
a: List[Int] = List(1, 2, 3)

scala> var a = List[Int](1,2,3)
a: List[Int] = List(1, 2, 3)

scala> var a = List[String]("a","b","c")
a: List[String] = List(a, b, c)

scala> var a:List[Int]=List(1,2,3)
a: List[Int] = List(1, 2, 3)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

List方法

添加元素+:/:+/::

+:/:+  添加元素,List接在:一边
+:
scala> a
res26: List[Int] = List(1, 2, 3)

scala> 0+:a
res27: List[Int] = List(0, 1, 2, 3)

:+
scala> a
res26: List[Int] = List(1, 2, 3)

scala> a:+4
res28: List[Int] = List(1, 2, 3, 4)

::
添加元素(元素在左,List在右)添加在List开头
def ::(x: A): List[A]
scala> a::6
<console>:20: error: value :: is not a member of Int
       a::6
        ^

scala> 6::a
res30: List[Int] = List(6, 1, 2, 3)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

addString 集合转成字符串

def addString(b: StringBuilder): StringBuilder
def addString(b: StringBuilder, sep: String): StringBuilder

scala> a
res36: List[String] = List(a, b, c)

scala> var b = new StringBuilder
b: StringBuilder =

scala> a.addString(b)
res37: StringBuilder = abc
//不初始化就会在后面追加
scala> var b = new StringBuilder
b: StringBuilder =

scala> a.addString(b,",")
res38: StringBuilder = a,b,c

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

::: 集合合并

def :::(prefix: List[A]): List[A]
scala> a
res39: List[String] = List(a, b, c)

scala> var b = List(1,2,3)
b: List[Int] = List(1, 2, 3)

scala> a:::b
res40: List[Any] = List(a, b, c, 1, 2, 3)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

apply 取值

def apply(n: Int): A

scala> a
res41: List[String] = List(a, b, c)

scala> a.apply(1)
res42: String = b
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

contains检测列表中是否包含指定的元素

def contains(elem: Any): Boolean

scala> a
res43: List[String] = List(a, b, c)

scala> a.contains("a")
res44: Boolean = true

scala> a.contains("e")
res45: Boolean = false
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

copyToArray将列表的元素复制到数组中

def copyToArray(xs: Array[A], start: Int, len: Int): Unit

scala> var b=new Array[String](3)
b: Array[String] = Array(null, null, null)

scala> a
res46: List[String] = List(a, b, c)

scala> b
res47: Array[String] = Array(null, null, null)

scala> a.copyToArray(b)

scala> b
res49: Array[String] = Array(a, b, c)

方法后面可以跟数组的下标起始位置(左闭右开)
scala> a
res59: List[String] = List(a, b, c, d, e)

scala> var b=new Array[String](3)
b: Array[String] = Array(null, null, null)

scala> a.copyToArray(b,0,3)

scala> b
res61: Array[String] = Array(a, b, c)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

distinct去除列表的重复元素,并返回新列表

def distinct: List[A]


scala> var a=List(1,1,2,2,3,3)
a: List[Int] = List(1, 1, 2, 2, 3, 3)

scala> a.distinct
res62: List[Int] = List(1, 2, 3)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

drop丢弃前n个元素,并返回新列表

def drop(n: Int): List[A]

scala> var a=List(1,1,2,2,3,3)
a: List[Int] = List(1, 1, 2, 2, 3, 3)

scala> a.drop(2)
res69: List[Int] = List(2, 2, 3, 3)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

dropRight丢弃最后n个元素,并返回新列表

def dropRight(n: Int): List[A]

scala> a
res70: List[Int] = List(1, 1, 2, 2, 3, 3)

scala> a.dropRight(2)
res71: List[Int] = List(1, 1, 2, 2)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

dropWhile从左向右丢弃元素,直到条件p不成立(一旦条件不成立就结束匹配)

def dropWhile(p: (A) => Boolean): List[A]

scala> a
res89: List[Int] = List(1, 1, 2, 2, 3, 3)

scala> a.dropWhile(_==1)
res91: List[Int] = List(2, 2, 3, 3)

scala> a.dropWhile(_>2)
res92: List[Int] = List(1, 1, 2, 2, 3, 3)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

equals判断是否相等

def equals(that: Any): Boolean

scala> a
res100: List[String] = List(abc)

scala> b
res101: Array[String] = Array(a, b, c)

scala> a.equals(b)
res102: Boolean = false
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

exists判断列表中指定条件的元素是否存在

def exists(p: (A) => Boolean): Boolean

scala> a
res111: List[Int] = List(1, 2, 3)

scala> a.exists(x=>x==2)
res112: Boolean = true

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

filter输出符号指定条件的所有元素


def filter(p: (A) => Boolean): List[A]

scala> var a=List("aaa","aa","a","bbb")
a: List[String] = List(aaa, aa, a, bbb)

scala> a.filter(x=>x.size==3)
res113: List[String] = List(aaa, bbb)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

forall检测所有元素

def forall(p: (A) => Boolean): Boolean

scala> var a=List("aaa","aa","a")
a: List[String] = List(aaa, aa, a)

scala> a.forall(a=>a.startsWith("a"))
res115: Boolean = true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

foreach将函数应用到列表的所有元素

def foreach(f: (A) => Unit): Unit

scala> a
res134: List[String] = List(ccc, bb, b, bbb)

scala> a.foreach(println)
ccc
bb
b
bbb
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

head获取列表的第一个元素

def head: A

scala> a
res14: List[Int] = List(1, 2, 3)

scala> a.head
res15: Int = 1

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

indexOf 从指定位置 from 开始查找元素第一次出现的位置

def indexOf(elem: A, from: Int): Int

scala> var a=List(1,2,3)
a: List[Int] = List(1, 2, 3)

scala> a.indexOf(2)
res141: Int = 1

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

init 返回所有元素,除了最后一个

def init: List[A]

scala> a
res144: List[Int] = List(1, 2, 3)

scala> a.init
res145: List[Int] = List(1, 2)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

tail 返回所有元素,除了第一个

def tail: List[A]

scala> a
res144: List[Int] = List(1, 2, 3)

scala> a.tail
res17: List[Int] = List(2, 3)	

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

intersect 计算多个集合的交集

def intersect(that: Seq[A]): List[A]

scala> a
res146: List[Int] = List(1, 2, 3)

scala> var b = List(3,4,5)
b: List[Int] = List(3, 4, 5)

scala> a.intersect(b)
res147: List[Int] = List(3)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

isEmpty 检测列表是否为空

def isEmpty: Boolean

scala> a
res146: List[Int] = List(1, 2, 3)

scala> a.isEmpty
res18: Boolean = false

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

iterator 创建一个新的迭代器来迭代元素

def iterator: Iterator[A]

scala> a
res148: List[Int] = List(1, 2, 3)

scala> a.iterator
res149: Iterator[Int] = non-empty iterator

scala> var b=a.iterator
b: Iterator[Int] = non-empty iterator

scala> println(b.next())
1

scala> println(b.next())
2

scala> println(b.next())
3
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

last 返回最后一个元素

def last: A

scala> a
res153: List[Int] = List(1, 2, 3)

scala> a.last
res155: Int = 3

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

lastIndexOf 在指定的位置 end 开始查找元素最后出现的位置

def lastIndexOf(elem: A, end: Int): Int

scala> var a=List(1,2,3,1,3,4,1,5,1)
a: List[Int] = List(1, 2, 3, 1, 3, 4, 1, 5, 1)

scala> a.lastIndexOf(1)
res157: Int = 8

scala> a.lastIndexOf(1,5)
res158: Int = 3

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

length/size 返回列表长度

def length: Int

scala> a
res159: List[Int] = List(1, 2, 3, 1, 3, 4, 1, 5, 1)

scala> a.length
res160: Int = 9

scala> a.size
res161: Int = 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

map 通过给定的方法将所有元素重新计算

def map[B](f: (A) => B): List[B]

scala> a
res162: List[Int] = List(1, 2, 3, 1, 3, 4, 1, 5, 1)

scala> a.map(x=>x+2)
res163: List[Int] = List(3, 4, 5, 3, 5, 6, 3, 7, 3)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

max/min 查找最大/小元素

def max/min: A

scala> a
res165: List[Int] = List(1, 2, 3, 1, 3, 4, 1, 5, 1)

scala> a.max
res166: Int = 5

scala> a.min
res167: Int = 1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

mkString 列表所有元素作为字符串显示、使用分隔符将列表所有元素作为字符串显示

def mkString: String
def mkString(sep: String): String

scala> a
res168: List[Int] = List(1, 2, 3, 1, 3, 4, 1, 5, 1)

scala> a.mkString
res169: String = 123134151

scala> a.mkString(" ")
res170: String = 1 2 3 1 3 4 1 5 1

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

reverse列表反转

def reverse: List[A]

scala> var a = List(1,2,3,4)
a: List[Int] = List(1, 2, 3, 4)

scala> a.reverse
res171: List[Int] = List(4, 3, 2, 1)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

sorted 列表排序

def sorted[B >: A]: List[A]


scala> var a=List(1,2,3,1,2,3,5,4,2,7,2)
a: List[Int] = List(1, 2, 3, 1, 2, 3, 5, 4, 2, 7, 2)

scala> a.sorted
res172: List[Int] = List(1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 7)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

sum 计算集合元素之和

def sum: A

scala> a
res174: List[Int] = List(1, 2, 3, 1, 2, 3, 5, 4, 2, 7, 2)

scala> a.sum
res175: Int = 32
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

take 提取列表的前n个元素

def take(n: Int): List[A]

scala> a
res176: List[Int] = List(1, 2, 3, 1, 2, 3, 5, 4, 2, 7, 2)

scala> a.take(5)
res177: List[Int] = List(1, 2, 3, 1, 2)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

takeRight 提取列表的后n个元素

def takeRight(n: Int): List[A]

scala> a
res176: List[Int] = List(1, 2, 3, 1, 2, 3, 5, 4, 2, 7, 2)

scala> a.takeRight(5)
res178: List[Int] = List(5, 4, 2, 7, 2)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

toArray 列表转换为数组

def toArray: Array[A]

scala> a
res179: List[Int] = List(1, 2, 3, 1, 2, 3, 5, 4, 2, 7, 2)

scala> a.toArray
res180: Array[Int] = Array(1, 2, 3, 1, 2, 3, 5, 4, 2, 7, 2)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

toBuffer 返回缓冲区,包含了列表的所有元素

def toBuffer[B >: A]: Buffer[B]

scala> a.toBuffer
res181: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 2, 3, 1, 2, 3, 5, 4, 2, 7, 2)
  • 1
  • 2
  • 3
  • 4

toMap List 转换为 Map

def toMap[T, U]: Map[T, U]

scala> var a = List("cat","bird","tiger")
a: List[String] = List(cat, bird, tiger)

scala> a.map(a=>(a.size,a))
res184: List[(Int, String)] = List((3,cat), (4,bird), (5,tiger))

scala> a.map(a=>(a.size,a)).toMap
res185: scala.collection.immutable.Map[Int,String] = Map(3 -> cat, 4 -> bird, 5 -> tiger)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

toSet List 转换为 Set

def toSet[B >: A]: Set[B]

scala> var a = List("cat","bird","tiger")
a: List[String] = List(cat, bird, tiger)

scala> a.map(a=>(a.size,a))
res184: List[(Int, String)] = List((3,cat), (4,bird), (5,tiger))

scala> a.map(a=>(a.size,a)).toSet
res186: scala.collection.immutable.Set[(Int, String)] = Set((3,cat), (4,bird), (5,tiger))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

toString 列表转换为字符串

def toString(): String

scala> var a=List(1,2,3)
a: List[Int] = List(1, 2, 3)

scala> a.toString
res187: String = List(1, 2, 3)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/189440
推荐阅读
相关标签
  

闽ICP备14008679号