文章目录
- ++ ++:
- +: :+
- /: :\
- 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: