scala集合
集合默认的都是不可变集合
不可变集合 scala.collection.immutable._
可变集合 scala.collection.mutable._
元组TupleN n<=22
2种定义方式:
val tp4:Tuple4[String,...] = new Tuple4(a,b,c,x) // 元组中可以放任意类型的数据
val tp4:(String, Int,...) = ("", "",...)
获取元组中第二个元素: tp4._2
对偶元组: swap交换2个元素的位置
遍历:TupleN.productIterator.foreach
数组
不可变数组Array: 长度不可变,内容可变
val arr = Array[Int](1,3,4)
改变某个元素的值: arr(索引) = 值
可变的数组ArrayBuffer
添加元素:
append
+= (1,2,3)
-=
++= Array(1,2,3)
--=
基本方法:
max
min
reverse 反转scala
序列Seq
不可变List
内容和长度都不可以改变
可变ListBuffer
内容和长度都可以改变
基本操作:
2::3::Nil
head:取第一个值
tail:非第一个值的所有的元素
集Set
可变和不可变
元素不能重复,而且无序
映射Map
可变和不可变
定义方式:
Map[String, Int]("a" -> 1, ("b", 2))
可变Map
put
+=
...
基本操作:
遍历: for((k, v) <- map)
返回map中所有的key: map.keys | keySet
返回map中所有的values: map.values | valuesIterator
val value: Option = map.get(key)
Option
Some(值).get
None
map.getOrElse(key, default:B)
集合上常用的方法 ********
val list = List[String]("1", "2", "3")
***** map 就是对集合中每个元素进行某种函数操作
val nList: List[Int] =list.map(_.toInt)
val list = List[List[Int]](List(1,2,3), List(9,5,2,7))
***** flatMap 他会对集合内部的集合进行一个压平操作
val nList: List[Int] = list.flatMap(arr => arr.map(_*10))
val list = List(1,6,9)
***** filter 过滤集合中符合要求的数据(filter方法的返回值一定要是boolean类型)
list.filter(_ % 2 != 0)
***** zip操作
val l1 = List[Int](1,3,6)
val l2 = List[Int](5,9,0,7)
val ziped = l2.zip(l1) // List[(Int, Int)]((5,1), (9,3), (0, 6))
把ziped里面的元组求个和返回
ziped.map(tp => tp.productIterator.toList.sum)
val l = List[String]("a", "b", "c")
*** mkString将集合转为字符串 foreach (没有返回值)
l.mkString("-")//分隔符 a-b-c
l.foreach(x => println(x))
***** length size sum slice
val l= List("a", "b", "c", .....)
l.slice(l.length-2, l.length)
l.reverse.take(2)
scala集合
集合默认的都是不可变集合
不可变集合 scala.collection.immutable._
可变集合 scala.collection.mutable._
元组TupleN n<=22
2种定义方式:
val tp4:Tuple4[String,...] = new Tuple4(a,b,c,x) // 元组中可以放任意类型的数据
val tp4:(String, Int,...) = ("", "",...)
获取元组中第二个元素: tp4._2
对偶元组: swap交换2个元素的位置
遍历:TupleN.productIterator.foreach
数组
不可变数组Array: 长度不可变,内容可变
val arr = Array[Int](1,3,4)
改变某个元素的值: arr(索引) = 值
可变的数组ArrayBuffer
添加元素:
append
+= (1,2,3)
-=
++= Array(1,2,3)
--=
基本方法:
max
min
reverse 反转scala
序列Seq
不可变List
内容和长度都不可以改变
可变ListBuffer
内容和长度都可以改变
基本操作:
2::3::Nil
head:取第一个值
tail:非第一个值的所有的元素
集Set
可变和不可变
元素不能重复,而且无序
映射Map
可变和不可变
定义方式:
Map[String, Int]("a" -> 1, ("b", 2))
可变Map
put
+=
...
基本操作:
遍历: for((k, v) <- map)
返回map中所有的key: map.keys | keySet
返回map中所有的values: map.values | valuesIterator
val value: Option = map.get(key)
Option
Some(值).get
None
map.getOrElse(key, default:B)
集合上常用的方法 ********
val list = List[String]("1", "2", "3")
***** map 就是对集合中每个元素进行某种函数操作
val nList: List[Int] =list.map(_.toInt)
val list = List[List[Int]](List(1,2,3), List(9,5,2,7))
***** flatMap 他会对集合内部的集合进行一个压平操作
val nList: List[Int] = list.flatMap(arr => arr.map(_*10))
val list = List(1,6,9)
***** filter 过滤集合中符合要求的数据(filter方法的返回值一定要是boolean类型)
list.filter(_ % 2 != 0)
***** zip操作
val l1 = List[Int](1,3,6)
val l2 = List[Int](5,9,0,7)
val ziped = l2.zip(l1) // List[(Int, Int)]((5,1), (9,3), (0, 6))
把ziped里面的元组求个和返回
ziped.map(tp => tp.productIterator.toList.sum)
val l = List[String]("a", "b", "c")
*** mkString foreach
l.mkString("-")//分隔符 a-b-c
l.foreach(x => println(x))
***** length size Sum slice
val l= List("a", "b", "c", .....)
l.slice(l.length-2, l.length)
l.reverse.take(2)
scala集合
集合默认的都是不可变集合
不可变集合 scala.collection.immutable._
可变集合 scala.collection.mutable._
元组TupleN n<=22
2种定义方式:
val tp4:Tuple4[String,...] = new Tuple4(a,b,c,x) // 元组中可以放任意类型的数据
val tp4:(String, Int,...) = ("", "",...)
获取元组中第二个元素: tp4._2
对偶元组: swap交换2个元素的位置
遍历:TupleN.productIterator.foreach
数组
不可变数组Array: 长度不可变,内容可变
val arr = Array[Int](1,3,4)
改变某个元素的值: arr(索引) = 值
可变的数组ArrayBuffer
添加元素:
append
+= (1,2,3)
-=
++= Array(1,2,3)
--=
基本方法:
max
min
reverse 反转scala
序列Seq
不可变List
内容和长度都不可以改变
可变ListBuffer
内容和长度都可以改变
基本操作:
2::3::Nil
head:取第一个值
tail:非第一个值的所有的元素
集Set
可变和不可变
元素不能重复,而且无序
映射Map
可变和不可变
定义方式:
Map[String, Int]("a" -> 1, ("b", 2))
可变Map
put
+=
...
基本操作:
遍历: for((k, v) <- map)
返回map中所有的key: map.keys | keySet
返回map中所有的values: map.values | valuesIterator
val value: Option = map.get(key)
Option
Some(值).get
None
map.getOrElse(key, default:B)
集合上常用的方法 ********
val list = List[String]("1", "2", "3")
***** map 就是对集合中每个元素进行某种函数操作
val nList: List[Int] =list.map(_.toInt)
val list = List[List[Int]](List(1,2,3), List(9,5,2,7))
***** flatMap 他会对集合内部的集合进行一个压平操作
val nList: List[Int] = list.flatMap(arr => arr.map(_*10))
val list = List(1,6,9)
***** filter 过滤集合中符合要求的数据(filter方法的返回值一定要是boolean类型)
list.filter(_ % 2 != 0)
***** zip操作
val l1 = List[Int](1,3,6)
val l2 = List[Int](5,9,0,7)
val ziped = l2.zip(l1) // List[(Int, Int)]((5,1), (9,3), (0, 6))
把ziped里面的元组求个和返回
ziped.map(tp => tp.productIterator.toList.sum)
val l = List[String]("a", "b", "c")
*** mkString foreach
l.mkString("-")//分隔符 a-b-c
l.foreach(x => println(x))
***** length size Sum slice
val l= List("a", "b", "c", .....)
l.slice(l.length-2, l.length)
l.reverse.take(2)
scala集合
集合默认的都是不可变集合
不可变集合 scala.collection.immutable._
可变集合 scala.collection.mutable._
元组TupleN n<=22
2种定义方式:
val tp4:Tuple4[String,...] = new Tuple4(a,b,c,x) // 元组中可以放任意类型的数据
val tp4:(String, Int,...) = ("", "",...)
获取元组中第二个元素: tp4._2
对偶元组: swap交换2个元素的位置
遍历:TupleN.productIterator.foreach
数组
不可变数组Array: 长度不可变,内容可变
val arr = Array[Int](1,3,4)
改变某个元素的值: arr(索引) = 值
可变的数组ArrayBuffer
添加元素:
append
+= (1,2,3)
-=
++= Array(1,2,3)
--=
基本方法:
max
min
reverse 反转scala
序列Seq
不可变List
内容和长度都不可以改变
可变ListBuffer
内容和长度都可以改变
基本操作:
2::3::Nil
head:取第一个值
tail:非第一个值的所有的元素
集Set
可变和不可变
元素不能重复,而且无序
映射Map
可变和不可变
定义方式:
Map[String, Int]("a" -> 1, ("b", 2))
可变Map
put
+=
...
基本操作:
遍历: for((k, v) <- map)
返回map中所有的key: map.keys | keySet
返回map中所有的values: map.values | valuesIterator
val value: Option = map.get(key)
Option
Some(值).get
None
map.getOrElse(key, default:B)
集合上常用的方法 ********
val list = List[String]("1", "2", "3")
***** map 就是对集合中每个元素进行某种函数操作
val nList: List[Int] =list.map(_.toInt)
val list = List[List[Int]](List(1,2,3), List(9,5,2,7))
***** flatMap 他会对集合内部的集合进行一个压平操作
val nList: List[Int] = list.flatMap(arr => arr.map(_*10))
val list = List(1,6,9)
***** filter 过滤集合中符合要求的数据(filter方法的返回值一定要是boolean类型)
list.filter(_ % 2 != 0)
***** zip操作
val l1 = List[Int](1,3,6)
val l2 = List[Int](5,9,0,7)
val ziped = l2.zip(l1) // List[(Int, Int)]((5,1), (9,3), (0, 6))
把ziped里面的元组求个和返回
ziped.map(tp => tp.productIterator.toList.sum)
val l = List[String]("a", "b", "c")
*** mkString foreach
l.mkString("-")//分隔符 a-b-c
l.foreach(x => println(x))
***** length size Sum slice
val l= List("a", "b", "c", .....)
l.slice(l.length-2, l.length)
l.reverse.take(2)
//take()方法
val arr = Array(1,2,3,4,5)
arr: Array[Int] = Array(1, 2, 3, 4, 5)
arr.reverse.take(2) //先反转再取值
Array[Int] = Array(5, 4)
****sorted, ***** sortBy(根据什么进行排序), ***sortWith
val l = List(("a", 1, 2), ("c", 1, 1), ("b", 1, 3))
l.sortBy(_._3) //升序
l.sortBy(-_._3) //降序
***** groupBy grouped
val ll = List(("a",1),("b",2),("c",2),("b",2))
/**
* Map(b -> List((b,2), (b,2)), a -> List((a,1)), c -> List((c,2)))
* val map = ll.groupBy(_._1)
*/
println( ll.groupBy(_._1).mapValues(list => list.map(tp => tp._2).sum))
//结果集:Map(b -> 4, a -> 1, c -> 2)
val wordsArray = Array("hello tom hello jim hello jerry","hello sheep hello tom hello xiaomage")
****sorted, ***** sortBy, ***sortWith
val l = List(("a", 1, 2), ("c", 1, 1), ("b", 1, 3))
l.sortBy(_._3)
***** groupBy grouped
val l = List(("a",1), ("b", 2), ("b", 2))
l.groupBy(_._1).mapValues(list => list.map(tp => tp._2).sum)
val wordsArray = Array("hello tom hello jim hello jerry","hello sheep hello tom hello xiaomage")
****sorted, ***** sortBy, ***sortWith
val l = List(("a", 1, 2), ("c", 1, 1), ("b", 1, 3))
l.sortBy(_._3)
***** groupBy grouped
val l = List(("a",1), ("b", 2), ("b", 2))
l.groupBy(_._1).mapValues(list => list.map(tp => tp._2).sum)
val wordsArray = Array("hello tom hello jim hello jerry","hello sheep hello tom hello xiaomage")
****sorted, ***** sortBy, ***sortWith
val l = List(("a", 1, 2), ("c", 1, 1), ("b", 1, 3))
l.sortBy(_._3)
***** groupBy grouped
val l = List(("a",1), ("b", 2), ("b", 2))
l.groupBy(_._1).mapValues(list => list.map(tp => tp._2).sum)
Scala代码写 wordcount
val wordsArray = Array("hello tom hello jim hello jerry","hello sheep hello tom hello xiaomage")
scala> wordsArray.map(str => str.split(" "))
res0: Array[Array[String]] = Array(Array(hello, tom, hello, jim, hello, jerry), Array(hello, sheep, hello, tom, hello, xiaomage))
scala> res0.map(arr => arr.map(word => (word,1)))
res1: Array[Array[(String, Int)]] = Array(Array((hello,1), (tom,1), (hello,1), (jim,1), (hello,1), (jerry,1)), Array((hello,1), (sheep,1), (hello,1), (tom,1), (hello,1), (xiaomage,1)))
scala> res0.flatMap(arr => arr.map(word => (word,1)))
res2: Array[(String, Int)] = Array((hello,1), (tom,1), (hello,1), (jim,1), (hello,1), (jerry,1), (hello,1), (sheep,1), (hello,1), (tom,1), (hello,1), (xiaomage,1))
scala> res2.groupBy(_._1)
res3: scala.collection.immutable.Map[String,Array[(String, Int)]] = Map(sheep -> Array((sheep,1)), tom -> Array((tom,1), (tom,1)), jim -> Array((jim,1)), hello -> Array((hello,1), (hello,1), (hello,1), (hello,1), (hello,1), (hello,1)), jerry -> Array((jerry,1)), xiaomage -> Array((xiaomage,1)))
scala> res3.mapValues(_.size)
res5: scala.collection.immutable.Map[String,Int] = Map(sheep -> 1, tom -> 2, jim -> 1, hello -> 6, jerry -> 1, xiaomage -> 1)
scala> res5.toList
res6: List[(String, Int)] = List((sheep,1), (tom,2), (jim,1), (hello,6), (jerry,1), (xiaomage,1))
scala> res6.sortBy(_._2) //升序排序
res7: List[(String, Int)] = List((sheep,1), (jim,1), (jerry,1), (xiaomage,1), (tom,2), (hello,6))
scala> res6.sortBy(-_._2) //降序排序
res8: List[(String, Int)] = List((hello,6), (tom,2), (sheep,1), (jim,1), (jerry,1), (xiaomage,1))