基础环境
jdk1.8
scala 2.11.12
流程控制
package com.ww.syntax
/**
* 流程控制
*/
object syntax01_if_for_while {
def main(args: Array[String]): Unit = {
// if
var a = 2
if(a>0){
print(s"a>0 : ${a}")
}else{
print(s"a<=0 : ${a}")
}
// while
var b = 3
while (b<10){
println(b)
b+=1
}
//for
println("===========for============")
var seqs = 1 to (10,2)
println(seqs)
var seqs1 = 1 until 10
println(seqs1)
for(i <- seqs){
println(i)
}
for(i <- seqs1 if(i%2==0)){
println(i)
}
println("------------9*9乘法表------------")
var k=0
for(i <- 1 to 9;j <- 1 to 9 if(i>=j)){
k+=1
print(s"${i} * $j = ${i*j} ")
if(i==j) {
println()
}
}
println(k)
val ints = for (i <- 1 to 10) yield {i*2}
println(ints)
}
}
函数
package com.ww.syntax
import java.util.Date
/**
* 函数
*/
object syntax02_functions {
def main(args: Array[String]): Unit = {
def xxx(): Unit = {
}
// Unit = 可以不写
def xxx1() {
}
//没有返回值
def xxx31(): Unit = {
3
}
//有返回值
def xxx3(): Int = {
3
}
var tmp = xxx3()
println(tmp)
//有return的时候,必须要有返回值
def xxx4(): String = {
return "xxx"
}
println("============参数============")
//参数必须要指定类型
//必须是val 修饰
def xxx5(a: String): Unit = {
}
println("============递归============")
//递归必须要指定函数的返回值类型
def xxx6(a: Int): Int = {
if (a == 1) {
a
} else {
a * xxx6(a - 1)
}
}
println(xxx6(3));
println("============函数的默认值============")
def xxx7(a: Int = 8, b: String = "xx"): Unit = {
println(s"${a}-----$b")
}
xxx7(9)
xxx7(b = "00")
println("============匿名函数============")
//注意函数类型的写法(函数签名)
var y: (Int, Int) => Int = (a: Int, b: Int) => {
a + b
}
println(y(1, 2))
println("============嵌套函数============")
//xxx9 可以直接用xxx8 的参数
def xxx8(a: Int): Unit = {
def xxx9(): Unit = {
println(a)
}
xxx9()
}
xxx8(9999)
println("============偏应用函数============")
def xxx10(date: Date, level: String, msg: String): Unit = {
println(s"$date $level $msg")
}
xxx10(new Date(), "info", "ok")
var log = xxx10(_: Date, "debug", _: String)
log(new Date(), "xxx")
println("============可变参数============")
def xxx11(a: Int*): Unit = {
//println(a)
/*for (e <- a) {
print(e)
}*/
a.foreach((e: Int) => {
print(e)
})
println("省略类型")
a.foreach((e) => {
print(e)
})
println()
a.foreach(e => {
print(e)
})
println()
a.foreach(e => print(e))
println("直接传函数参数")
a.foreach(print)
println("当参数只使用一次时,可以用'_'替代")
a.foreach(print(_))
}
//xxx11(1)
xxx11(1, 2, 3)
println("============高阶函数============")
//函数作为参数
def xxx12(x: Int, y: Int, f: (Int, Int) => Int): Unit = {
val i = f(x, y)
println(s"计算结果:${i}")
}
xxx12(1,7,(x:Int,y:Int)=>{x+y})
xxx12(1,7,(x,y)=>{x+y})
xxx12(1,7,(x,y)=>x+y)
//参数按照顺序只使用一次,就可以用下滑线代替
xxx12(1,7,_+_)
//函数作为返回值
def factory(i:String): (Int,Int)=>Int = {
if("+".equals(i)){
_+_
}else {
_ * _
}
}
xxx12(1,7,factory("+"))
xxx12(1,7,factory("*"))
println("============柯里化============")
def xxx13(x:Int)(y:Int)(z:Int): Unit ={
println(s"${x} $y $z")
}
xxx13(1)(2)(3)
println("============变量接受函数============")
def xxx14(): Int={
println(3)
3
}
var fun = xxx14()
println(s"fun $fun")
var fun2 = xxx14 _
println(s"fun $fun2")
}
}
集合
package com.ww.syntax
import java.util
/**
* 集合
*/
object syntax03_collections {
def main(args: Array[String]) : Unit = {
//scala 中的泛型用[]
val strings = new util.LinkedList[String]()
strings.add("")
//数组
val ints = Array[Int](1, 2, 3, 4)
println(ints(1))
println("===================list======================")
val ints1 = List(1, 2, 3, 4, 5)
for (elem <- ints1) {
}
ints1.foreach(println)
println("===================set======================")
val ints2 = Set[Int](1, 2, 3, 4, 3, 4, 3, 4)
ints2.foreach(println)
//不可变
import scala.collection.mutable.Set
val ints3 = Set(1, 2, 3, 4, 3, 2)
ints3.add(5)
ints3.foreach(println)
println("===================tuple======================")
val t2 = new Tuple2(1, "xxx")
val t3 = Tuple3(1, "22", '3')
val t4 = (1, 2, 3, 4)
println(t2._2)
println(t4._3)
println("===================map======================")
val map01 = Map[String, Int](("a", 1), "b" -> 2, ("a", 3))
val keys = map01.keys
println(map01.get("a").get)
println(map01.get("b"))
println(map01.get("w").getOrElse("hello world"))
println("===================流式api======================")
val ints4 = List(1, 2, 3, 4, 5, 6, 7)
ints4.foreach(println)
val ints5 = ints4.map(_ + 10)
ints5.foreach(println)
println("===================流式api flatMap======================")
val list = List[String]("hello 1", "hello 2", "hello 3")
val list1 = list.flatMap(_.split(" "))
list1.foreach(println)
val tuples = list1.map((_, "value"))
tuples.foreach(println)
println("===================流式api 迭代器======================")
val iterator = list.iterator
val strings1 = iterator.flatMap((x)=>{
println("执行了")
x.split(" ")
})
strings1.foreach(println)
//val tuples1 = iterator.map((_, "value"))
//tuples1.foreach(println)
}
}
trait
package com.ww.syntax
import java.util
/**
* trait
*/
object lesson04_trait {
def main(args: Array[String]): Unit = {
val p = new Persion("憨憨")
p.sy()
p.say
p.ku
p.haiRen()
}
}
trait God {
def say = println("God say")
}
trait Mg {
def ku = println("Mg ku")
def haiRen()
}
class Persion(name: String) extends God with Mg {
def sy() = {
println(s"我是$name")
}
override def haiRen(): Unit = {
println("我是害人测实现")
}
}
case_class
package com.ww.syntax
import java.util
/**
* case_class
*/
object lesson05_case_class {
def main(args: Array[String]): Unit = {
val dog1 = new Dog("hsq", 18)
val dog2 = new Dog("hsq", 18)
println(dog1.equals(dog2))
println(dog1 == dog2)
}
}
case class Dog(name: String, age: Int) {}
match
package com.ww.syntax
import java.util
/**
* match
*/
object syntax06_match {
def main(args: Array[String]): Unit = {
val tuple = (1.0, 88, "str",false)
val iterator = tuple.productIterator
val units = iterator.map(
(x) => {
x match {
case y: Int => println(s"${y} is int")
case 1 => println(s"$x is 1")
case "str" => println(s"$x is str")
case _ => println("不知道是个啥")
}
}
)
println("开始while")
while(units.hasNext) println(units.next())
println(println)
}
}
偏函数
package com.ww.syntax
/**
* 偏函数
*/
object syntax07_partialFunction {
def main(args: Array[String]): Unit = {
def xxx: PartialFunction[Any, String] = {
case "hello" => "hello"
case 4 => "4"
case _ => "none"
}
println(xxx(4))
println(xxx("hello"))
println(xxx("sss"))
}
}
隐式转换
package com.ww.syntax
import java.util
/**
* implicate
*/
object syntax08_implicate {
def main(args: Array[String]): Unit = {
val ints = new util.ArrayList[Int]()
ints.add(1)
ints.add(2)
ints.add(3)
def foreach[T](list: util.List[T], f: (T) => Unit): Unit = {
val iterator = list.iterator()
while (iterator.hasNext) f(iterator.next())
}
foreach(ints, println)
println("===================隐式转换1======================")
val value = new Xxx[Int](ints)
value.foreach(println)
println("===================隐式转换2======================")
implicit def x[T](list: util.List[T]): Xxx[T] = {
new Xxx[T](list)
}
ints.foreach(println)
println("===================隐式转换3======================")
implicit def x1[T](list: util.ArrayList[T]): Ooo[T] = {
val v = list.iterator()
new Ooo[T](v)
}
implicit def x2[T](list: util.LinkedList[T]): Ooo[T] = {
val v = list.iterator()
new Ooo[T](v)
}
val ints2 = new util.LinkedList[Int]()
ints2.add(1)
ints2.add(2)
ints2.add(3)
ints2.foreach(println)
ints.foreach(println)
println("===================隐式转换4======================")
implicit val x3:String = "lisi"
def xx(implicit name:String): Unit ={
println(name)
}
xx
println("===================隐式转换5======================")
def xx2(age:Int)(implicit name:String): Unit ={
println(name+" "+ age)
}
xx2(10)
}
class Xxx[T](list: util.List[T]) {
def foreach(f: (T) => Unit): Unit = {
val iterator = list.iterator()
while (iterator.hasNext) f(iterator.next())
}
}
class Ooo[T](iter: util.Iterator[T]) {
def foreach(f: (T) => Unit): Unit = {
while (iter.hasNext) f(iter.next)
}
}
}