文章目录
- ++ ++:
- +: :+
- /: :\
- addString(b:StringBuilder):StringBuilder
- addString(b:StringBuilder,sep String):StringBuilder
- aggregate[B](z: => B)(seqop: (B, Int) => B,combop: (B , B) =>B):B
- andThen(override def andThen[C](k: Int => C): PartialFunction[Int,C])
- apply(i:Int):T
- applyOrElse:(def applyOrElse[A1 <: Int, B1 >: Int](x: A1,default: A1 => B1): B1)
- array:(def array: Array[Int])
- canEqual(that:Any):Boolean
- clone():Array[T]
- collect(pf:PartialFunction[A,B]):Array[B]
- collectFirst(pf:PartialFunction[T,B])
- combinations(n:Int):collection.Iterator[Array[T]]
- companion:override def companion: scala.collection.generic.GenericCompanion[scala.collection.mutable.IndexedSeq]
- compose:def compose[A](g: A => Int): A => Int
- contains(b):Boolean
- containsSlice(that: GenSeq[B]):Boolean
- copyToArray(a:Array[A],start:Int):Unit、 copyToArray(a:Array[A],start:Int,len:Int)
- copyToBuffer(B:ArrayBuffer):Unit
- corresponds(that:GenSeq[B])(p:(T,B)=>Boolean):Boolean
- count(p:(T)=>Boolean):Int
- deep:def deep: IndexedSeq[Any]
- diff(that:collection.Seq[T]):Array[T]
- distinct:Array[T]
- drop(n:Int):Aray[T]
- dropRight(n:Int):Array[T]
- dropWhile(p:(T) => Boolean):Array[T]
- elemManifest: def elemManifest: ClassManifest[Int]
- elemTag:def elemTag: scala.reflect.ClassTag[Int]
- endsWith(that:GenSeq[B]):Boolean
- exists(p:(T) => Boolean):Boolean
- filter((T) => Boolean):Array[T]
- filterNot(p: (T) => Boolean): Array[T]
- find(p:(T) => Boolean):Option[T]
- flatMap(f:(A) => B):Array[B]
- flatten:Array[U]
- fold[A1 >: A](z: A1)(op:(A1,A1) => A1):A1
- foldLeft[B](z:B)(op: (B,T) => B):B
- foldRight[B](z: B)(op: (B,T) => B):B
- forall(p:(T) => Boolean):Boolean
- foreach(f:(A) => Unit):Unit
- genericBuilder:def genericBuilder[B]: scala.collection.mutable.Builder[B,scala.collection.mutable.IndexedSeq[B]]
- groupBy(f:(T) => K):Map[K,Array[T]]
- grouped(size:Int):collection.Iterator[Array[T]]
- hasDefiniteSize:Boolean
- head:T
- headOption:Option
- indexOf(elem:T):Int
- indexOf(elem:T,from:Int):Int
- indexOfSlice(that:GenSeq[B]):Int
- indexOfSlice(that:GenSeq[B],from:Int):Int
- indexWhere(p:(T) => Boolean):Int
- indexWhere(p:(T) => Boolean,from: Int):Int
- indices
- init
- inits
- intersect(that:collection.Seq[T]):Array[T]
- isDefinedAt(idx: Int): Boolean
- isEmpty:Boolean
- isTraversableAgain:Boolean
- iterator:collection.Iterator[T]
- last: T
- lastIndexOf(elem: T):Int
- lastIndexOfSlice[B>:A](that: GenSeq[B]):Int
- lastIndexOfSlice[B>:A](that: GenSeq[B],end: Int):Int
- lastIndexWhere(p: (T) => Boolean):Int
- lastOption:Option[T]
- length: Int
- lengthCompare(len: lnt):lnt
- lift:def lift: Int => Option[Int]
- map(f:(A) => B):Array[B]
- 小技巧:用scala函数实现wordcount
- map和foreach()的区别
- max: A
- maxBy[B](f: (A) => B): A
- min: A
- minBy[B](f: (A) => B):A
- mkString:String
- nonEmpty:Boolean
- orElse: def orElse[A1 <: Int, B1 >: Int](that: PartialFunction[A1,B1]): PartialFunction[A1,B1]
- padTo(len:lnt,elem:A):Array[A]
- par:ParArray[T]
- partition(p: (T) => Boolean): (Array[T],Array[T])
- patch(from: Int,that: GenSeq[A], replaced: Int): Array[A]
- permutations:collection.Iterator[Array[T]]
- prefixLength(p: (T) => Boolean):Int
- product:A
- reduce(op: (A1,A1) => A1):A1
- reduceLeft(op:(B,T) => B):B
- reduceLeftOption(op: (B,T) => B):Option[B]
- reduceRight(op: (B,T) => B): B
- reduceRightOption(op: (T,B) => B):Option[B]
- reverse:Array[T]
- reverselterator: collection.Iterator[T]
- reverseMap(f: (A) => B):Array[B]
- runWith:def runWith[U](action: Int =>U):Int => Boolean
- sameElements(that:Genlterable[A]):Boolean
- scan(z: B)(op: (B,B) => B)(implicit cbf: CanBuildFrom[Array[T],B,That]):That
- scanLeft(z: B)(op: (B,T) => B)(implicit bf: CanBuildFrom[Array[T],B,That]):That
- scanRight(z: B)(op: (T,B) => B)(implicit bf: CanBuildFrom[Array[T],B,That]):That
- segmentLength(p: (T) => Boolean,from:Int):Int
- seq: collection.mutable.IndexedSeq[T]
- size:Int
- slice(from: Int, until: Int): Array[T]
- sliding(size: Int): collection.Iterator[Array[T]]
- sliding(size:Int,step: Int):Iterator[Array[Int]]
- sortBy(f:(T) => B)(implicit ord: math.Ordering[B]):Array[T]
- sortWith()(It: (Int,Int) => Boolean): Array[Int]
- sorted(implicit ord:math.Ordering[B]):Array[T]
- span(p: (T) => Boolean):(Array[T],Array[T])
- splitAt(n : Int): (Array[T],Array[T])
- startsWith(that: GenSeq[B]): Boolean
- startsWith(that: GenSeq[B],offset: Int): Boolean
- stringPrefix:String
- sum:A
- take (n:Int): Array[T]
- tail:Array[T]
- tails:collection.Iterator[Array[T]]
- withFilter:def withFilter(p: Int => Boolean): scala.collection.generic.FilterMonadic[Int,Array[Int]]
- take(n:Int):Array[T]
- takeRight(n: Int): Array[T]
- takeWhile(p: (T) => Boolean): Array[T]
- to:
- toArray:Array[A]
- toBuffer:Buffer[A]
- toIndexedSeq:collection.immutable.IndexedSeq[T]
- tolterable:collection.Iterable[T]
- tolterator:collection.Iterator[T]
- toList:List[T]
- toMap:Map[T,U]
- toSeq: collection.Seq[T]
- toSet: Set[B > :A]:Set[B]
- toStream: collection.immutable.Stream[T]
- toTraversable
- toVector:Vector[T]
- transform
- transpose(implicit asArray: (T) => Array[U]): Array[Array[U]]
- union(that:collection.Seq[T]):Array[T]
- unzip(implicit asPair: (T) => (T1,T2),ct1:ClassTag[T1],ct2:ClassTag[T2]):(Array[T1],Array[T2])
- unzip3(implicit asTriple: (T) => (T1,T2,T3),ct1:ClassTag[T1],ct2:ClassTag[T2],ct3:ClassTag[T3]):(Array[T1],Array[T2],Array[T3])
- update(i: Int,x: T):Unit
- updated(index: Int, elem: A): Array[A]
- view(from: Int, until: Int):IndexedSeqView[T,Array[T]]
- withFilter(p: (T) => Boolean):FilterMonadic[T,Array[T]]
- zip(that: GenIterable[B]): Array[(A,B)]
- zipAll(that: collection.Iterable[B],thisElem: A, thatElem: B):Array[(A,B)]
- zipWithIndex
++ ++:
++和++:都是求两个集合的并集,不同的是:
++ 看左边集合的类型是什么类型,那么返回的集合就是什么类型
++: 看右边集合的类型是什么 类型,那么返回的集合就是什么类型
scala> val a = List(100,200,300)
a: List[Int] = List(100, 200, 300)
scala> val b = Array(6,7,8,9,10)
b: Array[Int] = Array(6, 7, 8, 9, 10)
scala> a++b
//返回的是List类型的集合
res21: List[Int] = List(100, 200, 300, 6, 7, 8, 9, 10)
//返回的是Array类型的集合
scala> a++:b
res22: Array[Int] = Array(100, 200, 300, 6, 7, 8, 9, 10)
+: :+
+:和:+都是在一个集合中添加元素,不同的是:
+: 是数字在+号前面,集合在:后面
:+ 是数字在+后面,集合在:前面
可以这么理解:+号跟的是数字,:号跟的是集合
scala> 1000+: a
//结果为
res23: List[Int] = List(1000, 100, 200, 300)
scala> a:+500
//结果为
res24: List[Int] = List(100, 200, 300, 500)
/: :\
对数组中数据进行迭代操作
/: 对数组从左向右遍历,进行相同的迭代操作 floadLeft
:\ 对数组从右向左遍历,进行相同的迭代操作floadRight
是二叉树迭代操作,如下:
scala> val c = Array(1,2,3,4,5)
c: Array[Int] = Array(1, 2, 3, 4, 5)
scala> (10/:c)(_+_)
//结果(((((10+1)+2)+3)+4)+5)
//从左向右进行两两相加
res25: Int = 25
scala> (c:\20)(_+_)
//结果(1+(2+(3+(4+(5+20)))))
//从右向左进行两两相加
res26: Int = 35
addString(b:StringBuilder):StringBuilder
将数组中的元素逐个添加到StringBuilder中
scala> val c = Array(1,2,3,4,5)
c: Array[Int] = Array(1, 2, 3, 4, 5)
//定义一个StringBuilder
scala> val g = new StringBuilder
g: StringBuilder =
//将数组添加到g中
scala> c.addString(g)
//结果
res27: StringBuilder = 12345
addString(b:StringBuilder,sep String):StringBuilder
将数组添加到StringBuilder中,每个元素用sep分隔符分开
scala> c.addString(g,"|")
res28: StringBuilder = 123451|2|3|4|5
aggregate[B](z: => B)(seqop: (B, Int) => B,combop: (B , B) =>B):B
**聚合计算,aggregate是柯里化方法,参数是两个方法,为了方便理解,我们把aggregate的两个参数,分别封装成两个方法,并把计算过程打印出来。
def main(args: Array[String]) {
val a = List(1,2,3,4)
val c = a.par.aggregate(5)(seqno,combine)
println("c:"+c)
}
def seqno(m:Int,n:Int): Int ={
val s = "seq_exp=%d+%d"
println(s.format(m,n))
return m+n
}
def combine(m:Int,n:Int): Int ={
val s = "com_exp=%d+%d"
println(s.format(m,n))
return m+n
}
/**
seq_exp=5+3
seq_exp=5+2
seq_exp=5+4
seq_exp=5+1
com_exp=6+7
com_exp=8+9
com_exp=13+17
c:30
*/
andThen(override def andThen[C](k: Int => C): PartialFunction[Int,C])
先执行一个方法,执行完的结果作为另一个方法的入参
val funs = new Function1[Int,Int] {
def apply(x:Int) = {
x + 1
}
}
//使用
println(funs.apply(5)) // 6
val funs = new Function1[Int,Int] {
def apply(x:Int) = {
println("第一步:"+x)
x + 1
}
}
val succ = (x: Int) => {
println("第二步:"+x)
x + 3
}
println(succ.andThen(funs).apply(5))
/**
第二步:5
第一步:8
*/
apply(i:Int):T
获得指定索引处的元素
scala> val e = List(1,2,3,4)
e: List[Int] = List(1, 2, 3, 4)
//结果,等同于e(2)
scala> e.apply(2)
res29: Int = 3
applyOrElse:(def applyOrElse[A1 <: Int, B1 >: Int](x: A1,default: A1 => B1): B1)
接收2个参数,第一个是调用的参数,第二个是个回调函数。如果第一个调用的参数匹配,返回匹配的值,否则调用回调函数。(回调函数的输入类型需与调用参数类型一致)
val pf1:PartialFunction[Int,String] = { //定义一个偏函数,输入 1 返回 "one"
case i if i == 1 => "One"
}
pf1.applyOrElse(1,{num:Int=>"two"}) //输入1匹配到"one"
pf1.applyOrElse(2,{num:Int=>"two"}) //输入2匹配失败则触发回调函数,返回 "Two" 字符串
pf1.applyOrElse(3,{num:Int=>"two"}) //输入3匹配失败则触发回调函数,返回 "Two" 字符串
array:(def array: Array[Int])
Array是scala中数组是用来存储同类型的元素结构,数组中本身size不可变,但是数组中元素可变,可重新赋值
val arr = Array(1, 2, 3, 4) // 声明一个数组对象
val first = arr(0) // 读取第一个元素
arr(3) = 100 // 替换第四个元素为 100
val newarr = arr.map(_ * 2) // 所有元素乘 2
println(newarr.mkString(",")) // 打印数组,结果为:2,4,6,200
canEqual(that:Any):Boolean
判断两个对象是否可以进行比较
clone():Array[T]
创建一个副本,只是值相等,地址不相等
scala> val c = Array(1,2,3,4,5)
c: Array[Int] = Array(1, 2, 3, 4, 5)
scala> val f = c//f指向c
f: Array[Int] = Array(1, 2, 3, 4, 5)
scala> f(3) = 100
//发现f中索引为3的元素改变了,c中相同索引的元素也改变了,因为他们指向了相同地址
scala> c
res33: Array[Int] = Array(1, 2, 3, 100, 5)
scala> f
res34: Array[Int] = Array(1, 2, 3, 100, 5)
//用clone()这个方法只是值相等,clone是浅拷贝
scala> val g = c.clone()
g: Array[Int] = Array(1, 2, 3, 100, 5)
scala> g(4) = 4
scala> g
res36: Array[Int] = Array(1, 2, 3, 100, 4)
scala> c
res37: Array[Int] = Array(1, 2, 3, 100, 5)
collect(pf:PartialFunction[A,B]):Array[B]
通过执行一个偏函数,得到一个新数组
//定义一个将a转成A的偏函数
scala> var h = Array('a','b','c','d')
h: Array[Char] = Array(a, b, c, d)
//第一个Char是传进来的参数类型,第二个Char是返回参数的类型
scala> val test1:PartialFunction[Char,Char] = {
| case 'a' => 'A'
| case x => x
| }
test1: PartialFunction[Char,Char] = <function1>
scala> h.collect(test1)
//结果为
res38: Array[Char] = Array(A, b, c, d)
collectFirst(pf:PartialFunction[T,B])
在序列中查找第一个符合偏函数定义的元素,并执行偏函数计算
scala> val pfTest01:PartialFunction[Any,Int] = {
| case x:Int => x*100
| }
pfTest01: PartialFunction[Any,Int] = <function1>
scala> val y =Array('t',2,'a',"b")
y: Array[Any] = Array(t, 2, a, b)
scala> y.collectFirst(pfTest01)
//结果
res44: Option[Int] = Some(200)
scala> y.collectFirst(pfTest01).get
//结果
res45: Int = 200
另一种写法
scala> y.collectFirst({case x:Int => x*100})
res46: Option[Int] = Some(200)
combinations(n:Int):collection.Iterator[Array[T]]
combinations表示组合,这个排列组合会选出所有包含字符不一样的组合,但不考虑顺序,对于 “abc” 、“cba” ,视为相同组合,参数n表示序列长度,就是几个字符为一组
scala> val y = Array('t',2,'a',"b")
y: Array[Any] = Array(t, 2, a, b)
scala> val t = y.combinations(3)
//返回值是一个迭代器
t: Iterator[Array[Any]] = non-empty iterator
scala> t.foreach(x=>println(x.mkString(",")))
//结果
t,2,a
t,2,b
t,a,b
2,a,b
companion:override def companion: scala.collection.generic.GenericCompanion[scala.collection.mutable.IndexedSeq]
compose:def compose[A](g: A => Int): A => Int
表示方法的连续调用,与 andThen 相反,不同的是需满足第二个函数的返回值类型是第一个函数的输入值类型
def f(a:Int) = {
println("2*"+a)
2*a
}
def g(b:Int) = {
println("3*"+b)
3*b
}
def result1 = f _ andThen g _
println(result1(1))
/*
2*1
2*3
6
*/
def result2 = f _ compose g _
println(result2(1))
/*
3*1
3*2
6
*/
contains(b):Boolean
判断数组中是否包含指定对象
scala> val y = Array('t',2,'a',"b")
y: Array[Any] = Array(t, 2, a, b)
scala> y.contains("b")
//结果为
res48: Boolean = true
containsSlice(that: GenSeq[B]):Boolean
判断当前序列中是否包含另一个序列
scala> val n = Array("a","b")
n: Array[String] = Array(a, b)
scala> val y = Array('t',2,"a","b")
y: Array[Any] = Array(t, 2, a, b)
scala> y.containsSlice(n)
//结果为
res49: Boolean = true
copyToArray(a:Array[A],start:Int):Unit、 copyToArray(a:Array[A],start:Int,len:Int)
将当前数组复制到另一个数组中,从start位置开始复制
scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)
//定义一个长度为10的数组
scala> val b:Array[Int] = new Array(10)
b: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
//把数组a复制给b,从索引为3开始
scala> a.copyToArray(b,3)
scala> b
//结果
res52: Array[Int] = Array(0, 0, 0, 1, 2, 3, 0, 0, 0, 0)
将当前数组复制到另一个数组中,从start位置开始复制,长度为len
scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)
//定义一个长度为10的数组
scala> val b:Array[Int] = new Array(10)
b: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
//把数组a复制给b,从索引为3开始,长度为2
scala> a.copyToArray(b,3,2)
scala> b
//结果
res56: Array[Int] = Array(0, 0, 0, 1, 2, 0, 0, 0, 0, 0)
copyToBuffer(B:ArrayBuffer):Unit
将数组中的元素复制到Buffer中
ArrayBuffer相当于java中的ArrayList,他是可变的,而Array不可变
需要导包 import scala.collection.mutable.ArrayBuffer
//导包
scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer
//创建一个ArrayBuffer
scala> val b:ArrayBuffer[Int] = new ArrayBuffer()
b: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
scala> val a = Array(1,2,3,100,5)
a: Array[Int] = Array(1, 2, 3, 100, 5)
scala> c.copyToBuffer(b)
scala> b
//结果为
res58: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 100, 5)
corresponds(that:GenSeq[B])(p:(T,B)=>Boolean):Boolean
判断两个序列的长度以及对应位置元素是否符合某个条件。如果两个序列具有相同的元素数量并且p(x,y)=true,则返回true
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> val b = Array(1,2,3,4,5)
b: Array[Int] = Array(1, 2, 3, 4, 5)
//结果
scala> a.corresponds(b)(_==_)
res60: Boolean = true
count(p:(T)=>Boolean):Int
统计符合条件的元素个数
下面判断数组元素中能被2整除的元素
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.count({_%2==0})
res61: Int = 2
【注】 如果变量在=>之后只使用一次,那么x=>x就可以用_代替
deep:def deep: IndexedSeq[Any]
diff(that:collection.Seq[T]):Array[T]
计算当前数组与另一个数组的差集,即将当前数组中没有在另一个数组中出现的元素返回
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> val b = Array(1,2,9,7,5)
b: Array[Int] = Array(1, 2, 9, 7, 5)
scala> a.diff(b)
res62: Array[Int] = Array(3, 4)
distinct:Array[T]
取出当前集合中重复的元素,只保留一个不重复的元素
scala> val a = Array(1,2,3,4,5,1,5)
a: Array[Int] = Array(1, 2, 3, 4, 5, 1, 5)
scala> a.distinct
res63: Array[Int] = Array(1, 2, 3, 4, 5)
drop(n:Int):Aray[T]
删除当前数组中的前n个元素,返回一个新数组
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.drop(2)
res64: Array[Int] = Array(3, 4, 5)
dropRight(n:Int):Array[T]
去掉当前数组尾部的n个元素
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.dropRight(3)
res65: Array[Int] = Array(1, 2)
dropWhile(p:(T) => Boolean):Array[T]
从第一个元素开始,去除当前数组中符合条件的元素,直到遇到第一个不满足条件的元素(即使后面还有符合条件的元素),返回剩余的数组,如果整个数组都满足条件,那么就返回整个数组
scala> val a = Array(1,2,3,4,3)
a: Array[Int] = Array(1, 2, 3, 4, 3)
//返回第一个不满足的元素及后面的所有元素
scala> a.dropWhile(_<3)
res66: Array[Int] = Array(3, 4, 3)
elemManifest: def elemManifest: ClassManifest[Int]
elemTag:def elemTag: scala.reflect.ClassTag[Int]
endsWith(that:GenSeq[B]):Boolean
判断当前序列是否以某个序列结尾
是整个序列,而不是序列的末尾元素
scala> val a = Array("a","b","c","d")
a: Array[String] = Array(a, b, c, d)
scala> val b = Array("c","d")
b: Array[String] = Array(c, d)
scala> a.endsWith(b)
res67: Boolean = true
exists(p:(T) => Boolean):Boolean
判断当前数组是否包含符合条件的元素
参数返回的是boolean类型
scala> val a = Array("a","b","c","d")
a: Array[String] = Array(a, b, c, d)
scala> a.exists({_=="c"})
res68: Boolean = true
filter((T) => Boolean):Array[T]
取得当前数组中符合条件的元素,组成新的数组返回
scala> val a = Array(1,2,3,4,3)
a: Array[Int] = Array(1, 2, 3, 4, 3)
scala> a.filter{_>2}
res69: Array[Int] = Array(3, 4, 3)
filterNot(p: (T) => Boolean): Array[T]
与上面的 filter 作用相反
find(p:(T) => Boolean):Option[T]
查找第一个符合条件的元素,返回存在Some,不存在None
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.find{_>3}
res70: Option[Int] = Some(4)
flatMap(f:(A) => B):Array[B]
是map的拓展函数:用于处理集合套集合,也就是一对一或一对多,最后返回一个聚合,这个集合就是把里面每个集合都扁平化了,大多数情况下用于降维
scala> val a = Array("hadoop easy","scala hard","java easy","hello world")
a: Array[String] = Array(hadoop easy, scala hard, java easy, hello world)
scala> a.flatMap(x=>x.split(" "))
res72: Array[String] = Array(hadoop, easy, scala, hard, java, easy, hello, world)
flatten:Array[U]
扁平化,将集合中的多个数组组合在一起,形成一个新数组返回
scala> val b = Array(Array(1,2,3,4,5),Array(1,2,3),Array(1,2,3))
b: Array[Array[Int]] = Array(Array(1, 2, 3, 4, 5), Array(1, 2, 3), Array(1, 2, 3))
scala> b.flatten
res71: Array[Int] = Array(1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 3)
fold[A1 >: A](z: A1)(op:(A1,A1) => A1):A1
对序列中的每个元素进行二元运算,和aggregate有类似的语义,但执行过程有所不同,我们来对比一下他们的执行过程
因为aggregate需要两个处理方法,所以我们定义一个combine方法
def seqno(m:Int,n:Int): Int ={
val s = "seq_exp=%d+%d"
println(s.format(m,n))
return m+n
}
def combine(m:Int,n:Int): Int ={
val s = "com_exp=%d+%d"
println(s.format(m,n))
return m+n
}
val a = Array(1, 2, 3,4)
val b = a.fold(5)(seqno)
/** 运算过程
seq_exp=5+1
seq_exp=6+2
seq_exp=8+3
seq_exp=11+4
*/
val c = a.par.aggregate(5)(seqno,combine)
/** 运算过程
seq_exp=5+1
seq_exp=5+4
seq_exp=5+3
com_exp=8+9
seq_exp=5+2
com_exp=6+7
com_exp=13+17
*/
看上面的运算过程发现,fold中,seqno是把初始值顺序和每个元素相加,把得到的结果与下一个元素进行运算
而aggregate中,seqno是把初始值与每个元素相加,但结果不参与下一步运算,而是放到另一个序列中,由第二个combine进行处理
foldLeftB(op: (B,T) => B):B
从左到右计算,简写方式:def /:[B](z: B)(op :(B, T) => B): B
def seqno(m:Int,n:Int): Int ={
val s = "seq_exp=%d+%d"
println(s.format(m,n))
return m+n
}
val a = Array(1, 2, 3,4)
val b = a.foldLeft(5)(seqno)
/** 运算过程
seq_exp=5+1
seq_exp=6+2
seq_exp=8+3
seq_exp=11+4
*/
/**
简写 (5 /: a)(_+_)
*/
foldRight[B](z: B)(op: (B,T) => B):B
def seqno(m:Int,n:Int): Int ={
val s = "seq_exp=%d+%d"
println(s.format(m,n))
return m+n
}
val a = Array(1, 2, 3,4)
val b = a.foldRight(5)(seqno)
/** 运算过程
seq_exp=4+5
seq_exp=3+9
seq_exp=2+12
seq_exp=1+14
*/
/**
简写 (a :\ 5)(_+_)
*/
forall(p:(T) => Boolean):Boolean
检测序列中的元素是否都满足p,如果序列为空则返回true
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> a.forall(_<5)
res74: Boolean = true
foreach(f:(A) => Unit):Unit
遍历数列中的元素,进行f操作
【注意】foreach是没有返回值的,只能在函数里面写输出语句来显示
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.foreach(x => println(x*10))
10
20
30
40
50
genericBuilder:def genericBuilder[B]: scala.collection.mutable.Builder[B,scala.collection.mutable.IndexedSeq[B]]
groupBy(f:(T) => K):Map[K,Array[T]]
按条件分组,条件由f匹配,返回值是Map类型,每个K对应一个数组
scala> val a = List(1,2,3,4,5)
a: List[Int] = List(1, 2, 3, 4, 5)
scala> a.groupBy(x=>x match {
| case x if x<3 => "small"
| case _ => "big"
| })
//结果,返回一个Map集合
res76: scala.collection.immutable.Map[String,List[Int]] = Map(small -> List(1, 2), big -> List(3, 4, 5))
grouped(size:Int):collection.Iterator[Array[T]]
按指定数量分组每组有size个元素,返回一个迭代器(迭代器用.next()方法去输出)
scala> val a = List(1,2,3,4,5)
a: List[Int] = List(1, 2, 3, 4, 5)
scala> val d = a.grouped(3)
d: Iterator[List[Int]] = non-empty iterator
//分为两组List
scala> d.toList
res77: List[List[Int]] = List(List(1, 2, 3), List(4, 5))
hasDefiniteSize:Boolean
检测序列是否存在有限的长度,对应Stream这样的流数据返回false
scala> val a = List(1,2,3,4,5)
a: List[Int] = List(1, 2, 3, 4, 5)
//结果
scala> a.hasDefiniteSize
res78: Boolean = true
//定义一个Stream
scala> val s = (1 to 100).toStream
s: scala.collection.immutable.Stream[Int] = Stream(1, ?)
scala> s.hasDefiniteSize
res79: Boolean = false
head:T
返回序列的第一个元素,如果序列为空,将引发错误
var s =(1 to 1000).toStream
s: scala.collection.immutable.Stream[Int] = Stream(1, ?)
s.head
//结果
res1: Int = 1
headOption:Option
返回序列的第一个元素的Option类型对象,如果序列为空,则返回None
scala> var a = List(1,2,3,4,5)
a: List[Int] = List(1, 2, 3, 4, 5)
scala> a.headOption
res4: Option[Int] = Some(1)
indexOf(elem:T):Int
返回元素elem在序列中第一次出现的索引
scala> var a = List(1,2,3,4,5)
a: List[Int] = List(1, 2, 3, 4, 5)
scala> a.indexOf(4)
res5: Int = 3
indexOf(elem:T,from:Int):Int
返回元素elem在序列中第一次出现的索引,指定从索引form开始查找
scala> var a = List(1,2,3,4,5)
a: List[Int] = List(1, 2, 3, 4, 5)
scala> a.indexOf(3,1)
res8: Int = 2
indexOfSlice(that:GenSeq[B]):Int
检查当前序列中是否包含序列that,并返回第一次出现该序列的索引
scala> var a = List(1,2,3,4,3,4,5)
a: List[Int] = List(1, 2, 3, 4, 3, 4, 5)
scala> a.indexOfSlice(List(3,4))
//结果
res9: Int = 2
indexOfSlice(that:GenSeq[B],from:Int):Int
检测当前序列中是否包含另一个序列that,指定从索引form开始查找,并返回第一次出现该索引的序列
scala> val a = Array(1,2,3,2,3,4)
a: Array[Int] = Array(1, 2, 3, 2, 3, 4)
scala> val b = Array(2,3)
b: Array[Int] = Array(2, 3)
scala> println(a.indexOfSlice(b,2))
3
indexWhere(p:(T) => Boolean):Int
返回当前序列中第一个满足条件p的元素的索引
scala> var a = List(1,2,3,4,3,4,5)
a: List[Int] = List(1, 2, 3, 4, 3, 4, 5)
scala> a.indexWhere(_>3)
//结果
res11: Int = 3
indexWhere(p:(T) => Boolean,from: Int):Int
返回当前序列中第一个满足条件p的元素的索引,指定从索引from开始查找
scala> var a = List(1,2,3,4,3,4,5)
a: List[Int] = List(1, 2, 3, 4, 3, 4, 5)
scala> a.indexWhere(_>3,4)
//结果
res12: Int = 5
indices
返回当前序列索引集合
scala> var a = List(1,2,3,4,3,4,5)
a: List[Int] = List(1, 2, 3, 4, 3, 4, 5)
scala> a.indices
//结果
res13: scala.collection.immutable.Range = Range(0, 1, 2, 3, 4, 5, 6)
init
返回当前序列中不包含最后一个元素的序列
scala> var a = List(1,2,3,4,3,4,5)
a: List[Int] = List(1, 2, 3, 4, 3, 4, 5)
scala> a.init
//结果
res14: List[Int] = List(1, 2, 3, 4, 3, 4)
inits
对集合中的元素进行init迭代操作,该操作的返回值中,第一个值是当前序列的副本,最后一个值为空,每一个都进行init操作,上一步的结果为下一步的操作对象
scala> var a = List(1,2,3,4,3,4,5)
a: List[Int] = List(1, 2, 3, 4, 3, 4, 5)
scala> a.inits.toList
//结果
res15: List[List[Int]] = List(List(1, 2, 3, 4, 3, 4, 5), List(1, 2, 3, 4, 3, 4), List(1, 2, 3, 4, 3), List(1, 2, 3, 4), List(1, 2, 3), List(1, 2), List(1), List())
intersect(that:collection.Seq[T]):Array[T]
取两个集合的交集
scala> var a = List(1,2,3,4,3,4,5)
a: List[Int] = List(1, 2, 3, 4, 3, 4, 5)
scala> var b = List(2,3,6,4)
b: List[Int] = List(2, 3, 6, 4)
scala> a.intersect(b)
//结果
res16: List[Int] = List(2, 3, 4)
isDefinedAt(idx: Int): Boolean
判断序列中是否存在指定索引
val a = Array(1,2,3,4,5)
println(a.isDefinedAt(1)) //true
println(a.isDefinedAt(10)) //false
isEmpty:Boolean
判断当前序列是否为空
isTraversableAgain:Boolean
判断序列是否可以反复遍历,该方法是GenTraversableOnce中的方法,对于Traversables一般返回true,对于Iterator返回false,除非被复写
iterator:collection.Iterator[T]
对序列中的每个元素产生一个iterator
val a = Array(1,2,3,4,5)
val b = a.iterator //此时就可以通过迭代器访问 b
last: T
取得序列中最后一个元素
val a = Array(1,2,3,4,5)
println(a.last) //return 5
lastIndexOf(elem: T):Int
取得序列中最后一个等于elem的元素的位置
val a = Array(1,4,2,3,4,5)
println(a.lastIndexOf(4)) //return 4
lastIndexOfSlice[B>:A](that: GenSeq[B]):Int
判断当前序列中是否包含序列that , 并返回最后一次出现该序列的位置处的索引
val a = Array(1,4,2,3,4,5,1,4)
val b = Array(1,4)
println(a.lastIndexOfSlice(b)) //return 6
lastIndexOfSlice[B>:A](that: GenSeq[B],end: Int):Int
判断当前序列中是否包含序列that,并返回最后一次出现该序列处的位置处的索引,可以指定在 end之前(包括)的元素中查找
val a = Array(1,4,2,3,4,5,1,4)
val b = Array(1,4)
println(a.lastIndexOfSlice(b,4)) //return 0
lastIndexWhere(p: (T) => Boolean):Int
返回当前序列中最后一个满足条件p的元素的索引
val a = Array(1,4,2,3,4,5,1,4)
val b = Array(1,4)
println(a.lastIndexWhere({x:Int => x<2})) //return 6
lastOption:Option[T]
返回当前序列中最后一个对象
val a = Array(1,2,3,4,5)
println(a.lastOption) //return Some(5)
length: Int
返回当前序列中元素个数
val a = Array(1,2,3,4,5)
println(a.length) // return 5
lengthCompare(len: lnt):lnt
比较序列的长度和参数len,根据二者的关系返回不同的值,比较规则
x<0 if this.length < len
x==0 if this.length == len
x>0 if this.length > len
lift:def lift: Int => Option[Int]
返回该下标的元素的Option的对象
val a = Array(1,2,3,4,5)
a.lift(2)
res39: Option[Int] = Some(3)
map(f:(A) => B):Array[B]
遍历数组中的每一个元素,并对每一项执行指定的f方法,返回一个集合
scala> val a = Array("hadoop easy","scala hard","java easy","hello world")
a: Array[String] = Array(hadoop easy, scala hard, java easy, hello world)
//使用split(" ")将里面每个字符串分割成一个数组
scala> a.map(x=>x.split(" "))
//结果
res0: Array[Array[String]] = Array(Array(hadoop, easy), Array(scala, hard), Array(java, easy), Array(hello, world))
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.map{_*2}
//结果返回一个集合
res1: Array[Int] = Array(2, 4, 6, 8, 10)
小技巧:用scala函数实现wordcount
val words = Array("hello world","hello scala","spark scala")
words.flatMap(x=>x.split(" ")).groupBy(x=>x).foreach(f=>println(f._1,f._2.length))
var a = Array("java","scala","spark","python","spark","hadoop","java","spark","scala","java","scala")
a.map((_,1)).groupBy(_._1).map(x=>(x._1,x._2.length))
//结果
scala.collection.immutable.Map[String,Int] = Map(java -> 3, hadoop -> 1, spark -> 3, scala -> 3, python -> 1)
map和foreach()的区别
map:返回一个集合
foreach:无返回值
max: A
返回序列中最大的元素
val a = Array(1,2,3,4,5)
println(a.max) //return 5
maxBy[B](f: (A) => B): A
返回序列中第一个符合条件的最大的元素
val a = Array(1,2,3,4,5)
println(a.maxBy( {x:Int => x >2 } )) //return 3
min: A
返回序列中最小的元素
val a = Array(1,2,3,4,5)
println(a.min) //return 1
minBy[B](f: (A) => B):A
返回序列中第一个不符合条件的最小的元素
val a = Array(1,2,3,4,5)
println(a.minBy( {x: Int => x<3 } ))
mkString:String
将序列中所有元素拼接成一个字符串,以sep作为元素间的分隔符
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = a.mkString(",")
b: String = 1,2,3,4
nonEmpty:Boolean
判断序列是否不为空
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = new Array[Int](0)
b: Array[Int] = Array()
scala> a.nonEmpty
res3: Boolean = true
scala> b.nonEmpty
res4: Boolean = false
orElse: def orElse[A1 <: Int, B1 >: Int](that: PartialFunction[A1,B1]): PartialFunction[A1,B1]
padTo(len:lnt,elem:A):Array[A]
填充序列,如果当前序列长度小于len,那么新产生的序列长度是len,多出几个位置填充elem,如果当前序列大于等于len,则返回当前序列
//填充一个长度为7的序列不足位补8
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = a.padTo(7,8)
b: Array[Int] = Array(1, 2, 3, 4, 8, 8, 8)
//填充的序列小于当前序列,返回当前序列
scala> val b = a.padTo(3,8)
b: Array[Int] = Array(1, 2, 3, 4)
par:ParArray[T]
返回一个并行实现,产生的并行序列不能被修改
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = a.par
b: scala.collection.parallel.mutable.ParArray[Int] = ParArray(1, 2, 3, 4)
partition(p: (T) => Boolean): (Array[T],Array[T])
按条件将序列拆分成两个数组,满足条件的放到一个数组,其余的放到第二个数组,返回的是包含这两个数组的元祖
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b =a.partition(_%2==0)
b: (Array[Int], Array[Int]) = (Array(2, 4),Array(1, 3))
patch(from: Int,that: GenSeq[A], replaced: Int): Array[A]
批量替换,从原序列的from处开始,后面的replaced个元素,将被替换成序列that
scala> val a =Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
//从索引1开始,总共2个元素需要被新的序列替换
scala> val b = a.patch(1,Array(6,7,8),2)
b: Array[Int] = Array(1, 6, 7, 8, 4)
permutations:collection.Iterator[Array[T]]
permutations表示排列,这个排列组合会选出所有排列顺序不同的字符组合
permutations和combinations的区别:
combinations(个数):
permutations:对于 “abc”、“cba”,视为不同的组合
scala> val b = a.permutations
b: Iterator[Array[String]] = non-empty iterator
scala> b.toList
//结果
res5: List[Array[String]] = List(Array(a, b, c), Array(a, c, b), Array(b, a, c), Array(b, c, a), Array(c, a, b), Array(c, b, a))
//combinations
scala> val b = a.combinations(3)
b: Iterator[Array[String]] = non-empty iterator
//结果
scala> b.toList
res6: List[Array[String]] = List(Array(a, b, c))
prefixLength(p: (T) => Boolean):Int
给定一个条件p,返回一个前置数列的长度,这个数列中的元素都满足p
scala> val a = Array(1,2,3,4,5,6,7,4,4)
a: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 4, 4)
//前缀数列的长度
scala> val b = a.prefixLength(x=>x<5)
b: Int = 4
product:A
返回所有元素乘积的值
scala> val a =Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b= a.product
//1*2*3*4=24
b: Int = 24
reduce(op: (A1,A1) => A1):A1
同fold,不需要初始值,从左向右进行
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = a.reduce((x,y)=>{println(x,y);x+y})
//(((1+2)+3)+4)=10
(1,2)
(3,3)
(6,4)
b: Int = 10
reduceLeft(op:(B,T) => B):B
同foldLeft,从左向右计算,不需要初始值
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = a.reduce((x,y)=>{println(x,y);x+y})
//(((1+2)+3)+4)=10
(1,2)
(3,3)
(6,4)
b: Int = 10
reduceLeftOption(op: (B,T) => B):Option[B]
同reduceLeft,返回Option
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = a.reduceLeftOption((x,y)=>{println(x,y);x+y})
(1,2)
(3,3)
(6,4)
b: Option[Int] = Some(10)
reduceRight(op: (B,T) => B): B
同foldRight,从右向左计算,不需要返回值
scala> val a =Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b =a.reduceRight((x,y)=>{println(x,y);x+y})
(3,4)
(2,7)
(1,9)
b: Int = 10
reduceRightOption(op: (T,B) => B):Option[B]
同reduceRight,返回Option
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = a.reduceRightOption((x,y)=>{println(x,y);x+y})
(3,4)
(2,7)
(1,9)
b: Option[Int] = Some(10)
reverse:Array[T]
反转序列
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b= a.reverse
b: Array[Int] = Array(4, 3, 2, 1)
reverselterator: collection.Iterator[T]
反向生成迭代器
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = a.reverseIterator.toList
b: List[Int] = List(4, 3, 2, 1)
reverseMap(f: (A) => B):Array[B]
map+reverse
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b =a.reverseMap(_*10)
b: Array[Int] = Array(40, 30, 20, 10)
runWith:def runWith[U](action: Int =>U):Int => Boolean
执行偏函数,当参数不在定义域内时,返回false,否则返回true,并执行action
val pf: PartialFunction[Int,Int] = { case m: Int if m %2 == 1=> m*2}
pf.runWith(println)(3) //return: Boolen = true
pf.runWith(println)(2) //return: Boolen = false
sameElements(that:Genlterable[A]):Boolean
判断两个序列的顺序和对应位置上元素是否一样
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = Array(1,2,3,4)
b: Array[Int] = Array(1, 2, 3, 4)
scala> a.sameElements(b)
res8: Boolean = true
scala> val c = Array(1,3,2,4)
c: Array[Int] = Array(1, 3, 2, 4)
scala> c.sameElements(a)
res9: Boolean = false
scan(z: B)(op: (B,B) => B)(implicit cbf: CanBuildFrom[Array[T],B,That]):That
scan会把每一步的计算结果放到一个新的集合中返回,而fold返回的是最后的结果
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = a.scan(5)(_+_)
b: Array[Int] = Array(5, 6, 8, 11, 15)
scanLeft(z: B)(op: (B,T) => B)(implicit bf: CanBuildFrom[Array[T],B,That]):That
从左向右计算,每一步的计算结果放到一个新的集合中返回
scala> val a =Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b =a.scanLeft(5)(_+_)
b: Array[Int] = Array(5, 6, 8, 11, 15)
scanRight(z: B)(op: (T,B) => B)(implicit bf: CanBuildFrom[Array[T],B,That]):That
从右向左计算,每一步的计算结果(从右向左)放到一个新的集合中返回
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = a.scanRight(5)(_+_)
b: Array[Int] = Array(15, 14, 12, 9, 5)
//和foldRight的区别
scala> val b = a.foldRight(5)(_+_)
b: Int = 15
segmentLength(p: (T) => Boolean,from:Int):Int
从序列的from开始查找,返回满足条件p的连续元素的长度,只返回第一个满足条件序列的长度
scala> val a =Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> val b = a.segmentLength(x=>x>3,3)
b: Int = 2
seq: collection.mutable.IndexedSeq[T]
产生一个引用当前序列的sequential视图
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> a.toSeq
res10: Seq[Int] = WrappedArray(1, 2, 3, 4, 5)
scala> a.seq
res13: scala.collection.mutable.IndexedSeq[Int] = WrappedArray(1, 2, 3, 4, 5)
size:Int
返回序列元素个数,同length
slice(from: Int, until: Int): Array[T]
返回当前序列中从from到until,不包括until处的元素
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> val b = a.slice(1,3)
b: Array[Int] = Array(2, 3)
sliding(size: Int): collection.Iterator[Array[T]]
滑动,当从第一个元素开始,每个元素和它后面的size-1个元素组成一个数组,最终组成一个迭代器返回,当剩余元素个数不够size时,则结束。
scala> val a= Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> val b = a.sliding(2)
b: Iterator[Array[Int]] = non-empty iterator
scala> b.toList
res14: List[Array[Int]] = List(Array(1, 2), Array(2, 3), Array(3, 4), Array(4, 5))
sliding(size:Int,step: Int):Iterator[Array[Int]]
该方法可以设置步长step,每一组元素组合完后,下一组从上一组起始元素位置 +step 后的位置处开始
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> val b = a.sliding(2,2)
b: Iterator[Array[Int]] = non-empty iterator
scala> b.toList
//以步长step设置为2,所以第二组从3开始
res15: List[Array[Int]] = List(Array(1, 2), Array(3, 4), Array(5))
sortBy(f:(T) => B)(implicit ord: math.Ordering[B]):Array[T]
按照指定的排序规则f对序列排序,默认是升序
//升序
scala> val a = Array(3,1,2,4)
a: Array[Int] = Array(3, 1, 2, 4)
scala> val b = a.sortBy(x=>x)
b: Array[Int] = Array(1, 2, 3, 4)
//降序
scala> val b = a.sortBy(x => -x)
b: Array[Int] = Array(4, 3, 2, 1)
sortWith()(It: (Int,Int) => Boolean): Array[Int]
按照指定的排序规则来对序列排序
scala> val a = Array(3,1,2,4)
a: Array[Int] = Array(3, 1, 2, 4)
//升序
scala> val b =a.sortWith(_>_)
b: Array[Int] = Array(4, 3, 2, 1)
//降序
scala> val b =a.sortWith(_<_)
b: Array[Int] = Array(1, 2, 3, 4)
sorted(implicit ord:math.Ordering[B]):Array[T]
使用默认的排序规则对序列排序(默认是升序排序),数字按照大小,字符/字符串按照ASCII码排序
scala> val a = Array(3,1,2,4)
a: Array[Int] = Array(3, 1, 2, 4)
//默认升序排序
scala> val b = a.sorted
b: Array[Int] = Array(1, 2, 3, 4)
//降序排序
scala> val b = a.sorted(Ordering.Int.reverse)
b: Array[Int] = Array(4, 3, 2, 1)
span(p: (T) => Boolean):(Array[T],Array[T])
将序列拆分成两个数组,从第一个元素开始,直到第一个不满足条件的元素为止,其中满足条件的放第一个数组,第一个不满足条件的放第二个数组,返回的是包含这两个数组的元祖
scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
//返回的是元祖
scala> val b = a.span(x=>x>3)
b: (Array[Int], Array[Int]) = (Array(),Array(1, 2, 3, 4, 5))
scala> val b = a.span(x=>x<3)
b: (Array[Int], Array[Int]) = (Array(1, 2),Array(3, 4, 5))
splitAt(n : Int): (Array[T],Array[T])
从指定位置开始,把序列拆分成两个数组
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = a.splitAt(2)
b: (Array[Int], Array[Int]) = (Array(1, 2),Array(3, 4))
scala> val b = a.splitAt(3)
b: (Array[Int], Array[Int]) = (Array(1, 2, 3),Array(4))
startsWith(that: GenSeq[B]): Boolean
判断当前序列是否以that序列开始
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = Array(1,2)
b: Array[Int] = Array(1, 2)
scala> val c = a.startsWith(b)
c: Boolean = true
startsWith(that: GenSeq[B],offset: Int): Boolean
判断序列从指定偏移处offset是否以某个序列that开始
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = Array(2,3)
b: Array[Int] = Array(2, 3)
scala> val c = a.startsWith(b,1)
c: Boolean = true
stringPrefix:String
返回toString结果的前缀
sum:A
序列求和,元素需为Numeric[T]类型
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = a.sum
b: Int = 10
//char数组
scala> val c = Array('a','b','c')
c: Array[Char] = Array(a, b, c)
//限定类型为Int
scala> val b:Int =c.sum
b: Int = 294
take (n:Int): Array[T]
返回当前序列中前n个元素组成的序列
val a = Array(1,2,3,4,5)
val b = a.take(3) //1,2,3
tail:Array[T]
返回当前序列中不包含第一个元素的序列
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = a.tail
b: Array[Int] = Array(2, 3, 4)
tails:collection.Iterator[Array[T]]
同inits,每一步都进行tail操作,最后返回的是一个迭代器,迭代器最后一个为空数组
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> a.tails.foreach(x=>println(x.mkString(",")))
//结果
1,2,3,4
2,3,4
3,4
4
withFilter:def withFilter(p: Int => Boolean): scala.collection.generic.FilterMonadic[Int,Array[Int]]
take(n:Int):Array[T]
返回当前序列中,前n个元素组成的序列
scala> val a =Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b=a.take(3)
b: Array[Int] = Array(1, 2, 3)
takeRight(n: Int): Array[T]
返回当前序列中,从右边开始,后n个元素组成的序列
scala> val a =Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = a.takeRight(3)
b: Array[Int] = Array(2, 3, 4)
takeWhile(p: (T) => Boolean): Array[T]
返回当前序列中,从第一个元素开始,满足条件p的连续元素组成的数组,如果第一个不满足就返回一个空数组
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = a.takeWhile(x=>x<3)
b: Array[Int] = Array(1, 2)
scala> val a = Array(3,1,2,4)
a: Array[Int] = Array(3, 1, 2, 4)
//第一个不满足,所以返回一个空数组
scala> val b = a.takeWhile(x=>x<3)
b: Array[Int] = Array()
to:
toArray:Array[A]
将序列转成Array类型
toBuffer:Buffer[A]
将序列转换成Buffer类型
toIndexedSeq:collection.immutable.IndexedSeq[T]
将序列转换成IndexedSeq类型
tolterable:collection.Iterable[T]
将序列转换成可迭代的类型
tolterator:collection.Iterator[T]
将序列转换成迭代器,同 iterator方法
toList:List[T]
toMap:Map[T,U]
需要被转换序列中包含的元素是 Tuple2类型
toSeq: collection.Seq[T]
同Seq类型
toSet: Set[B > :A]:Set[B]
同Set类型
toStream: collection.immutable.Stream[T]
同Stream类型
toTraversable
toVector:Vector[T]
将序列转换成Vector类型
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = a.toVector
b: Vector[Int] = Vector(1, 2, 3, 4)
transform
transpose(implicit asArray: (T) => Array[U]): Array[Array[U]]
转换矩阵,将二位数组转置
scala> val a = Array(Array(1,2),Array(3,4),Array(5,6))
a: Array[Array[Int]] = Array(Array(1, 2), Array(3, 4), Array(5, 6))
scala> val b = a.transpose
b: Array[Array[Int]] = Array(Array(1, 3, 5), Array(2, 4, 6))
union(that:collection.Seq[T]):Array[T]
合并两个序列,同操作符++
scala> val a = Array(1,2)
a: Array[Int] = Array(1, 2)
scala> val b = List(3,4)
b: List[Int] = List(3, 4)
scala> val c = a.union(b)
c: Array[Int] = Array(1, 2, 3, 4)
//++看左边序列的类型
scala> val c = b.union(a)
c: List[Int] = List(3, 4, 1, 2)
unzip(implicit asPair: (T) => (T1,T2),ct1:ClassTag[T1],ct2:ClassTag[T2]):(Array[T1],Array[T2])
含有两个二元组的数组,将每个元祖的第一个元素组成一个新数组,第二个元素组成一个新数组,返回包含这两个数组的元祖
scala> val a = Array(("a",1),("b",2))
a: Array[(String, Int)] = Array((a,1), (b,2))
scala> val b = a.unzip
b: (Array[String], Array[Int]) = (Array(a, b),Array(1, 2))
unzip3(implicit asTriple: (T) => (T1,T2,T3),ct1:ClassTag[T1],ct2:ClassTag[T2],ct3:ClassTag[T3]):(Array[T1],Array[T2],Array[T3])
含有三个三元组的数组,将每个元祖的第一个元素组成一个数组,第二个元素组成一个数组,第三个元素组成一个数组,返回包含这三个数组的元祖
scala> val chars = Array(("a","b","x"),("c","d","y"),("e","f","z"))
chars: Array[(String, String, String)] = Array((a,b,x), (c,d,y), (e,f,z))
scala> val b = chars.unzip3
b: (Array[String], Array[String], Array[String]) = (Array(a, c, e),Array(b, d, f),Array(x, y, z))
update(i: Int,x: T):Unit
将序列中i索引处的元素更新为x
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> a.update(1,7)
scala> a
res20: Array[Int] = Array(1, 7, 3, 4)
updated(index: Int, elem: A): Array[A]
将序列中i索引处的元素更新为elem,并返回替换后的数组
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = a.updated(1,7)
b: Array[Int] = Array(1, 7, 3, 4)
view(from: Int, until: Int):IndexedSeqView[T,Array[T]]
返回当前序列中从from到until之间的序列,不包括until处的元素
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = a.view(1,3).mkString(",")
b: String = 2,3
withFilter(p: (T) => Boolean):FilterMonadic[T,Array[T]]
根据条件p过滤元素
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = a.withFilter(x=>x>2).map(x=>x)
b: Array[Int] = Array(3, 4)
withFilter和filter的区别
filter返回的是Array类型
withFilter则是不生成新的集合
withFilter将非严格地(即延迟地)将过滤后的值传递到以后的map / flatMap / withFilter 调用,保存第二遍通过(过滤)集合,因此,当传递到这些后续方法调用时,它将更有效。
最后结论是,有链式操作的时候,推荐使用withFilter
zip(that: GenIterable[B]): Array[(A,B)]
将两个序列对应位置上的元素组成一个元素数组,要求两个序列长度相同
scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)
scala> val b = Array(4,3,2,1)
b: Array[Int] = Array(4, 3, 2, 1)
scala> val c =a.zip(b)
c: Array[(Int, Int)] = Array((1,4), (2,3), (3,2), (4,1))
zipAll(that: collection.Iterable[B],thisElem: A, thatElem: B):Array[(A,B)]
允许两个序列长度不同进行zip操作,长度不足的自动根据值填充,如果当前序列(调用zipAll的序列)短,不足的填充为thisElem,如果that序列(传进来当参数的序列)短,填充为thatElem
scala> val a = Array(1,2,3,4,5,6,7)
a: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7)
scala> val b = Array(5,4,3,2,1)
b: Array[Int] = Array(5, 4, 3, 2, 1)
scala> val c = a.zipAll(b,8,9)
c: Array[(Int, Int)] = Array((1,5), (2,4), (3,3), (4,2), (5,1), (6,9), (7,9))
scala> val d = b.zipAll(a,8,9)
d: Array[(Int, Int)] = Array((5,1), (4,2), (3,3), (2,4), (1,5), (8,6), (8,7))
zipWithIndex
序列中的每个元素和它的索引组成一个元组数组
scala> val a = Array('a','b','c','d')
a: Array[Char] = Array(a, b, c, d)
//结果
scala> val b = a.zipWithIndex
b: Array[(Char, Int)] = Array((a,0), (b,1), (c,2), (d,3))
本文详细介绍了Scala中数组和集合的各种操作函数,包括++、+:、/:、:、addString、aggregate、andThen、apply、collect、contains、diff、distinct、drop、exists、filter、flatMap、fold、foldLeft、foldRight、foreach、groupBy、indexOf、intersect、isDefinedAt、last、length、map、mkString、padTo、partition、reduce、reverse、scan、slice、sortBy、span、startsWith、tail、take、takeWhile、to系列转换函数、transform、transpose等。通过这些函数,开发者可以高效地处理和操作Scala数组和集合数据。
2522

被折叠的 条评论
为什么被折叠?



