一些你可能用得到的scala函数

一、scala函数详解

本文中所有函数均在cmd中运行,如不适应黑界面可以自行改颜色:color f0

(1)声明一个数组对象

var arr = Array(1,2,3,4,5,6)
//arr: Array[Int] = Array(1, 2, 3, 4, 5, 6)

(2)数组++数组:把数组里的元素放到一个数组中,即合并数组,并返回

scala> var c = Array(2,4)
c: Array[Int] = Array(2, 4)

scala> arr++c
res1: Array[Int] = Array(1, 2, 3, 4, 5, 6, 2, 4)

(3)++:合并,右边操纵数的类型决定着返回结果的类型

scala> var arr = Array(1,2,3,4,5,6)
arr: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> var c = List(2,4)
c: List[Int] = List(2, 4)

scala> arr++:c
res0: List[Int] = List(1, 2, 3, 4, 5, 6, 2, 4)

(4)变量+:数组:把变量值加到数组的前面
数组:+变量:把变量值加到数组的后面

scala> var k = 100
k: Int = 100

scala> k+:arr
res2: Array[Int] = Array(100, 1, 2, 3, 4, 5, 6)

scala> arr:+k
res4: Array[Int] = Array(1, 2, 3, 4, 5, 6, 100)

(5)定义一个list型,变量+:list返回list型,数组.++(list)返回数组型

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

scala> k+:a
res8: List[Int] = List(100, 3, 4, 5)

scala> c.++(a)
res7: Array[Int] = Array(2, 4, 3, 4, 5)

(6)/:对数组中所有元素进行相同的操作(foldLeft的简写)
:\(foldRight的简写)

scala> var k = 100
k: Int = 100

scala> val a = List(3,4,5)
a: List[Int] = List(3, 4, 5) 

scala> (100 /: a)(_+_)
res9: Int = 112     //3+4+5+100

scala> (a :\ 100)(_*_)
res11: Int = 6000   //5*4*3*100

(7)addString把数组中的元素逐个添加到str中,中间用“#”(分隔符)隔开

scala> var str = new StringBuilder
str: StringBuilder =

scala> a.addString(str)
res12: StringBuilder = 345

scala> str
res13: StringBuilder = 345

scala> a.addString(str,"#")
res14: StringBuilder = 3453#4#5

(8)addString(str,start,seq,end)首尾和中间不同位置
在不同位置指定不同字符

scala> var str = ""
str: String = ""

scala> var str = new StringBuilder
str: StringBuilder =

scala> a.addString(str,"[",",","]");
res15: StringBuilder = [3,4,5]

(9)apply取出指定索引处的元素

scala> var c = Array(1,2,3,4,5,6)
c: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> c.apply(2)
res16: Int = 3

(10)canEqual判断两个对象是否可以比较

scala> val d = Array(2,3,45,5)
d: Array[Int] = Array(2, 3, 45, 5)

scala> c
res18: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> c.canEqual(d)
res17: Boolean = true

scala> c.canEqual(10)
res19: Boolean = true

scala> c.canEqual("aa")
res20: Boolean = true

scala> c.canEqual('z')
res21: Boolean = true

scala> object myObj{}
defined object myObj

scala> c.canEqual(myObj)
res22: Boolean = true

(11)charAt获取索引处的字符
这个方法会执行一个隐式转换,将Array[T]转换为ArrayCharSequence,只有当T为char类型时,这个转换才会发生

scala> var e = Array('a','b','c')
e: Array[Char] = Array(a, b, c)

scala> e.charAt(2)
res23: Char = c

(12)clone创建一个副本,创建后的副本不因原值的变动而改动

scala> var e = c
e: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> c
res24: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> c(2) = 44

scala> c
res27: Array[Int] = Array(1, 2, 44, 4, 5, 6)

scala> e
res28: Array[Int] = Array(1, 2, 44, 4, 5, 6)

scala> var k = c.clone
k: Array[Int] = Array(1, 2, 44, 4, 5, 6)

scala> c(2) = 100

scala> k
res30: Array[Int] = Array(1, 2, 44, 4, 5, 6)

(13)collect通过执行一个偏函数,得到一个新的数组对象

scala> c
res31: Array[Int] = Array(1, 2, 100, 4, 5, 6)

scala> val fun:PartialFunction[Int,Int]={ //偏函数定义
     |   case x => x+1
     | }
fun: PartialFunction[Int,Int] = <function1>

scala> c.collect(fun)
res32: Array[Int] = Array(2, 3, 101, 5, 6, 7)

scala> val fun:PartialFunction[Int,Int]={
     |   case 2 => 1
     |   case y if y % 2 == 0 => y+50
     |   case x => x
     | }
fun: PartialFunction[Int,Int] = <function1>

scala> c.collect(fun)
res33: Array[Int] = Array(1, 1, 150, 54, 5, 56)

scala> val fun:PartialFunction[Int,String]={
     |   case 1 =>"xixi"
     |   case y if y%2 == 0 => "gg"
     |   case x => x+ "ff"
     | }
fun: PartialFunction[Int,String] = <function1>

scala> c.collect(fun)
res34: Array[String] = Array(xixi, gg, gg, gg, 5ff, gg)

(14)collectFirst在序列中查找第一个符合偏函数定义的元素,并执行偏函数计算

scala> arr
res8: Array[Any] = Array(1, a, b)

scala> val value = arr.collectFirst({case x:Int => x*100})
value: Option[Int] = Some(100)

scala> var arr = Array(2,'a',"b")
arr: Array[Any] = Array(2, a, b)

scala> val value = arr.collectFirst({case x:Int => x* 100})
value: Option[Int] = Some(200)

(15)combinations(n)选出所有包含字符不一样的组合,n表示组合的长度
mkstring(",")把元素用特殊符号拼起来

scala> val arr = Array("a","b","c")
arr: Array[String] = Array(a, b, c)

scala> val nw = arr.combinations(2)
nw: Iterator[Array[String]] = non-empty iterator

scala> nw.foreach((x)=>println(x.mkString(",")))
a,b
a,c
b,c

scala> c
res36: Array[Int] = Array(1, 2, 100, 4, 5, 6)

scala> c.mkString(",")
res37: String = 1,2,100,4,5,6

(16)contains是否包含特定元素

scala> c
res39: Array[Int] = Array(1, 2, 100, 4, 5, 6)

scala> c.contains(50)
res40: Boolean = false

scala> c.contains(100)
res41: Boolean = true

(17)containsSlice数组里是否包含另一个数组

scala> c
res43: Array[Int] = Array(1, 2, 100, 4, 5, 6)

scala> var d = Array(2,4,5)
d: Array[Int] = Array(2, 4, 5)

scala> c.containsSlice(d)
res42: Boolean = false

scala> var d = Array(4,5)
d: Array[Int] = Array(4, 5)

scala> c.containsSlice(d)
res44: Boolean = true

(18)c.copyToArray(a,n)从第n个元素开始把c拷贝给a
此外还可设定copy长度即c.copyToArray(a,n,l),l代表长度

scala> c
res45: Array[Int] = Array(1, 2, 100, 4, 5, 6)

scala> val a = Array(0,0,0,0,0,0)
a: Array[Int] = Array(0, 0, 0, 0, 0, 0)

scala> c.copyToArray(a,2)

scala> a
res47: Array[Int] = Array(0, 0, 1, 2, 100, 4)

(19)copyToBuffer把数组中的内容拷贝到buffer中

scala> c
res48: Array[Int] = Array(1, 2, 100, 4, 5, 6)

scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> val a = ArrayBuffer(0,0,0,0,0,0)
a: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(0, 0, 0, 0, 0, 0)

scala> c.copyToBuffer(a)

scala> a
res54: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(0, 0, 0, 0, 0, 0, 1, 2, 100, 4, 5, 6)

(20)corresponds判断数组长度是否相等,并且对应位置的元素是否满足某种条件

scala> c
res57: Array[Int] = Array(1, 2, 100, 4, 5, 6)

scala> val a = Array(0,0,0,0,0,0)
a: Array[Int] = Array(0, 0, 0, 0, 0, 0)

scala> c.corresponds(a)(_==_)
res58: Boolean = false

(21)count统计满足条件的个数

scala> c
res59: Array[Int] = Array(1, 2, 100, 4, 5, 6)

scala> c.count(p=>p%2==0)  //偶数
res60: Int = 4

(22)diff返回c中不同于a的数组

scala> c
res61: Array[Int] = Array(1, 2, 100, 4, 5, 6)

scala> var a = Array(2,4,5)
a: Array[Int] = Array(2, 4, 5)

scala> c.diff(a)
res64: Array[Int] = Array(1, 100, 6)

(23)remove去除某个位置的元素
remove(m,n)m表示下标,n表示元素个数

scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> val a = ArrayBuffer(1,2,3)
a: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)

scala> a.remove(1)
res67: Int = 2

scala> a
res68: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 3)

(24)distinct 去重:去除重复元素只保留一个

scala> val m = Array(1,1,2,3,3,3,4,4)
m: Array[Int] = Array(1, 1, 2, 3, 3, 3, 4, 4)

scala> val n = m.distinct
n: Array[Int] = Array(1, 2, 3, 4)

(25)drop删除前n个元素,并返回
dropRight去掉尾部的n个元素,并返回

scala> n
res71: Array[Int] = Array(1, 2, 3, 4)

scala> val p = n.drop(2)
p: Array[Int] = Array(3, 4)

(26)dropWhile去除当前数组中符合条件的元素,从数组的第一个条件起就要满足条件,直到第一个不满足条件的元素为止,若第一个元素不符合条件,就返回整个数组

scala> val c = Array(1,2,100,4,5,6)
c: Array[Int] = Array(1, 2, 100, 4, 5, 6)

scala> c.dropWhile(p=>p%2==1)
res1: Array[Int] = Array(2, 100, 4, 5, 6)

(27)endsWith判断是否以某个序列结尾
startsWith判断是否以某个序列开头

scala> val m = Array(2,3,4)
m: Array[Int] = Array(2, 3, 4)

scala> val n = Array(3,2,3,4)
n: Array[Int] = Array(3, 2, 3, 4

scala> n.endsWith(m)
res4: Boolean = true

scala> n.startsWith(m)
res5: Boolean = false

(27)exists判断当前数组是否包含符合条件的元素

scala> c
res6: Array[Int] = Array(1, 2, 100, 4, 5, 6)

scala> c.exists(p=>p>5)
res7: Boolean = true

(28)filter取得当前数组中符合条件的元素,组成新的数组返回
withFilter§根据条件p过滤元素

scala> c
res8: Array[Int] = Array(1, 2, 100, 4, 5, 6)

scala> c.filter(p=>p%2 == 0)
res9: Array[Int] = Array(2, 100, 4, 6)

scala> def myfun(p:Int):Boolean = {
     |   p%2 == 0
     | }
myfun: (p: Int)Boolean

scala> c.filter(myfun)
res10: Array[Int] = Array(2, 100, 4, 6)

(28)find查找第一个符合条件的元素

scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> val a = ArrayBuffer(1,100,4,5,6)
a: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 100, 4, 5, 6)

scala> c
res13: Array[Int] = Array(1, 2, 100, 4, 5, 6)

scala> a.find(x=>x==2)
res14: Option[Int] = None

scala> c.find(x=>x==2)
res15: Option[Int] = Some(2)

scala> c.find(x=>x==2).get
res16: Int = 2

(29)flatMap和Map(是否降维)
map对序列中的元素进行某操作

scala> val c = Array(1,2,3)
c: Array[Int] = Array(1, 2, 3)

scala> c.flatMap(x=>(1 to x))
res17: Array[Int] = Array(1, 1, 2, 1, 2, 3)//第一次1;第二次1,2;第三次1,2,3


scala> c.map(x=>(1 to x))
res18: Array[scala.collection.immutable.Range.Inclusive] = Array(Range(1), Range(1, 2), Range(1, 2, 3))

(30)flatten将二维数组变成一维数组

scala> val p = c.map(x=>(1 to x))
p: Array[scala.collection.immutable.Range.Inclusive] = Array(Range(1), Range(1, 2), Range(1, 2, 3))

scala> val q = p.flatten
q: Array[Int] = Array(1, 1, 2, 1, 2, 3)

(31)fold对数组中的每个元素进行计算

scala> c
res19: Array[Int] = Array(1, 2, 3)

scala> c.fold(5)(_+_)
res20: Int = 11
//运算过程
		 5+1=6
		 6+2=8
		 8+3=11


scala> def myfun(m:Int,n:Int):Int={
     |   m+n
     | }
myfun: (m: Int, n: Int)Int

scala> c.fold(5)(myfun)
res21: Int = 11

(32)aggregate先分组再聚合

scala> val m = Array(1,2,3,4)
m: Array[Int] = Array(1, 2, 3, 4)

scala> val c = m.par.aggregate(5)(_+_,_+_)
c: Int = 30(有时会出现25,错误)
//运算过程
		 5+1=6
		 5+2=7
		 5+3=8
		 5+4=9
		 8+6=14
		 7+9=15 

(33)forall检查序列中的元素是否都满足某个条件,如果序列为空,返回true

scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)

scala> val b = a.forall({x:Int => x>0})
b: Boolean = true

scala> val b = a.forall({x:Int => x>2})
b: Boolean = false

(34)foreach遍历序列中的元素,并进行一定的操作

scala> val a = Array(1,2,3,4)
a: Array[Int] = Array(1, 2, 3, 4)

scala> a.foreach(x=>println(x*10))
10
20
30
40

(35)分组
groupBy按条件分组
grouped按指定数量分组

scala> val k = Array(1,2,3,4,5,6)
k: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> k.groupBy(x=> x match{      //按奇偶性分
     |   case x if x%2 ==0 => "ou"
     |   case _ => "ji"
     | })
res25: scala.collection.immutable.Map[String,Array[Int]] = Map(ou -> Array(2, 4, 6), ji -> Array(1, 3, 5))

scala> k.grouped(3)      //每组3个
res26: Iterator[Array[Int]] = non-empty iterator

scala> k.grouped(3).foreach(x=>println(x))
[I@734ab79
[I@5fa8a0b4

scala> k.grouped(3).foreach(x=>println(x.mkString(",")))
1,2,3
4,5,6

(36)head返回序列的第一个元素,若序列为空,则报错

scala> k
res29: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> k.head
res30: Int = 1

(37)tail和last
tail指除head外剩下的其他元素组成的集合
last指序列中最后一个元素

scala> k
res32: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> k.tail
res31: Array[Int] = Array(2, 3, 4, 5, 6)

scala> k.last
res33: Int = 6

(38)headOption返回some()或者none,为空则为none

scala> var lst = ArrayBuffer()
lst: scala.collection.mutable.ArrayBuffer[Nothing] = ArrayBuffer()

scala> lst.headOption
res34: Option[Nothing] = None

(39)indexOf返回元素在序列中的索引,可以指定从某个索引处开始查找,找到第一个就返回

scala> val a = Array(1,3,4,5,3,1)
a: Array[Int] = Array(1, 3, 4, 5, 3, 1)

scala> a.indexOf(3,4)
res3: Int = 4

(40)indexOfSlice检验序列中是否包含另一个序列,并返回第一个匹配出现的元素的索引
indexWhere返回当前序列中第一个满足 p 条件的元素的索引

scala> val a = (1 to 20)
a: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)

scala> a.indexOfSlice((4 to 6))
res35: Int = 3

scala> a.indexWhere(p=>p>10)
res36: Int = 10

(41)init返回当前序列中不包含最后一个元素的序列

scala> val a = (1 to 20)
a: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)

scala> a.init
res0: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19)

42)intersect取两个集合的交集

scala> var b = (10 to 15)
b: scala.collection.immutable.Range.Inclusive = Range(10, 11, 12, 13, 14, 15)

scala> var a  = (13 to 19)
a: scala.collection.immutable.Range.Inclusive = Range(13, 14, 15, 16, 17, 18, 19)

scala> a.intersect(b)
res1: scala.collection.immutable.IndexedSeq[Int] = Vector(13, 14, 15)

43)isDefinedAt数组中是否有这个下标

scala> a
res4: scala.collection.immutable.Range.Inclusive = Range(13, 14, 15, 16, 17, 18, 19)

scala> a.isDefinedAt(20)
res2: Boolean = false

scala> a.isDefinedAt(2)
res3: Boolean = true

44)isEmpty判断数组是否为空

scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> val c = ArrayBuffer()
c: scala.collection.mutable.ArrayBuffer[Nothing] = ArrayBuffer()

scala> c.isEmpty
res5: Boolean = true

45)isTraversableAgain判断序列是否可以反复遍历,对于Traverables一般返回true,对于Iterators返回false

scala> val arr = Array(1,2,3,4,5,6)
arr: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> arr.isTraversableAgain
res6: Boolean = true

46)iterator对序列中的每个元素产生一个迭代器

scala>val b =  arr.iterator
res7: Iterator[Int] = non-empty iterator      //产生迭代器后可以通过迭代器访问b

47)inits对集合中的元素进行 init 操作,该操作的返回值中, 第一个值是当前序列的副本,包含当前序列所有的元素,最后一个值是空的,对头尾之间的值进行init操作,上一步的结果作为下一步的操作对象

scala> val a =  Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> val b = a.inits.toList
b: List[Array[Int]] = List(Array(1, 2, 3), Array(1, 2), Array(1), Array())

48)lengthCompare(len)比较序列的长度和参数len,根据二者的关系返回不同的值

scala> arr
res11: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> arr.lengthCompare(5)
res9: Int = 1

scala> arr.lengthCompare(6)
res12: Int = 0

scala> arr.lengthCompare(7)
res10: Int = -1

scala> arr.lengthCompare(8)
res13: Int = -2

49)scala实现wordcount

scala> val str = "hello mysql hello world"
str: String = hello mysql hello world

scala> str.split(" ").map(x=>(x,1))
res14: Array[(String, Int)] = Array((hello,1), (mysql,1), (hello,1), (world,1))

scala> str.split(" ").map(x=>(x,1)).groupBy(x=>x._1).foreach(x=>{print(x._1,x._2.length)})
(world,1)(mysql,1)(hello,2)

50)max返回序列中最大的元素
maxBy返回序列中第一个符合条件的最大的元素

scala> arr
res16: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> arr.max
res17: Int = 6

scala> val b = Array(1,2,4,2,3,6)
b: Array[Int] = Array(1, 2, 4, 2, 3, 6)

scala> println(b.maxBy({x:Int => x>2}))
4

51)nonEmpty判断序列不是空

scala> arr
res18: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> arr.nonEmpty
res19: Boolean = true

52)padTo(m,n)后补齐序列,需要一个长度为m的新序列,空出的填n
A.copyToArray(B,1,2)从下标为1的位置把A中的元素拷贝给B,长度为2

scala> val arr1 = Array(1,2,3)
arr1: Array[Int] = Array(1, 2, 3)

scala> val a1 = arr.padTo(10,0)
a1: Array[Int] = Array(1, 2, 3, 4, 5, 6, 0, 0, 0, 0)

scala> arr1.copyToArray(a1,5,3)

scala> a1
res21: Array[Int] = Array(1, 2, 3, 4, 5, 1, 2, 3, 0, 0)

53)partititon
按条件将序列拆分为两个新的序列,满足条件的放到第一个序列,其余的放入第二个序列

scala> arr
res22: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> arr.partition(_%2==0)
res23: (Array[Int], Array[Int]) = (Array(2, 4, 6),Array(1, 3, 5))

54)patch批量替换

scala> arr
res24: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> arr1
res25: Array[Int] = Array(1, 2, 3)

scala> arr.patch(3,arr1,2)
res26: Array[Int] = Array(1, 2, 3, 1, 2, 3, 6)
//从arr的第4个元素开始,取两个元素,即4和5,这两个元素替换为arr1的内容

55)permuntations排列组合
组合内的内容可以相同,但顺序不能相同

scala> arr1.permutations.toList
res28: List[Array[Int]] = List(Array(1, 2, 3), Array(1, 3, 2), Array(2, 1, 3), Array(2, 3, 1), Array(3, 1, 2), Array(3, 2, 1))

56)prefixLength§
给定一个条件p,返回一个前置数列的长度,这个数列中的元素都满足p

scala> arr
res31: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> arr.prefixLength(_<4)
res30: Int = 3

57)product返回所有元素乘积的值
sum返回所有元素和的值

scala> (1 to 5).product
res32: Int = 120

scala> (1 to 5).sum
res33: Int = 15

58)reduce
对序列中的每个元素进行二元运算,无初始值
reduceLeft从左往右计算;reduceRight从右往左计算
reduceLeftOption和reduceRightOption计算Option,计算方向不同

scala> arr
res34: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> arr.reduce(_+_)
res35: Int = 21
/**
1+2=3;3+3=6;6+4=10;10+5=15;15+6=21
*/

scala> arr.reduce(_-_)
res36: Int = -19
/**
1-2=-1;-1-3=-4;-4-4-=-8;-8-5=-13;-13-6=-19
*/

59)reserve 反转
reverseIterator 反向生成迭代

scala> arr
res37: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> arr.reverse
res38: Array[Int] = Array(6, 5, 4, 3, 2, 1)

60)reserverMap((_,1))与map方向相反

scala> arr
res39: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> arr.reverseMap((_,1))
res40: Array[(Int, Int)] = Array((6,1), (5,1), (4,1), (3,1), (2,1), (1,1))

61)sameElements判断两个序列是否顺序和对应位置上的元素都一样

scala> val arr2 = arr.clone
arr2: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> arr.sameElements(arr2)
res41: Boolean = true

62)segmentLength(p,from)
从序列的from处开始向后查找,所有满足p的连续元素的长度

scala> arr
res42: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> arr.segmentLength(_<4,1)
res43: Int = 2

63)seq产生一个引用当前序列的sequential视图

scala> arr
res44: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> arr.seq
res45: scala.collection.mutable.IndexedSeq[Int] = WrappedArray(1, 2, 3, 4, 5, 6)

64)sliding(size,步长)
从第一个元素开始,每个元素和它后面的size-1个元素组成一个数组,最终组成一个新的集合返回,当剩余元素不够size数,则停止

scala> arr
res47: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> arr.sliding(3).foreach(x=>println(x.mkString(",")))
1,2,3
2,3,4
3,4,5
4,5,6

scala> arr.sliding(3,3).foreach(x=>println(x.mkString(",")))
1,2,3
4,5,6

scala> arr.sliding(3,2).foreach(x=>println(x.mkString(",")))
1,2,3
3,4,5
5,6

scala> arr.sliding(3,4).foreach(x=>println(x.mkString(",")))
1,2,3
5,6

65)sortBy按指定的排序规则排序

scala> arr
res52: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> arr.sortBy(x=>x)
res53: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> arr.sortBy(x=>0-x)
res54: Array[Int] = Array(6, 5, 4, 3, 2, 1)

scala> var arr = Array("hello","world","are")
arr: Array[String] = Array(hello, world, are)

scala> arr.sortBy(x=>x)
res55: Array[String] = Array(are, hello, world)

scala> var arr1 = arr:+20:+"abc"
arr1: Array[Any] = Array(hello, world, are, 20, abc)

scala> arr1.sortBy(x=>x)
<console>:15: error: No implicit Ordering defined for Any.
       arr1.sortBy(x=>x)
    

66)sortWith自定义排序方法

scala> arr1.map(x=>x+"").sortWith(_.compareTo(_)>0)
res57: Array[String] = Array(world, hello, are, abc, 20)

scala> def mycomp(x:Any,y:Any):Boolean = {
     |   x.asInstanceOf[String] >y.asInstanceOf[String]
     | }
mycomp: (x: Any, y: Any)Boolean

scala> mycomp("abc",20)
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
  at .mycomp(<console>:13)
  ... 32 elided

scala> def mycomp(x:Any,y:Any):Boolean ={
     |   x+"" >y+ ""
     | }
mycomp: (x: Any, y: Any)Boolean

scala> mycomp("abc",20)
res59: Boolean = true

scala> arr.sortWith(mycomp)
res60: Array[String] = Array(world, hello, are)

scala> def mycomp(x:Any,y:Any):Boolean ={
     |   x.toString>y.toString
     | }
mycomp: (x: Any, y: Any)Boolean

scala> mycomp("abc",24)
res61: Boolean = true

scala> arr1
res62: Array[Any] = Array(hello, world, are, 20, abc)

scala> arr1.partition(x=>x.isInstanceOf[String])
res63: (Array[Any], Array[Any]) = (Array(hello, world, are, abc),Array(20))

67)span分割序列为两个集合,从第一个元素开始,直到找到第一个不满足条件的元素止,之前的元素放到第一个集合,其它的放到第二个集合

scala> arr1
res64: Array[Any] = Array(hello, world, are, 20, abc)

scala> arr1.span(_.isInstanceOf[String])
res65: (Array[Any], Array[Any]) = (Array(hello, world, are),Array(20, abc))

68)splitAt从指定位置开始把序列拆分为两个集合

scala> arr1.splitAt(3)
res67: (Array[Any], Array[Any]) = (Array(hello, world, are),Array(20, abc))

scala> arr
res69: Array[String] = Array(hello, world, are)

scala> arr.splitAt(2)
res68: (Array[String], Array[String]) = (Array(hello, world),Array(are))

69)startsWith从指定偏移处是否以某个序列开始
endsWith判断是否以某个序列结尾

scala> val t1 = Array("world","are")
t1: Array[String] = Array(world, are)

scala> arr1
res70: Array[Any] = Array(hello, world, are, 20, abc)

scala> arr.startsWith(t1)
res71: Boolean = false

scala> arr.startsWith(t1,1)
res72: Boolean = true

scala> arr1
res73: Array[Any] = Array(hello, world, are, 20, abc)

scala> val t2 = Array("are",20)
t2: Array[Any] = Array(are, 20)

scala> arr1.endsWith(t2)
res77: Boolean = false
//endsWith无法加偏移量

70)stringPrefix返回toString结果的前缀

scala> "abcd".stringPrefix
res78: String = String

scala> "243".stringPrefix
res79: String = String

71)subSequence返回start和end间的字符序列

scala> var chars = Array('a','b','c','d')
chars: Array[Char] = Array(a, b, c, d)

scala> val b = chars.subSequence(1,3)
b: CharSequence = bc

scala> println(b.toString)
bc

72)take返回当前序列中前n个元素组成的序列
slice

scala> arr1
res81: Array[Any] = Array(hello, world, are, 20, abc)

scala> arr1.take(3)
res82: Array[Any] = Array(hello, world, are)

scala> arr1.slice(1,3)
res83: Array[Any] = Array(world, are)

73)takeRight返回当前序列中,从右边开始,选择n 个元素组成的序列

scala> arr1
res85: Array[Any] = Array(hello, world, are, 20, abc)

scala> arr1.takeRight(3)
res84: Array[Any] = Array(are, 20, abc)

74)takeWhile返回当前序列中,从第一个元素开始,满足条件的连续元素组成的序列

scala> arr1.takeWhile(_.isInstanceOf[String])
res86: Array[Any] = Array(hello, world, are)

75)transpose
矩阵转换,二维数组行列转换

scala> val ar = Array(Array(3,4,5),Array(7,8,9),Array(10,11,12))
ar: Array[Array[Int]] = Array(Array(3, 4, 5), Array(7, 8, 9), Array(10, 11, 12))

scala> ar.transpose
res87: Array[Array[Int]] = Array(Array(3, 7, 10), Array(4, 8, 11), Array(5, 9, 12))

76)unzip(只能2*2,且必须是数组套元组)
将含有两个元素的数组,第一个元素取出组成一个序列,第二个元素组成一个序列

scala> val ar1 = Array((3,4),(7,8))
ar1: Array[(Int, Int)] = Array((3,4), (7,8))

scala> ar1.unzip
res88: (Array[Int], Array[Int]) = (Array(3, 7),Array(4, 8))

77)update(i,x)将序列中i索引处的元素更新为x

scala> arr1
res89: Array[Any] = Array(hello, world, are, 20, abc)

scala> arr1(3) = 60

scala> arr1
res91: Array[Any] = Array(hello, world, are, 60, abc)

scala> arr1.update(3,80)

scala> arr1
res93: Array[Any] = Array(hello, world, are, 80, abc)

78)updated(i,x)将序列中i索引处的元素更新为x,并返回替换后的数组

scala> arr1.updated(3,100)
res94: Array[Any] = Array(hello, world, are, 100, abc)

scala> arr1
res95: Array[Any] = Array(hello, world, are, 80, abc)

79)view(a,b)返回a到b间的序列,不包括until处的元素

scala> arr1
res96: Array[Any] = Array(hello, world, are, 80, abc)

scala> arr1.view(1,3).foreach(x=>println(x))
world
are

80)zip将两个序列对应位置上的元素组成一个pair序列

scala> t1
res98: Array[String] = Array(world, are)

scala> t2
res99: Array[Any] = Array(are, 20)

scala> t1.zip(t2)
res100: Array[(String, Any)] = Array((world,are), (are,20))

scala> val t3 = Array(1,2,3,4,5)
t3: Array[Int] = Array(1, 2, 3, 4, 5)

scala> t3.zip(t1)
res101: Array[(Int, String)] = Array((1,world), (2,are))

81)zipAll(b,a,b),同zip,但是允许两个序列长度不一样,不足的自动填充,如果当前序列端,空出的填充为a,如果b短,填充为b
scala> t3

res102: Array[Int] = Array(1, 2, 3, 4, 5)

scala> t1
res103: Array[String] = Array(world, are)

scala> t1.zipAll(t3,0,3)
res104: Array[(Any, Int)] = Array((world,1), (are,2), (0,3), (0,4), (0,5))

82)zipWithIndex
序列中的每个元素和它的索引组成一个序列

scala> t3
res105: Array[Int] = Array(1, 2, 3, 4, 5)

scala> t3.zipWithIndex
res106: Array[(Int, Int)] = Array((1,0), (2,1), (3,2), (4,3), (5,4))

83)transform数组中的每个元素加

scala> val n = arr.transform(_+"10")
n: scala.collection.mutable.WrappedArray[String] = WrappedArray(hello10, world10, are10)

84)hasDefiniteSize 检测序列是否存在有限的长度,对应Stream这样的流数据

scala> a
res13: Array[Int] = Array(1, 2, 3)

scala> println(a.hasDefiniteSize)
true

85)类型转换

toArray  转换成Array类型
toBuffer   转换成Buffer类型
toIndexedSeq  转换成IndexedSeq类型
toIterable  转换成可迭代的类型
toIterator  同iterator方法
toList  同List类型
toMap  同Map类型,需要被转化序列中包含的元素是元组2类型数据
toSeq  同Seq类型
toSet  同Set类型
toStream  同Stream类型
toVector  同Vector类型

86)min返回序列中最小的元素
minBy返回序列中第一个不符合条件的元素

scala> val a = Array(3,2,1,4,3,2)
a: Array[Int] = Array(3, 2, 1, 4, 3, 2)

scala> a.minBy(_<3)
res31: Int = 3

scala> a.minBy(_>3)
res32: Int = 3

scala> a.minBy(_>=3)
res33: Int = 2

87)scan把每一步的计算结果放到一个新的集合中返回
scanLeft从左往右计算,scanRight从右往左计算

scala> val a = Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> val b = a.scan(5)(_+_)
b: Array[Int] = Array(5, 6, 8, 11, 15, 20)

88)indices返回当前序列索引集合

scala> val a = Array(1,2,1,4,3)
a: Array[Int] = Array(1, 2, 1, 4, 3)

scala> val b = a.indices
b: scala.collection.immutable.Range = Range(0, 1, 2, 3, 4)

89)size 序列元素个数,同length

scala> a
res36: Array[Int] = Array(1, 2, 1, 4, 3)

scala> a.size
res37: Int = 5

scala> a.length
res43: Int = 5

90)lastIndexOf取得序列中最后一个等于某个元素的位置
lastIndexOfSlice判断当前序列中是否包含另一个序列,并返回最后一次出现该序列的位置处的索引

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

scala> a.lastIndexOf(4,3)
res38: Int = 1

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

scala> val b = Array(1,4)
b: Array[Int] = Array(1, 4)

scala> a.lastIndexOfSlice(b)
res39: Int = 6

91) lastIndexWhere返回当前序列中最后一个满足条件p的元素的索引

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

scala> val b = Array(1,4)
b: Array[Int] = Array(1, 4)

scala> a.lastIndexWhere({x:Int => x<2},2)
res40: Int = 0

92)lastOption返回当前序列中最后一个对象

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

scala> a.lastOption
res41: Option[Int] = Some(5)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值