Hello Scala 1. 基本数据类型练习

  • 1.0 启动Scala

mac $ ./scala
cat: /release: No such file or directory
Welcome to Scala 2.12.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_112).
Type in expressions for evaluation. Or try :help.
  • 2.0 基本数据类型
scala> val i =1
i: Int = 1

scala> val s = "hello"
s: String = hello

scala> val str: String = "itcast"
str: String = itcast

scala> val x = 1;
x: Int = 1

scala> val y = if (x > 0) 1 else -1
y: Int = 1

scala> println(y)
1
scala> val z = if (x > 2) 1 else "error"
z: Any = error

scala> val z = if (x > 1) 1 else "error"
z: Any = error

scala> val z = if (x > 0) 1 else "error"
z: Any = 1

scala> val m = if (x >2) 1
m: AnyVal = ()

scala> val n = if (x > 2) 1 else ()
n: AnyVal = ()

scala> val k = if (x < 0) 0
k: AnyVal = ()

scala> val k = if (x < 0) 0 else if (x >=1) 1 else -1
k: Int = 1

scala> val x =0
x: Int = 0

scala> val result = {
     | if (x < 0) {
     | -1
     | }
     | else if (x >=1) {
     | 1 
     | } else {
     | "error"
     | }
     | }
result: Any = error

scala> for (i <- 1 to 10)
     | println(i)
1
2
3
4
5
6
7
8
9
10

scala> for (i<-1 to 2)
     | print(i)
12
scala> val arr = Array("a", "b", "c")
arr: Array[String] = Array(a, b, c)

scala> for (i <- arr)
     | print i
<console>:14: error: missing argument list for method print in object Predef
Unapplied methods are only converted to functions when a function type is expected.
You can make this conversion explicit by writing `print _` or `print(_)` instead of `print`.
       print i
       ^
scala> print(i)
1
scala> i
res43: Int = 1

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

scala> for (i <-arr)
     | println(i)
a
b
c

scala> for(i <- 1 to 3; j <- 1 to 3 if i!=j)
     | print ((10 *i +j) + " ")
12 13 21 23 31 32 
scala> 

scala> val v = for(i <- 1 to 10) yield i * 10
v: scala.collection.immutable.IndexedSeq[Int] = Vector(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)

scala> val v = for(i <- 1 to 10) i *10
v: Unit = ()

scala> val a = 1
a: Int = 1

scala> val b = 3
b: Int = 3

scala> a +b
res47: Int = 4

scala> a.+(b)
res48: Int = 4

scala> def m1(x: Int, y: Int) : Int = x*y
m1: (x: Int, y: Int)Int

scala> m1(3 , 9)
res49: Int = 27

scala> val f1 = (x: Int, y: Int) => x +y
f1: (Int, Int) => Int = $$Lambda$1478/866199557@5b897f0

scala> f1(2, 8)
res50: Int = 10

scala> def m2(f: (Int, Int) => Int) = f(2, 6)
m2: (f: (Int, Int) => Int)Int

scala> val f2 = (x : Int, y: Int) => x -y
f2: (Int, Int) => Int = $$Lambda$1479/1002210819@78e10901

scala> m2(f2)
res51: Int = -4

scala> def m1(f: (Int, Int) => Int) : Int = {}
<console>:11: error: type mismatch;
 found   : Unit
 required: Int
       def m1(f: (Int, Int) => Int) : Int = {}
                                            ^

scala> def m1(f: (Int, Int) => Int) : Int = { f(2, 6)}
m1: (f: (Int, Int) => Int)Int

scala> val f1 = (x: Int, y : Int) => x +y
f1: (Int, Int) => Int = $$Lambda$1481/876210403@550267a1

scala> val f2 = (m: Int, n: Int) => m * n
f2: (Int, Int) => Int = $$Lambda$1485/2072971801@31461d7c

scala> val r2 = m2(f2)
r2: Int = 12

scala> def m1(x: Int, y: Int) : Int = x* y
m1: (x: Int, y: Int)Int

scala> val f1 = m1 _
f1: (Int, Int) => Int = $$Lambda$1489/474568428@213562e7

scala> val arr1 = new Array[Int](8)
arr1: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0)

scala> println(arr1)
[I@53955236

scala> arr1
res55: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0)

scala> print(arr1)
[I@53955236

scala> 

scala> println(arr1.toBuffer)
ArrayBuffer(0, 0, 0, 0, 0, 0, 0, 0)

scala> println(arr1)
[I@53955236

scala> val arr2 = Array[Int](10)
arr2: Array[Int] = Array(10)

scala> println(arr2)
[I@4bd15a8f

scala> println(arr2.toBuffer)
ArrayBuffer(10)

scala> arr2
res67: Array[Int] = Array(10)

scala> arr1
res68: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0)

scala> val arrs3 = Array("hadoop", "storm", "spark")
arrs3: Array[String] = Array(hadoop, storm, spark)

scala> println(arrs3(2))
spark

scala> val ab = ArrayBuffer[Int] ()
<console>:11: error: not found: value ArrayBuffer
       val ab = ArrayBuffer[Int] ()
                ^

scala> import scala
   final package scala

scala> import scala
     | ._
import scala._

scala> val ab = ArrayBuffer[Int]()
<console>:14: error: not found: value ArrayBuffer
       val ab = ArrayBuffer[Int]()
                ^

scala> import scala.collection.mutable._
import scala.collection.mutable._

scala> val ab = ArrayBuffer[Int]()
ab: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

scala> ab
res70: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()

scala> ab += 1
res71: ab.type = ArrayBuffer(1)

scala> ab += (2, 3, 4, 5)
res72: ab.type = ArrayBuffer(1, 2, 3, 4, 5)

scala> ab ++= Array(3,9)
res73: ab.type = ArrayBuffer(1, 2, 3, 4, 5, 3, 9)

scala> ab ++= ArrayBuffer(6, 12)
res74: ab.type = ArrayBuffer(1, 2, 3, 4, 5, 3, 9, 6, 12)

scala> ab.insert(0, -1 ,0 , 10)

scala> ab
res76: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(-1, 0, 10, 1, 2, 3, 4, 5, 3, 9, 6, 12)

scala> ab.insert(2, 12, 11, 9)

scala> ab
res78: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(-1, 0, 12, 11, 9, 10, 1, 2, 3, 4, 5, 3, 9, 6, 12)

scala> 

scala> ab.remove(2, 2)

scala> ab
res80: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(-1, 0, 9, 10, 1, 2, 3, 4, 5, 3, 9, 6, 12)

scala> ab.remove(3, 2)

scala> ab
res82: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(-1, 0, 9, 2, 3, 4, 5, 3, 9, 6, 12)

scala> 0 until 10
res84: scala.collection.immutable.Range = Range 0 until 10

scala> res84
res85: scala.collection.immutable.Range = Range 0 until 10

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

scala> for(i <- arr)
     | print(i)
13456892
scala> for(i <- (arr.length to 0))
     | print(arr[i])
<console>:20: error: not found: type i
       print(arr[i])
                 ^
<console>:20: error: value arr of type Array[Int] does not take type parameters.
       print(arr[i])
                ^

scala> 9 to 0
res88: scala.collection.immutable.Range.Inclusive = empty Range 9 to 0

scala> for(i <- 9 to 2)
     | println(i)

scala> for(i <- 9 to 13)
     | println(i)
9
10
11
12
13

scala> for(i <- (0 until arr.length).reverse)
     | println(arr(i))
2
9
8
6
5
4
3
1

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

scala> val res = for(e <- arr) yield e *2
res: Array[Int] = Array(4, 2, 6, 8, 10, 18, 14)

scala> arr.map(_ * 2)
res92: Array[Int] = Array(4, 2, 6, 8, 10, 18, 14)

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

scala> val res = for(e <- arr if e % 2 == 0) yield e * 10
res: Array[Int] = Array(20, 60, 80)

scala> println(res.toBuffer)
ArrayBuffer(20, 60, 80)

scala> val r = arr.filter(_ % 2 == 0).map(_ * 10)
r: Array[Int] = Array(20, 60, 80)

scala> val r = arr.filter(_ % 2 ==0)
r: Array[Int] = Array(2, 6, 8)

scala> 

scala> 

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

scala> arr.sum
res94: Int = 25

scala> arr.max
res95: Int = 9

scala> arr.sorted
res96: Array[Int] = Array(1, 2, 3, 5, 5, 9)

scala> 

scala> val scores = Map("tom" -> 85, "jerry" -> 99, "kitty" -> 90)
scores: scala.collection.mutable.Map[String,Int] = Map(tom -> 85, kitty -> 90, jerry -> 99)

scala> val scores = Map(("tom", 96),("herry", 87), ("tom",91))
scores: scala.collection.mutable.Map[String,Int] = Map(herry -> 87, tom -> 91)

scala> scores("tom")
res97: Int = 91

scala> scores("rose")
java.util.NoSuchElementException: key not found: rose
  at scala.collection.MapLike.default(MapLike.scala:232)
  at scala.collection.MapLike.default$(MapLike.scala:231)
  at scala.collection.AbstractMap.default(Map.scala:59)
  at scala.collection.mutable.HashMap.apply(HashMap.scala:65)
  ... 29 elided

scala> scores.getOrElse("rose", 12)
res99: Int = 12

scala> import scala.collection.mutable.Map
import scala.collection.mutable.Map

scala> val scores = Map("tom" -> 80, "jerry" -> 90)
scores: scala.collection.mutable.Map[String,Int] = Map(tom -> 80, jerry -> 90)

scala> scores("tom")
res100: Int = 80

scala> scores("tom") = 88

scala> scores
res102: scala.collection.mutable.Map[String,Int] = Map(tom -> 88, jerry -> 90)

scala> scores += ("kitty" -> 99)
res103: scores.type = Map(tom -> 88, kitty -> 99, jerry -> 90)

scala> scores += ("kitty" -> 99)
res104: scores.type = Map(tom -> 88, kitty -> 99, jerry -> 90)

scala> scores += ("jerry" -> 99)
res105: scores.type = Map(tom -> 88, kitty -> 99, jerry -> 99)

scala> scores += (("rose", 80))
res106: scores.type = Map(rose -> 80, tom -> 88, kitty -> 99, jerry -> 99)

scala> val t = ("hadoop", 3.14, 678)
t: (String, Double, Int) = (hadoop,3.14,678)

scala> val t(a,b,c) = ("hadoop", 3.13, 4566)
<console>:21: error: value t is not a case class, nor does it have an unapply/unapplySeq member
       val t(a,b,c) = ("hadoop", 3.13, 4566)
           ^

scala> val t,(a,b,c) = ("hadoop", 3.13, 4566)
t: (String, Double, Int) = (hadoop,3.13,4566)
a: String = hadoop
b: Double = 3.13
c: Int = 4566

scala> val r1 =t._1
r1: String = hadoop

scala> val r1 =t.1
<console>:1: error: ';' expected but double literal found.
val r1 =t.1
         ^

scala> val r2 = t._2
r2: Double = 3.13

scala> val arr = Array(("tom", 98), ("jerry", 90))
arr: Array[(String, Int)] = Array((tom,98), (jerry,90))

scala> arr.toMap
res107: scala.collection.immutable.Map[String,Int] = Map(tom -> 98, jerry -> 90)

scala> val scores = Array(88, 95, 80)
scores: Array[Int] = Array(88, 95, 80)

scala> val ns = names.zip(scores)
<console>:19: error: not found: value names
       val ns = names.zip(scores)
                ^

scala> val names = Array("jim", "tom", "herry")
names: Array[String] = Array(jim, tom, herry)

scala> val ns = names.zip(scores)
ns: Array[(String, Int)] = Array((jim,88), (tom,95), (herry,80))

scala> ns.toMap
res108: scala.collection.immutable.Map[String,Int] = Map(jim -> 88, tom -> 95, herry -> 80)
  • List
scala> val l1 = List(2,3,1)
l1: List[Int] = List(2, 3, 1)

scala> val l2 = 0 :: l1
l2: List[Int] = List(0, 2, 3, 1)

scala> val l3 = l1.::(0)
l3: List[Int] = List(0, 2, 3, 1)

scala> val l4 = 0 +: l1
l4: List[Int] = List(0, 2, 3, 1)

scala> val l5 = l1.+:(0)
l5: List[Int] = List(0, 2, 3, 1)

scala> val l6 = l1 :+ 3
l6: List[Int] = List(2, 3, 1, 3)

scala> val l0 = List(5,4,6)
l0: List[Int] = List(5, 4, 6)

scala> val l7 = l1 ++ l0
l7: List[Int] = List(2, 3, 1, 5, 4, 6)

scala> val l8 = l1 ++: l0
l8: List[Int] = List(2, 3, 1, 5, 4, 6)

scala> val l8 = l1 :++ l0
<console>:20: error: value :++ is not a member of List[Int]
       val l8 = l1 :++ l0
                   ^

scala> val l9 = l1.:::(l0)
l9: List[Int] = List(5, 4, 6, 2, 3, 1)

scala> val l9 = l1 +++ l0
<console>:20: error: value +++ is not a member of List[Int]
       val l9 = l1 +++ l0
                   ^

scala> println(l9)
List(5, 4, 6, 2, 3, 1)

scala> import scala.collection.mutable._
import scala.collection.mutable._

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

scala> l1
res110: List[Int] = List(2, 3, 1)

scala> val l1 = new ListBuffer[Int]
l1: scala.collection.mutable.ListBuffer[Int] = ListBuffer()

scala> l1 += 4
res111: l1.type = ListBuffer(4)

scala> l1
res112: scala.collection.mutable.ListBuffer[Int] = ListBuffer(4)

scala> l1.append(5)

scala> l1
res114: scala.collection.mutable.ListBuffer[Int] = ListBuffer(4, 5)

scala> l0
res115: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 3, 2)

scala> l0 ++= l1
res116: l0.type = ListBuffer(1, 3, 2, 4, 5)

scala> l0
res117: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 3, 2, 4, 5)

scala> l1
res118: scala.collection.mutable.ListBuffer[Int] = ListBuffer(4, 5)

scala> val l2 = l0 +l1
<console>:23: error: type mismatch;
 found   : scala.collection.mutable.ListBuffer[Int]
 required: String
       val l2 = l0 +l1
                    ^

scala> val l2 = l0 + l1
<console>:23: error: type mismatch;
 found   : scala.collection.mutable.ListBuffer[Int]
 required: String
       val l2 = l0 + l1
                     ^

scala> val l2 = l0 ++ l1
l2: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 3, 2, 4, 5, 4, 5)

scala> val l3 = l0 :+ 5
l3: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 3, 2, 4, 5, 5)

scala> val l3 = 5 :+ l0
<console>:22: error: value :+ is not a member of Int
       val l3 = 5 :+ l0
                  ^
  • Set
scala> val set1 = new HashSet[Int]()
set1: scala.collection.mutable.HashSet[Int] = Set()

scala> val set2 = set1 +4
set2: scala.collection.mutable.HashSet[Int] = Set(4)

scala> val set3 = set1 ++ Set(6,5,7)
set3: scala.collection.mutable.HashSet[Int] = Set(5, 6, 7)

scala> val set0 = Set(3,1,4) ++ set1
set0: scala.collection.mutable.Set[Int] = Set(1, 3, 4)

scala> println(set0.getClass)
class scala.collection.mutable.HashSet

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

scala> val s1 = new mutable.HashSet[Int]() 
s1: scala.collection.mutable.HashSet[Int] = Set()

scala> s1 += 2
res120: s1.type = Set(2)

scala> s1.add(4)
res121: Boolean = true

scala> s
res122: String = hello

scala> s1
res123: scala.collection.mutable.HashSet[Int] = Set(2, 4)

scala> a1 ++= Set(3, 1, 5)
<console>:23: error: not found: value a1
       a1 ++= Set(3, 1, 5)
       ^

scala> s1 ++= Set(3, 1, 5)
res125: s1.type = Set(1, 5, 2, 3, 4)

scala> println(s1)
Set(1, 5, 2, 3, 4)

scala> s1
res127: scala.collection.mutable.HashSet[Int] = Set(1, 5, 2, 3, 4)

scala> s1 ++= Set(10, 15, 12)
res128: s1.type = Set(15, 12, 1, 5, 2, 3, 10, 4)

scala> s1 -= 5
res129: s1.type = Set(15, 12, 1, 2, 3, 10, 4)

scala> s1 ++= Set(15)
res130: s1.type = Set(15, 12, 1, 2, 3, 10, 4)

scala> s1.remove(2)
res131: Boolean = true

scala> s1
res132: scala.collection.mutable.HashSet[Int] = Set(15, 12, 1, 3, 10, 4)

scala> println(s1)
Set(15, 12, 1, 3, 10, 4)

  • Map

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

scala> 

scala> val m1 = new mutable.HashMap[String, Int]()
m1: scala.collection.mutable.HashMap[String,Int] = Map()

scala> m1("spark") = 1

scala> m1 += (("hadoop", 2))
res135: m1.type = Map(hadoop -> 2, spark -> 1)

scala> m1 += ("hive" -> 3)
res136: m1.type = Map(hadoop -> 2, spark -> 1, hive -> 3)

scala> m1.put("storm",3)
res137: Option[Int] = None

scala> m1
res138: scala.collection.mutable.HashMap[String,Int] = Map(hadoop -> 2, spark -> 1, hive -> 3, storm -> 3)

scala> println(m1)
Map(hadoop -> 2, spark -> 1, hive -> 3, storm -> 3)

scala> m1-="spark"
res140: m1.type = Map(hadoop -> 2, hive -> 3, storm -> 3)

scala> m1.remove("hadoop")
res141: Option[Int] = Some(2)

scala> m1
res142: scala.collection.mutable.HashMap[String,Int] = Map(hive -> 3, storm -> 3)

scala> println(m1)
Map(hive -> 3, storm -> 3)

scala> 
  • Test
scala> val l0 = List(1,2,5,7,8,9,12)
l0: List[Int] = List(1, 2, 5, 7, 8, 9, 12)

scala> val l1 = l0.map(_ * 10)
l1: List[Int] = List(10, 20, 50, 70, 80, 90, 120)

scala> val l2 = l0.filter(_ %2 == 0)
l2: List[Int] = List(2, 8, 12)

scala> l0.sorted
res1: List[Int] = List(1, 2, 5, 7, 8, 9, 12)

scala> l0.add(3,12,5,30)
<console>:13: error: value add is not a member of List[Int]
       l0.add(3,12,5,30)
          ^
scala> l0
res4: List[Int] = List(1, 2, 5, 7, 8, 9, 12)

scala> val l0 = List(6,3,2,1,7,8,9)
l0: List[Int] = List(6, 3, 2, 1, 7, 8, 9)

scala> l0.sorted
res5: List[Int] = List(1, 2, 3, 6, 7, 8, 9)

scala> l0.sorted.reverse
res7: List[Int] = List(9, 8, 7, 6, 3, 2, 1)

scala> l0.grouped(4)
res8: Iterator[List[Int]] = non-empty iterator

scala> l0.grouped(4).toList
res9: List[List[Int]] = List(List(6, 3, 2, 1), List(7, 8, 9))

scala> res8
res10: Iterator[List[Int]] = non-empty iterator

scala> res8.map
<console>:13: error: missing argument list for method map in trait Iterator
Unapplied methods are only converted to functions when a function type is expected.
You can make this conversion explicit by writing `map _` or `map(_)` instead of `map`.
       res8.map
            ^

scala> res8.map(_)
<console>:13: error: missing parameter type for expanded function ((x$1: <error>) => res8.map(x$1))
       res8.map(_)
                ^

scala> res8.map(x => x)
res13: Iterator[List[Int]] = non-empty iterator

scala> res9
res14: List[List[Int]] = List(List(6, 3, 2, 1), List(7, 8, 9))

scala> val lines = List("hello tom hello jerry", "hello jerry", "hell kitty")
lines: List[String] = List(hello tom hello jerry, hello jerry, hell kitty)

scala> val l1 = lines.map(_.split(" "))
l1: List[Array[String]] = List(Array(hello, tom, hello, jerry), Array(hello, jerry), Array(hell, kitty))

scala> val l1 = lines.map(_.split(" ")).toList
l1: List[Array[String]] = List(Array(hello, tom, hello, jerry), Array(hello, jerry), Array(hell, kitty))

scala> val l1 = lines.map(_.split(" "))
l1: List[Array[String]] = List(Array(hello, tom, hello, jerry), Array(hello, jerry), Array(hell, kitty))

scala> val l1 = lines.map(_.split(" ").toList)
l1: List[List[String]] = List(List(hello, tom, hello, jerry), List(hello, jerry), List(hell, kitty))

scala> val l1 = lines.map(_.split(" ").toList).toList
l1: List[List[String]] = List(List(hello, tom, hello, jerry), List(hello, jerry), List(hell, kitty))

scala> val l1 = lines.map(_.split(" ").toList).reduce
<console>:12: error: missing argument list for method reduce in trait TraversableOnce
Unapplied methods are only converted to functions when a function type is expected.
You can make this conversion explicit by writing `reduce _` or `reduce(_)` instead of `reduce`.
       val l1 = lines.map(_.split(" ").toList).reduce
                                               ^

scala> val l1 = lines.map(_.split(" ").toList).reduce((_, 1))
<console>:12: error: missing parameter type for expanded function ((x$2: <error>) => scala.Tuple2(x$2, 1))
       val l1 = lines.map(_.split(" ").toList).reduce((_, 1))
                                                       ^

scala> lines.flatMap(_.split(" "))
res15: List[String] = List(hello, tom, hello, jerry, hello, jerry, hell, kitty)

scala> lines.flatMap(_.split(" ")).map((_, 1))
res16: List[(String, Int)] = List((hello,1), (tom,1), (hello,1), (jerry,1), (hello,1), (jerry,1), (hell,1), (kitty,1))

scala> lines.flatMap(_.split(" ")).map((_, 1)).groupBy(_._1)
res17: scala.collection.immutable.Map[String,List[(String, Int)]] = Map(kitty -> List((kitty,1)), hell -> List((hell,1)), tom -> List((tom,1)), hello -> List((hello,1), (hello,1), (hello,1)), jerry -> List((jerry,1), (jerry,1)))

scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).ma
map   mapValues   max   maxBy
                             
scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).ma                  p
par   partition   product

scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).ma                  p
par   partition   product

scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).ma                  p
par   partition   product

scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).ma                   v 
Display all 407 possibilities? (y or n)

scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues
 v 
override def mapValues[W](f: List[(String, Int)] => W): scala.collection.immutable.Map[String,W]

scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues

override def mapValues[W](f: List[(String, Int)] => W): scala.collection.immutable.Map[String,W]

scala> lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2))
res26: scala.collection.immutable.Map[String,Int] = Map(kitty -> 1, hell -> 1, tom -> 1, hello -> 3, jerry -> 2)

scala> lines.flatMap(_.split(" ")).map((_, 1)).groupBy(_._1).map(t => (t._1, t._2.size))
res27: scala.collection.immutable.Map[String,Int] = Map(kitty -> 1, hell -> 1, tom -> 1, hello -> 3, jerry -> 2)

scala> lines.flatMap(_.split(" ")).map((_, 1)).groupBy(_._1).map(t => (t._1, t._2.size)).toList
res28: List[(String, Int)] = List((kitty,1), (hell,1), (tom,1), (hello,3), (jerry,2))

scala> lines.flatMap(_.split(" ")).map((_, 1)).groupBy(_._1).map(t => (t._1, t._2.size)).toList.sortedBy(_._2)
<console>:13: error: value sortedBy is not a member of List[(String, Int)]
       lines.flatMap(_.split(" ")).map((_, 1)).groupBy(_._1).map(t => (t._1, t._2.size)).toList.sortedBy(_._2)
                                                                                                ^

scala> lines.flatMap(_.split(" ")).map((_, 1)).groupBy(_._1).map(t => (t._1, t._2.size)).toList.sortBy(_._2)
res30: List[(String, Int)] = List((kitty,1), (hell,1), (tom,1), (jerry,2), (hello,3))

scala> lines.flatMap(_.split(" ")).map((_, 1)).groupBy(_._1).map(t => (t._1, t._2.size)).toList.sortBy(_._2).reverse
res31: List[(String, Int)] = List((hello,3), (jerry,2), (tom,1), (hell,1), (kitty,1))

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,254评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,875评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,682评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,896评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,015评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,152评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,208评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,962评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,388评论 1 304
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,700评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,867评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,551评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,186评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,901评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,142评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,689评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,757评论 2 351

推荐阅读更多精彩内容