![](https://i-blog.csdnimg.cn/blog_migrate/da2c03506a16eecb126567f17a80eaa4.png)
Java内置4个标准函数式接口
Supplier<T> =>T get() 提供者
Consumer<T>=>void accept(T t) 消费者
Function<T,R>=> R apply(T t) 函数
Predicate<T> => boolean
Scala
类型转换
var v:xxx=v.toxxx
控制台输入
val reader=new BufferedReader(new InputStreamReader(System.in))
String value=reader.readline()
//添加必要的验证
if(条件){
var v:xxx=value.toxxx
}
元组
var t1=("henry","peppa","sucy")
print(t1._1) 即可得henry
分支代替三元运算符
var v=if(条件) v1 else v2
var g=1
var g= if(g==0) 'f' else 'm'
值守卫
_ 下划线表示一个变量的占位符,无须特殊处理
var g=1
var gen=g match {
case 1=> 'm'
case 0=> 'f'
case _=> 'u'
}print(gen)
if守卫
var score=55
var level=score match {
case x if(x>=90)=>'A'
case x if(x>=80)=>'B'
case x if(x>=70)=>'C'
case _=>'D'
}
print(level)
模式守卫(至少包括一个分组)
val rPhone="(1[3-9]\\d{9})".r
val rPid="(\\d{17}[0-9X])".r
var str="abc@bb.com.co"
var t=str match{
case rPhone(p)=>"phone"
case rPid(p)=>"pid"
case _=>"UNKNOWN"
}
println(t)
val rPhone="(1[3-9]\\d{9})".r
val rPid="(\\d{17}[0-9X])".r
var str="abc@bb.com.co"
var t=str match{
case rPhone(p)=>"phone"
case rPid(p)=>"pid"
case _=>"UNKNOWN"
}
println(t)
val r3="^(.*?);(.*?);(.*?)$".r
var str="beauty,money"
var t:Array[String]=str match {
case r2(a,b)=>Array(a,b)
case r3(a,b,c)=>Array(a,b,c)
case _=>Array()
}
t.foreach(print)
var a:AnyVal=10
var t =a match {
case _:Int=>"int"
case _:Double=>"double"
case _=>"Exception"
} println(t)
sorted对一个集合进行自然排序,通过传递隐式的Ordering,适合单集合的升降序 逆序返回 | val xs=Seq(1,5,3,4,6,2) println(xs.sorted.reverse) 输出List(6, 5, 4, 3, 2, 1) | |
顺序返回 | println(xs.sorted) 输出List(1, 2, 3, 4, 5, 6) | |
sortedBy 对一个属性或多个属性进行排序,通过它的类型,适合对单个或多个属性的排序,代码量比较少,推荐使用这种 逆序返回 | println( xs.sortBy(d=>d).reverse) 输出List(6, 5, 4, 3, 2, 1) | |
顺序返回 | println( xs.sortBy(d=>d)) 输出List(1, 2, 3, 4, 5, 6) | |
Sort with基于函数的排序,通过一个comparator函数,实现自定义排序的逻辑有,适合对单个或多个属性的排序,代码量比较少,推荐使用这种 逆序返回 | println( xs.sortWith(_>_) ) 输出List(6, 5, 4, 3, 2, 1) | |
顺序返回 | println( xs.sortWith(_<_)) 输出List(1, 2, 3, 4, 5, 6) | |
++ | 合并两个序列(序列为空,必须指定泛型) 若序列类型不同以左边序列类型为主; 若泛型类型不同,以两个序列共同的最小父类为结果类型; | |
chatAt | def charAt(index: Int): Char | val a = Array('a','b','c','d') 输出d |
apply | def apply(i: Int): Int | val a = Array(1,2,3,4,5) 输出5 |
aggregate | aggregate是柯里化方法(将原来接受两个参数的函数变成新的接受一个参数的函数的过程。新的函数返回一个以原有第二个参数为参数的函数), aggregate 方法是一个聚合函数,接受多个输入,并按照一定的规则运算以后输出一个结果值 | |
addString | def addString(b: StringBuilder): StringBuilder -【将数组中元素逐个添加到可变字符串序列b中】 | val b=new StringBuilder() val a=Array(1,2,3) val c=a.addString(b)//即a的元素放入b中 println(c) println(b) |
array | def array: Array[Int] | val a = Array(1,2,3,4) 输出4 |
copytoArray | def copyToArray[B >: Int](xs: Array[B]): Unit | val a=Array(1,2,3,4) |
copytoBuffer | def copyToBuffer[B >: Int](dest: scala.collection.mutable.Buffer[B]): Unit | val a=Array(1,2,3,4) 输出1234 |
canEqual | override def canEqual(that: Any): Boolean | val a = Array(1,2,3,4) 输出true |
clone | override def clone(): Array[Char] char和int都可以使用 | val a = Array(1,2,3,4) 输出4 |
collect | def collect[B](pf: PartialFunction[A, B]): Array[B] | val a = Array('a','b','c') 输出B |
collectFirst | def collectFirst[B](pf: PartialFunction[T, B]): Option[B] | val a = Array(12, 'b', 'c') 输出22 |
corresponds | final override def corresponds[B](that: scala.collection.GenSeq[B])(p: (Int, B) => Boolean): Boolean | 检查 a 和 b、c,d长度是否相等,并且 a 中元素是否都小于 b、c 中对应位置的元素 val a=Array(1,2,3) val b=Array(7,8,9) val c=Array(1,2,4) val d=Array(7,8,9,4) print(a.corresponds(b)(_<_))//true print(a.corresponds(c)(_<_))//false print(a.corresponds(d)(_<_))//false |
combinations | def combinations(n: Int): Iterator[Array[String]] | var a=Array('a','b','c','d') var a=Array('a','b','c','d') 输出 a,b,c |
foreach | 遍历序列中的元素 | val a = Array(1,2,3,4,5) 输出10,20,30,40,50 val a = Array(1,2,3,4,5) 输出12345 |
mkString | def mkString: String mkString(start, sep, end) 将序列中所有元素拼接成一个字符串,以 start 开头,以 sep 作为元素间的分隔符,以 end 结尾) | var a=Array('a','b','c','d') 输出abcd var a=Array('a','b','c','d') 输出a!b!c!d var a=Array('a','b','c','d') !a;b;c;d? |
contains | override def contains[A1 >: Int](elem: A1): Boolean | var a=Array('a','b','c','d') 输出true var a=Array('a','b','c','d') 输出false |
containsSlice | def containsSlice[B](that: scala.collection.GenSeq[B]): Boolean | var a=Array(1,2,3,4) var a=Array(1,2,3,4) |
count | def count(p: Int => Boolean): Int | val a=Array(1,2,3) print(a.count(_>2))//输出1,a中>2的数字个数 print(a.count(_>0))//输出3 |
diff | def diff[B >: Int](that: scala.collection.GenSeq[B]): List[Int] | val a=Array(1,2,3) val b=Array(4,5,6) val c=Array(1,2,9) println(a.diff(b).mkString("")) //输出123 println(a.diff(c).mkString(""))//输出3 |
distinct | def distinct: List[Int] | val a=Array(1,2,2,3) 输出123 |
drop | override def drop(n: Int): List[Int] | val a=Array(1,2,2,3) 输出23 |
dropRight | override def dropRight(n: Int): List[Int] | val a=Array(1,2,2,3) 输出1 |
dropWhile | final override def dropWhile(p: Int => Boolean): List[Int] -【从当前数组的第一个元素起,判断是否满足指定条件,直到碰到第一个不满足条件的元素结束(即使后面还有符合条件的元素),删除结束前的所有元素 | val a=Array(1,2,2,3) val b=a.dropWhile(_>2) println(b.mkString(""))//输出1223 val c=a.dropWhile(_==1) println(c.mkString(""))//输出223 val d=a.dropWhile(_<=2) println(d.mkString(""))//输出3 |
endsWith | def endsWith[B](that: scala.collection.GenSeq[B]): Boolean | val a=Array(1,2,2,3) val b=Array(2,3) val c=Array(2,4) print(a.endsWith(b))//ture print(a.endsWith(c))//false |
exists | override def exists(p: Int => Boolean): Boolean | val a=Array(1,2,2,3) print(a.exists(_==1))//true print(a.exists(_>3))//false |
filter | def filter(p: Int => Boolean): List[Int] 筛选数组中符合条件的元素 | val a=Array(1,2,2,3) println(a.filter(_%2==1).mkString(""))//输出13 println(a.filter(_==2).mkString(""))//输出22 |
filterNot | 与filter相反,将不满足条件的元素筛选出来 | val a=Array(1,2,2,3) println(a.filterNot(_%2==1).mkString(""))//输出22 println(a.filterNot(_==2).mkString(""))//输出13 |
flatMap | def flatMap[B](f: (A) ⇒ GenTraversableOnce[B]): Array[B] | |
flatten | def flatten[B](implicit asTraversable: Int => scala.collection.GenTraversableOnce[B]): List[B] | print(a(1)(2))//输出6 print(a.flatten.mkString(""))//输出123456 |
fold | def fold[A1 >: Int](z: A1)(op: (A1, A1) => A1): A1 从左到右计算,对序列中的每个元素进行二元运算,和 aggregate 有类似的语义,但执行过程有所不同 | |
foldLeft | override def foldLeft[B](z: B)(op: (B, Int) => B): B | |
foldRight | override def foldRight[B](z: B)(op: (Int, B) => B): B | |
find | override def find(p: Int => Boolean): Option[Int] | val a=Array(1,2,2,3) println(a.find(_==2).get)//输出2 |
forall | override def forall(p: Int => Boolean): Boolean | val a = Array(4,5,6) println(a.forall(_<7))//true println(a.forall(_==5))//false |
groupBy | def groupBy[K](f: Int => K): scala.collection.immutable.Map[K,List[Int]] | |
grouped | def grouped(size: Int): Iterator[List[Int]] | val a = Array(1,2,3,4,5,6,7,8) 输出123 456 78 |
hasDefiniteSize | def hasDefiniteSize: Boolean | val a = Array(1,2,3,4,5,6,7,8) print(a.hasDefiniteSize) //true print(a.toStream.hasDefiniteSize) //false |
toStream | 将序列转换成 Stream 类型 | |
head | override def head: Int | val a = Array(1,2,3,4,5,6,7,8) print(a.head) //1 |
headOption | def headOption: Option[Int] | val o=ArrayBuffer() val a = Array(1,2,3,4,5,6,7,8) print(a.headOption.get)//1 |
indexOf | def indexOf(elem: T): Int | val a = Array(1,2,3,4,5,6,7,8) print(a.indexOf(3,0))//2 |
indexOfSlice | def indexOfSlice[B >: A](that: GenSeq[B]): Int | val a = Array(1,2,3,4,5,6,7,8) val b = Array(3,4,5) print(a.indexOfSlice(b))//2 |
indexWhere | def indexWhere(p: (T) ⇒ Boolean): Int | val a = Array(1, 2, 3, 4, 5, 6, 7, 8) print(a.indexWhere(_%2==0))//1 //从指定索引处开始,返回当前序列中第一个满足 p 条件的元素的索引 print(a.indexWhere(_>2,3))//3 |
indices | def indices: scala.collection.immutable.Range | val a = Array(1, 2, 3, 4, 5, 6, 7, 8) print(a.indices.mkString(""))//01234567 |
init | def init: List[Int] | val a = Array(1, 2, 3, 4, 5, 6, 7, 8) print(a.init.mkString(""))//1234567 |
inits | def inits: Iterator[List[Int]] | val a = Array(1, 2, 3, 4, 5, 6, 7, 8) 12345678 |
intersect | def intersect[B >: Int](that: scala.collection.GenSeq[B]): List[Int] | val a = Array(1, 2, 3, 4, 5, 6, 7, 8) val b = Array(0,3,5) println(a.intersect(b).mkString(""))//35 |
isDefinedAt | override def isDefinedAt(x: Int): Boolean | val a = Array(1, 2, 3, 4, 5, 6, 7, 8) println(a.isDefinedAt(3))//true println(a.isDefinedAt(10))//false |
isEmpty | override def isEmpty: Boolean | val a = Array(1, 2, 3, 4, 5, 6, 7, 8) println(a.isEmpty)//false val b = new ArrayBuffer() println(b.isEmpty)//true |
isTraversableAgain | final def isTraversableAgain: Boolean | val a = Array(1, 2, 3, 4, 5, 6, 7, 8) println(a.isTraversableAgain)//true println(a.inits.isTraversableAgain)//false |
iterator | override def iterator: Iterator[Int] | val a = Array(1, 2, 3, 4, 5, 6, 7, 8) 换行输出12345678 |
last | def last: T | val a = Array(1, 2, 3, 4, 5, 6, 7, 8) print(a.last)//8 |
lastIndexOf | def lastIndexOf(elem: T): Int | val a = Array(1, 2, 3, 4, 5, 6, 7, 8) //在指定索引end前,返回数组中最后一个等于elem的元素索引 |