集合
list
set
tuple
map
访问数据应该是下划线1,2,如hostPort._1。下标是从1开始的。
package cn.chinahadoop.scala class Basic5 { } //默认为val case class Book(name: String, author: String) object Basic5 extends App { // val value = 3 // //match是有返回值的 // val result = value match { // case 1 => "one" // case 2 => "two" // case _ => "some other number" // } // println("result of match is :" + result) // val value = 3 // val result = value match { // case i if i == 1 => "one" // case i if i == 2 => "two" // case _ => "some other number" // // } // println("result of match is : " + result) // def t(obj: Any) = obj match { // case x: Int => println("Int") // case s: String => println("String") // case _ => println("unknown type") // } // t(1) // t("1") // t(1L) // val macTalk = Book("MacTalk", "CJQ") // macTalk match { // case Book(name, author) => println(" this is a book") // case _ => println("unknown") // } // val l = List(1, 2, 3, 4, 5, 6, 7, 8, 9) // val newList = l.map((x: Int) => 2 * x) // val newList2 = l.map((x) => 2 * x) // val newList3 = l.map(x => 2 * x) // println(newList) // println(newList2) // println(newList3) // val newList = l.map(2 * _) // println(newList) var l = List(1, 2, 3, 4, 5, 6); var s = Set(1, 2, 3, 4, 5, 6); val l1 = List(1, 2, 3, 4, 5) val s1 = Set(1, 2, 3); val hostPort = ("localhost", "8080") //访问数组 println(hostPort._1) }
结果:
localhost
package cn.chinahadoop.scala class Basic5 { } //默认为val case class Book(name: String, author: String) object Basic5 extends App { // val value = 3 // //match是有返回值的 // val result = value match { // case 1 => "one" // case 2 => "two" // case _ => "some other number" // } // println("result of match is :" + result) // val value = 3 // val result = value match { // case i if i == 1 => "one" // case i if i == 2 => "two" // case _ => "some other number" // // } // println("result of match is : " + result) // def t(obj: Any) = obj match { // case x: Int => println("Int") // case s: String => println("String") // case _ => println("unknown type") // } // t(1) // t("1") // t(1L) // val macTalk = Book("MacTalk", "CJQ") // macTalk match { // case Book(name, author) => println(" this is a book") // case _ => println("unknown") // } // val l = List(1, 2, 3, 4, 5, 6, 7, 8, 9) // val newList = l.map((x: Int) => 2 * x) // val newList2 = l.map((x) => 2 * x) // val newList3 = l.map(x => 2 * x) // println(newList) // println(newList2) // println(newList3) // val newList = l.map(2 * _) // println(newList) var l = List(1, 2, 3, 4, 5, 6); var s = Set(1, 2, 3, 4, 5, 6); val l1 = List(1, 2, 3, 4, 5) val s1 = Set(1, 2, 3); val hostPort = ("localhost", "8080") //访问数组 println(hostPort._1) println("a" -> "s1") }
结果:
localhost (a,s1)
用"a" -> "b" 创建了一个字符串(a,s1)的字符数组。
package cn.chinahadoop.scala class Basic5 { } //默认为val case class Book(name: String, author: String) object Basic5 extends App { // val value = 3 // //match是有返回值的 // val result = value match { // case 1 => "one" // case 2 => "two" // case _ => "some other number" // } // println("result of match is :" + result) // val value = 3 // val result = value match { // case i if i == 1 => "one" // case i if i == 2 => "two" // case _ => "some other number" // // } // println("result of match is : " + result) // def t(obj: Any) = obj match { // case x: Int => println("Int") // case s: String => println("String") // case _ => println("unknown type") // } // t(1) // t("1") // t(1L) // val macTalk = Book("MacTalk", "CJQ") // macTalk match { // case Book(name, author) => println(" this is a book") // case _ => println("unknown") // } // val l = List(1, 2, 3, 4, 5, 6, 7, 8, 9) // val newList = l.map((x: Int) => 2 * x) // val newList2 = l.map((x) => 2 * x) // val newList3 = l.map(x => 2 * x) // println(newList) // println(newList2) // println(newList3) // val newList = l.map(2 * _) // println(newList) // var l = List(1, 2, 3, 4, 5, 6); // var s = Set(1, 2, 3, 4, 5, 6); // val l1 = List(1, 2, 3, 4, 5) // val s1 = Set(1, 2, 3); // val hostPort = ("localhost", "8080") //访问数组 // println(hostPort._1) // println("a" -> "s1") val m1 = Map("a" -> "b"); println(m1) }
结果:
Map(a -> b)
map的初始化。
package cn.chinahadoop.scala class Basic5 { } //默认为val case class Book(name: String, author: String) object Basic5 extends App { // val value = 3 // //match是有返回值的 // val result = value match { // case 1 => "one" // case 2 => "two" // case _ => "some other number" // } // println("result of match is :" + result) // val value = 3 // val result = value match { // case i if i == 1 => "one" // case i if i == 2 => "two" // case _ => "some other number" // // } // println("result of match is : " + result) // def t(obj: Any) = obj match { // case x: Int => println("Int") // case s: String => println("String") // case _ => println("unknown type") // } // t(1) // t("1") // t(1L) // val macTalk = Book("MacTalk", "CJQ") // macTalk match { // case Book(name, author) => println(" this is a book") // case _ => println("unknown") // } // val l = List(1, 2, 3, 4, 5, 6, 7, 8, 9) // val newList = l.map((x: Int) => 2 * x) // val newList2 = l.map((x) => 2 * x) // val newList3 = l.map(x => 2 * x) // println(newList) // println(newList2) // println(newList3) // val newList = l.map(2 * _) // println(newList) // var l = List(1, 2, 3, 4, 5, 6); // var s = Set(1, 2, 3, 4, 5, 6); // val l1 = List(1, 2, 3, 4, 5) // val s1 = Set(1, 2, 3); // val hostPort = ("localhost", "8080") //访问数组 // println(hostPort._1) // println("a" -> "s1") val m1 = Map("a" -> "b", "c" -> "d"); println(m1) }
结果:
Map(a -> b, c -> d)
集合操作
foreach(类似于map,但是没有返回值)
map(迭代)
filter(过滤)
zip(集合)
partition(列表过滤)
flatten(扁平化)
flatMap(map+flatten)
val m1 = Map("a" -> "b", "c" -> "d", "e" -> "f", "c" -> "f"); println(m1("c"))
结果:
f
在java中key是不能有重复的,但在scala中可以,但如果key重复的话后面的值会覆盖前面的值。
val m1 = Map("a" -> "b", "c" -> "d", "e" -> "f", "c" -> "f"); // println(m1("c")) println(m1.getOrElse("c", 3)); println(m1.getOrElse("cd", 4));
结果:
f 4
val l = List('c', 'd', 'e', 's', 'e', 's', 'e', 's', 'f', 'd', 'f', 's', 'w') println(l.take(1))
结果:
List(c)
foreach用法
val m1 = Map("a" -> "b", "c" -> "d", "e" -> "f", "c" -> "f"); val l = List('c', 'd', 'e', 's', 'e', 's', 'e', 's', 'f', 'd', 'f', 's', 'w') val r1=List(1,23,4,52,2,3) println(l.take(1)) println(r1.map(3 * _)) println(r1)
结果:
List(c) List(3, 69, 12, 156, 6, 9) List(1, 23, 4, 52, 2, 3)
foreach没有返回值
val r1 = List(1, 23, 4, 52, 2, 3) println(r1.foreach(3 * _))
结果:
()
filter的用法 (过滤)
val r1 = List(1, 23, 4, 52, 2, 3) println(r1.filter(x => x % 2 == 0))
结果:
List(4, 52, 2)
zip,聚合,对数值进行匹配
val a = List(1, 2, 3) val b = List(4, 5, 6) println(a.zip(b))
结果:
List((1,4), (2,5), (3,6))
如果没有数值对它进行匹配,就会省略
val a = List(1, 2, 3) val b = List(4, 5, 6,7) println(a.zip(b))
结果:
List((1,4), (2,5), (3,6))
partiton列表分割
val l = List(1, 2, 3, 4, 5, 6, 7, 8) println(l.partition(_ % 2 == 0))
结果:
(List(2, 4, 6, 8),List(1, 3, 5, 7))
flatten(扁平化)
val l=List(List("a","b"),List("c","d")).flatten println(l)
结果:
List(a, b, c, d)
flatMap(map+flatten)
val l = List(List(1, 2), List(3, 4)) println(l.flatMap(x => x.map(_ * 2)))
结果:
List(2, 4, 6, 8)
spark官网中http://spark.apache.org/有出现这个例子
file = spark.textFile("hdfs://...")
file.flatMap(lambda line: line.split())
.map(lambda word: (word, 1))
.reduceByKey(lambda a, b: a+b)
泛型
泛型类
class Pair[T,S](val first: T, val second: S)
泛型方法
def compute[T](list:List[T])=....
隐式转换
位于源目标类型的伴生对象中的隐式函数
位于当前作用域可以以单个标识符指代的隐匿函数
隐式参数
隐式类(scala 2.10)
隐式转换的关键字implicit
package cn.chinahadoop.scala class Basic6 { } class A { } class RichA(a: A) { def rich { println("rich ......") } } object Basic6 extends App { implicit def a_RichA(a: A) = new RichA(a) val a = new A a.rich }
结果:
rich ......
隐式参数
def testParam(implicit name: String) { println(name) } implicit val name = "implicit!!!" testParam
结果:
implicit!!!
def testParam(implicit name: String) { println(name) } implicit val name = "implicit!!!" testParam("chsl")
结果:
chsl
隐式类
implicit class Calculator(x: Int) { def add(a: Int): Int = a + 1 } print(1.add(1))
结果:
2
转载于:https://blog.51cto.com/chenshenglong/1415581