1、算数方法
val list = List(1,4,3,5,3)
println("sum="+list.sum) // 16
println("sum="+list.max) // 5
println("sum="+list.min) // 1
println("sum="+list.product) // 180
println(list.reverse) // List(3, 5, 3, 4, 1)
2、分组(返回值:例 Map[String, List[String]])
//分组 Map[String, List[String]] 或 Map[Int, List[Int]]
val res1: Map[Int, List[Int]] = list.groupBy(x=>x)
println(res1) // Map(5 -> List(5), 4 -> List(4), 1 -> List(1), 3 -> List(3, 3))
//res1.foreach(t=>print(t._1+"="+t._2+",")) // 5=List(5),4=List(4),1=List(1),3=List(3, 3),
// 奇偶分类
val res2: Map[Int, List[Int]] = list.groupBy(x=>{x % 2})
res2.foreach(t=>print(t._1+"="+t._2+",")) //1=List(1, 3, 5, 3),0=List(4),
//根据每一个元素的第二位进行分组
val list1 = List("11","12","22","23","34")
val res3: Map[String, List[String]] = list1.groupBy(x=>{x.substring(1)})
println("****************")
res3.foreach(t=>print(t._1+"="+t._2+",")) //2=List(12, 22),1=List(11),4=List(34),3=List(23),
3、排序
//排序(按照指定规则排序) sortBy 升序
val res4: List[Int] = list.sortBy(x=>x)
println(res4) // List(1, 3, 3, 4, 5)
// sortWith 升序 小->大
val res5: List[Int] = list.sortWith((x,y)=>{x<y})
println(res5.mkString(",")) //1,3,3,4,5
//降序 大->小
val res6: List[Int] = list.sortWith((x,y)=>{x>y})
println(res6.mkString(",")) //5,4,3,3,1
val res7: List[String] = list1.sortWith((left, right) => {
left.substring(1) < right.substring(1)
})
println(res7.mkString(",")) //11,12,22,23,34
4、迭代
//迭代
for (elem <- list.iterator) {
println(elem)
}
val res9: List[(Int, Int)] = list.map(x=>{(x,1)})
println(res9.mkString(",")) // (1,1),(4,1),(3,1),(5,1),(3,1)
val res10: Map[Int, List[(Int, Int)]] = res9.groupBy(t=>t._1)
res10.foreach(t=>print(t._1+"="+t._2+",")) //5=List((5,1)),4=List((4,1)),1=List((1,1)),3=List((3,1), (3,1)),
//(k,List) => (k,size)
val res11: Map[Int, Int] = res10.map(t=>{(t._1,t._2.size)})
println(res11.mkString(",")) // 5 -> 1,4 -> 1,1 -> 1,3 -> 2
6、扁平化操作
//扁平化操作(flatMap):将一个整体拆分成一个一个的个体
val list2 = List("rose is beautiful","jennie is beautiful","lisa is beautiful","jisoo is beautiful")
/**
* 将list中的元素按照分隔符这里是空格拆分,然后展开
* 先map(_.split(" "))将每一个元素按照空格拆分
* 然后flatten展开
* flatmap即为上面两个步骤的整合
*/
val res12: List[Array[String]] = list2.map(x=>{x.split(" ")})
val res13: List[String] = res12.flatten
val res14: List[String] = list2.flatMap(_.split(" "))
println(res13) //List(rose, is, beautiful, jennie, is, beautiful, lisa, is, beautiful, jisoo, is, beautiful)
println(res14) //List(rose, is, beautiful, jennie, is, beautiful, lisa, is, beautiful, jisoo, is, beautiful)
val list3: List[Int] = list.filter(x=>x%2 == 1)
print(list3) // List(1, 3, 5, 3)
val list6 = List(("rose is beautiful",4),("tom is beautiful",5))
val tuples: List[(String, Int)] = list6.flatMap(t => {
val words: mutable.ArrayOps[String] = t._1.split(" ")
words.map(x => (x, t._2))
})
print(tuples) //List((rose,4), (is,4), (beautiful,4), (tom,5), (is,5), (beautiful,5))
val list7 = List(1,2,3,4)
println(list7.reduce(_ + _)) // 10 (1+2+3+4)
7、拉链、交并补集
//拉链zip
val list4 = List(2,3,4)
val list5 = List(4,5,6,7)
print(list4.zip(list5)) // List((2,4), (3,5), (4,6))
//交集
print("********")
println(list4.intersect(list5))//List(4)
//并集
print(list4.union(list5))//List(2, 3, 4, 4, 5, 6, 7)
//差集(从A中把B中也有的剔除)
println(list4.diff(list5)) //List(2, 3)
8、reduce
val list = List(1,4,3,5,3)
println(list.reduce(_+_)) //16
println(list.reduceLeft(_+_)) //16
println(list.reduceRight(_+_)) //16
println(list.reduceLeft(_-_)) //-14
println(list.reduceRight(_-_)) // -2
源码:
1)reduce方法底层调用的是reduceLeft
2)reduceLeft源码
var acc: B = 0.asInstanceOf[B] 创建一个起始值相同类型(B)的acc对象
例:println('a'.asInstanceOf[Int]) //97
println(12.asInstanceOf[Int]) //12
3)reduceRight源码
(x, y) => op(y, x) x,y位置互换
例:list.reduceRight(_-_) =》过程:
List(1,4,3,5,3) = 》List(3,5,3,4,1)=》5-3=》3-(5-3)=》4-(3-(5-3))=》1-(4-(3-(5-3))) 小括号内的结果,每次都放在后面一个位置了
9、fold
val list = List(1,4,3,5,3)
println(list.fold(2)(_ + _)) // 18
println(list.foldLeft(2)(_+_)) // 18
println(list.foldRight(2)(_+_)) // 18
println(list.foldLeft(2)(_-_)) // -14
println(list.foldRight(2)(_-_)) // -4
1)fold底层调用foldLeft
2)foldLeft源码
3)foldRight源码
(x, y) => op(y, x) x,y位置互换
例:println(list.foldRight(2)(_-_)) =》过程:
List(1,4,3,5,3) = 》List(3,5,3,4,1)=》1-(4-(3-(5-(3-2)))) 小括号内的结果,每次都放在后面一个位置了
4)应用实例
import scala.collection.mutable
object Common_Method2 {
def main(args: Array[String]): Unit = {
val map1: mutable.Map[Char, Int] = mutable.Map('a'->2,'b'->5,'c'->4)
val map2: mutable.Map[Char, Int] = mutable.Map('a'->3,'b'->4,'d'->3)
val map3: mutable.Map[Char, Int] = map1.foldLeft(map2)((map, t) => {
map(t._1) = map.getOrElse(t._1, 0) + t._2
map
})
println(map3) // Map(b -> 9, d -> 3, a -> 5, c -> 4)
}
}
map2相当于一个初始化值,用map1中的每一个去累加,foldLeft第二个小括号中的第一个参数是累加之后的结果集,传入map类型,和一个(char,Int)元素,返回一个map类型