Scala 从入门到精通

Scala 从入门到精通

scala风格与java风格

public class JavaDemo01 {
    public static void main(String[] args) {
        System.out.println("我爱你中国");
    }
}

object ScalaDemo01 {
  def main(args: Array[String]): Unit = {
    println("我爱你中国")
  }
}

javac JavaDemo01
scalac ScalaDemo01

Scala 从入门到精通_第1张图片

scala项目启动方式

App是scala语言内置的一个特质,使用它,则把对应object内部的整体作为scala main的一部分,有延迟启动的特性。

同时,命令行参数args也作为App特质的一部分,可以被开发者直接使用。而main函数则是scala的默认启动方式。

main函数

class ScalaDemo02 {
  def main(args: Array[String]): Unit = {
    println("我爱你中国")
  }
}

使用App特质

object ScalaDemo03 extends App {
  println("我爱你中国")
}

object ScalaDemo04 extends App {
  println(args(0))
}

scalac ScalaDemo04.scala
scala ScalaDemo04 李昊哲

字符串基本使用

object ScalaDemo07 {
  def main(args: Array[String]): Unit = {
    var str01:String = "我爱你"
    var str02:String = "中国"
    // java 风格
    println(str01 + str02)
    // 插值方式 与 Python 风格比较相像
    var slogan:String = "桃李不言下自成蹊"
    println(s"slogan >>> $slogan")
    println(
      """
        |我爱你中国
        |亲爱的母亲
      """.stripMargin)
  }
}

选择结构

object ScalaDemo01 {
  def main(args: Array[String]): Unit = {
    val weekDay:Int = 3
    if (weekDay == 1){
      println("星期一")
    } else if (weekDay == 2){
      println("星期二")
    } else if (weekDay == 3) {
      println("星期三")
    } else if (weekDay == 4) {
      println("星期四")
    } else if (weekDay == 5) {
      println("星期五")
    } else if (weekDay == 6) {
      println("星期六")
    } else {
      println("星期日")
    }
  }
}

循环结构

while 循环

object ScalaDemo02 {
  def main(args: Array[String]): Unit = {
    var count:Int = 0
    while (count < 5){
      println(count)
      count += 1
    }
  }
}

for 循环

object ScalaDemo03 {
  def main(args: Array[String]): Unit = {
    // Range 1 to 5
    println(1 to 5)
    println(1.to(5))
    println(1 until 5)
    println(1.until(5))

    for (i <- 0 until 5){
      println(i)
    }
  }
}

object ScalaDemo04 {
  def main(args: Array[String]): Unit = {

    for (i <- 0 until 10) {
      if (i % 2 == 0) {
        println(i)
      }
    }

    println("++++++++++++++++++")
    for (i <- 0 until 10 if i % 2 == 0) {
      println(i)
    }

    println("++++++++++++++++++")
    for (i <- 0 until 10 by(2)) {
      println(i)
    }

    println("++++++++++++++++++")

    val courses = Array("Hadoop","Hive","Sqoop","Flume","Hbase","Phoenix","Presto")
    for (course <- courses){
      println(course)
    }
  }
}

函数

无参数 无返回值

object ScalaDemo01 {
  def main(args: Array[String]): Unit = {
    sayHi()
  }

  private def sayHi(): Unit = {
    println("无参数 无返回值 函数 sayHi")
  }

}

有参数 无返回值

object ScalaDemo02 {
  def main(args: Array[String]): Unit = {
    sayHi("hello world")
  }

  private def sayHi(msg: String): Unit = {
    println("有参数 无返回值 函数 sayHi")
    println(s"参数是 >>> ${msg}")
  }
}

默认参数 无返回值

object ScalaDemo03 {
  def main(args: Array[String]): Unit = {
    sayHi()
    sayHi("hello")
  }

  private def sayHi(msg: String = "hi"): Unit = {
    println("默认参数 无返回值 函数 sayHi")
    println(s"参数是 >>> ${msg}")
  }
}

运行结果:

默认参数 无返回值 函数 sayHi
参数是 >>> hi
默认参数 无返回值 函数 sayHi
参数是 >>> hello

有参数 有返回值

object ScalaDemo04 {
  def main(args: Array[String]): Unit = {
    println(sum(10, 20))
  }

  private def sum(a: Int, b: Int): Int = {
    // 函数的最后一行为返回值
    a + b
  }
}

可变参数

object ScalaDemo05 {
  def main(args: Array[String]): Unit = {
    println(format())
    println(format(1))
    println(format(1,2))
  }
  def format(numbers: Int*): Int = {
    var result:Int = 0
    if (numbers.size > 0){
      for (number <- numbers){
        result += number
      }
    }
    result
  }
}

运行结果:

0
1
3

面向对象

类与对象

object ScalaDemo01 {
  def main(args: Array[String]): Unit = {
    val person = new Person01
    person.realName = "李昊哲"
    println(person.realName)
  }
}
class Person01{
  var realName:String = _
}

运行结果:

李昊哲

封装

scala中属性、方法默认为public

private只在类的内部和伴生对象中可用

protected,同类、子类可以访问,同包无法访问

private[包名],增加包访问权限,包名下的其他类也可以使用

Scala提供进一步的封装,“public”属性底层实际上都是private,访问时本质上是调用了xxx(get) 和 xxx_eq$(set) 方法,

但这里的方法名并不是getXXX和setXXX,由于一些Java框架会利用反射调用getXXX和setXXX,

因此Scala也提供了上面提到过的@BeanProperty注解去生成某属性的这两个方法,

但注意@BeanProperty不能加在private修饰的属性上,

可以理解为由于“public”本身就是private,将变量修饰为private然后再提供getter、setter方法比较冗余,Scala不推荐这样做。

@BeanProperty
object ScalaDemo02 {
  def main(args: Array[String]): Unit = {
    val person = new Person02
    person.setRealName("李昊哲")
    println(person.getRealName())
    person.realName = "李哲"
    println(person.realName)
  }
}
class Person02{
  @BeanProperty
  var realName:String = _
}

运行结果:

李昊哲
李哲
private (不推荐)
object ScalaDemo03 {
  def main(args: Array[String]): Unit = {
    val person = new Person03
    person.setRealName("李昊哲")
    println(person.getRealName)
  }
}

class Person03 {
  private var realName: String = _

  def getRealName: String = {
    this.realName
  }

  def setRealName(realName: String): Unit = {
    this.realName = realName
  }

}

运行结果:

李昊哲
java混合开发
public class Person04 {
    private String realName;

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }
}

object ScalaDemo04 {
  def main(args: Array[String]): Unit = {
    val person = new Person04
    person.setRealName("李昊哲")
    println(person.getRealName)
  }
}

运行结果:

李昊哲

构造函数

object ScalaDemo05 {
  def main(args: Array[String]): Unit = {
    val person01 = new Person05("person01","220422198311222011")
    val person02 = new Person05("person02","220422198311222011",15311484567L)
    val person03 = new Person05("person02","220422198311222011","我爱你中国")
    val person04 = new Person05("person02","220422198311222011",15311484568L,"我爱你中国")
    println(person01)
    println(person02)
    println(person03)
    println(person04)
  }
}

// 主构造函数
class Person05(val realName: String, val idCard: String) {
  println("Person05 主构造器 执行了")
  var mobile: Long = _
  var slogan: String = _

  // 辅助构造函数 辅助构造函数不能有类型注解
  def this(realName: String, idCard: String, mobile: Long) = {
    // 辅助构造函数 的 第一行代码必须要调用 主构造函数 或者其他 辅助构造函数
    this(realName, idCard)
    this.mobile = mobile
  }

  def this(realName: String, idCard: String, slogan: String) = {
    // 辅助构造函数 的 第一行代码必须要调用 主构造函数 或者其他 辅助构造函数
    this(realName, idCard)
    this.slogan = slogan
  }

  def this(realName: String, idCard: String, mobile: Long, slogan: String) = {
    // 辅助构造函数 的 第一行代码必须要调用 主构造函数 或者其他 辅助构造函数
    this(realName, idCard, mobile)
    this.slogan = slogan
  }

  override def toString: String =
    this.getClass.getSimpleName +
      ", realName:" + realName +
      ", idCard:"+ idCard +
      ", mobile:" + mobile +
      ", slogan:"  + slogan
}

运行结果:

Person05 主构造器 执行了
Person05 主构造器 执行了
Person05 主构造器 执行了
Person05 主构造器 执行了
Person05, realName:person01, idCard:220422198311222011, mobile:0, slogan:null
Person05, realName:person02, idCard:220422198311222011, mobile:15311484567, slogan:null
Person05, realName:person02, idCard:220422198311222011, mobile:0, slogan:我爱你中国
Person05, realName:person02, idCard:220422198311222011, mobile:15311484568, slogan:我爱你中国

继承

object ScalaDemo06 {
  def main(args: Array[String]): Unit = {
    val worker = new Worker("李昊哲", "15311484568", "架构师")
    println(worker)
  }
}

// 主构造函数
class Person06(val realName: String, val mobile: String) {

  println("Person06 主构造器 执行了")

  override def toString: String =
    this.getClass.getSimpleName +
      ", realName:" + realName +
      ", mobile:" + mobile
}

class Worker(realName: String, mobile: String, job: String) extends Person06(realName, mobile) {
  println("Worker 主构造器 执行了")

  override def toString: String =
    this.getClass.getSimpleName +
      ", realName:" + realName +
      ", mobile:" + mobile +
      ", job:" + job
}

运行结果:

Person06 主构造器 执行了
Worker 主构造器 执行了
Worker, realName:李昊哲, mobile:15311484568, job:架构师

抽象类

object ScalaDemo07 {
  def main(args: Array[String]): Unit = {
    val teacher = new Teacher
    teacher.realName = "李昊哲"
    teacher.mobile = "15311484568"
    println(teacher)
    teacher.hi()
  }
}

abstract class Person07() {

  println("Person07 主构造器 执行了")

  var realName: String
  var mobile: String

  def hi(): Unit

  override def toString: String =
    this.getClass.getSimpleName +
      ", realName:" + realName +
      ", mobile:" + mobile
}

class Teacher extends Person07 {
  println("Teacher 主构造器 执行了")

  override def toString: String =
    this.getClass.getSimpleName +
      ", realName:" + realName +
      ", mobile:" + mobile

  override var realName: String = _
  override var mobile: String = _

  override def hi(): Unit = {
    println("接着奏乐 接着舞")
  }
}

运行结果:

Person07 主构造器 执行了
Teacher 主构造器 执行了
Teacher, realName:李昊哲, mobile:15311484568
接着奏乐 接着舞

伴生类与伴生对象

scala中的类不能定义静态成员,而代之以定义单例对象来替代
单例对象通过object关键字来声明
单例对象中的所有方法,可以直接通过object单例对象的名字直接来调用。
一个单例对象可以绑定在一个类,当单例对象和某个类写在同一个源文件且共享一个名字,它们就产生了绑定关系。
此时单例对象称之为该类的伴生对象,类称之为该对象的伴生类。

类和它的伴生对象可以互相访问其私有成员
单例对象不能new,所以也没有构造参数
可以把单例对象当做java中可能会用到的静态方法工具类。
作为程序入口的方法必须是静态的,所以main方法必须处在一个单例对象中,而不能写在一个类中。

object ScalaDemo08 {
  def main(args: Array[String]): Unit = {
    Food.printMsg()
    new Food().printSlogan()
  }
}

class Food {
  private val msg: String = "hello"
  def printSlogan(): Unit = {
    println(Food.slogan)
  }
}

object Food {
  private val slogan: String = "桃李不言下自成蹊"
  def printMsg(): Unit = {
    println(new Food().msg)
  }
}

运行结果:

hello
桃李不言下自成蹊

apply

object ScalaDemo09 {
  def main(args: Array[String]): Unit = {
    // Drink() 相当于 Drink.apply
    Drink()
    new Drink().apply()
  }
}

class Drink{
  def apply(): Unit = {
    println("class Food 执行...")
  }
}

object Drink {
  def apply(): Unit = {
    println("object Food 执行...")
  }
}

运行结果:

object Food 执行...
class Food 执行...

Trait

特质

trait Animal {
  var name: String

  def eat(): Unit
}

class Dog extends Animal {
  override var name: String = "大黄"

  override def eat(): Unit = {
    println(s"$name 改不了吃屎")
  }
}

object AnimalTest {
  def main(args: Array[String]): Unit = {
    new Dog().eat()
  }
}

运行结果:

大黄 改不了吃屎

case class

初始化的时候可以不用new,当然你也可以加上,普通类一定需要加new;

toString的实现更漂亮;

默认实现了equals 和hashCode;

默认是可以序列化的,也就是实现了Serializable ;

自动从scala.Product中继承一些函数;

case class构造函数的参数是public级别的,可以直接访问;

支持模式匹配;

case class ScalaDemo10(name:String)

object CaseClassTest {
  def main(args: Array[String]): Unit = {
    val person = ScalaDemo10("李昊哲")
    println(person)
  }
}

运行结果:

ScalaDemo10(李昊哲)

数据结构

Array

public class JavaDemo01 {
    public static void main(String[] args) {
        String[] courses = new String[5];
        courses[0] = "hadoop";
        System.out.println(courses[0]);
        String[] hobbies = {"swim", "walk", "photography"};
        for (String hobby : hobbies) {
            System.out.println(hobby);
        }
    }
}

运行结果:

hadoop
swim
walk
photography

object ScalaDemo01 {
  def main(args: Array[String]): Unit = {
    val courses = new Array[String](5)
    println(courses.length)
    courses(0) = "hadoop";
    println(courses(0))
    val hobbies = Array("swim", "walk", "photography")
    for (hobby <- hobbies) {
      println(hobby)
    }
  }
}

运行结果:

5
hadoop
swim
walk
photography

Array常用方法

object ScalaDemo02 {
  def main(args: Array[String]): Unit = {
    val numbers = Array(2,4,6,8,0,1,3,5,7,9)
    // 求和
    val sumResult = numbers.sum
    println("sumResult = " + sumResult)

    // 最大值
    val maxResult = numbers.max
    println("maxResult = " + maxResult)

    // 最小值
    val minResult = numbers.min
    println("minResult = " + minResult)

    // 排序
    val sortedNumbers = numbers.sorted
    for (number <- sortedNumbers){
      println(number)
    }
    println("++++++++++++++++++++")
    val reverseNumbers = sortedNumbers.reverse
    for (number <- reverseNumbers) {
      println(number)
    }
  }
}

运行结果:

sumResult = 45
maxResult = 9
minResult = 0
0
1
2
3
4
5
6
7
8
9
++++++++++++++++++++
9
8
7
6
5
4
3
2
1
0

可变长Array

object ScalaDemo03 {
  def main(args: Array[String]): Unit = {
    val courses = ArrayBuffer[String]()
    courses += "hadoop"
    courses += "hive"
    courses ++= Array("presto","hbase","phoenix","scala","spark")
    courses.insert(1,"logger")
    for (course <- courses) {
      println(course)
    }
  }
}

运行结果:

hadoop
logger
hive
presto
hbase
phoenix
scala
spark

object ScalaDemo03 {
  def main(args: Array[String]): Unit = {
    val courses = ArrayBuffer[String]()
    courses += "hadoop"
    courses += "hive"
    courses ++= Array("presto","hbase","phoenix","scala","spark")
    courses.insert(1,"logger")
    courses.remove(5)
    for (course <- courses) {
      println(course)
    }
  }
}

运行结果:

hadoop
logger
hive
presto
hbase
scala
spark

object ScalaDemo03 {
  def main(args: Array[String]): Unit = {
    val courses = ArrayBuffer[String]()
    courses += "hadoop"
    courses += "hive"
    courses ++= Array("presto","hbase","phoenix","scala","spark")
    courses.insert(1,"logger")
    courses.remove(5)
    courses.remove(2,3)
    for (course <- courses) {
      println(course)
    }
  }
}

运行结果:

hadoop
logger
scala
spark

List

Scala 列表类似于数组,它们所有元素的类型都相同,

但是它们也有所不同:列表是不可变的,值一旦被定义了就不能改变,

其次列表 具有递归的结构(也就是链接表结构)而数组不是。。

object ScalaDemo04 {
  def main(args: Array[String]): Unit = {
    val courses = List("presto","hbase","phoenix","scala","spark")
    for (course <- courses) {
      println(course)
    }
  }
}

运行结果:

presto
hbase
phoenix
scala
spark

bject ScalaDemo05 {
  def main(args: Array[String]): Unit = {
    val courses = ListBuffer[String]()
    courses += "hadoop"
    courses += "hive"
    courses ++= List("presto", "hbase", "phoenix", "scala", "spark")
    for (course <- courses) {
      println(course)
    }
  }
}

运行结果:

hadoop
hive
presto
hbase
phoenix
scala
spark

object ScalaDemo06 {
  def main(args: Array[String]): Unit = {
    val courses = ListBuffer[String]()
    courses += "hadoop"
    courses += "hive"
    courses ++= List("presto", "hbase", "phoenix", "scala", "spark")
    println(courses.head)
    println(courses.tail)
  }
}

运行结果:

hadoop
ListBuffer(hive, presto, hbase, phoenix, scala, spark)

Nil

object ScalaDemo07 {
  def main(args: Array[String]): Unit = {
    println(Nil)
    val courses = "hadoop" :: "presto" :: "hbase" :: Nil
    println("courses 元素数量 >>> " + courses.size)
    for (course <- courses) {
      println(course)
    }
  }
}

运行结果:

List()
courses 元素数量 >>> 3
hadoop
presto
hbase

Set

Scala Set(集合)是没有重复的对象集合,所有的元素都是唯一的。

Scala 集合分为可变的和不可变的集合。

默认情况下,Scala 使用的是不可变集合,如果你想使用可变集合,需要引用 scala.collection.mutable.Set 包。

object ScalaDemo08 {
  def main(args: Array[String]): Unit = {
    val courses = Set("presto","hbase","phoenix","scala","spark","hbase","scala")
    for (course <- courses) {
      println(course)
    }
  }
}

运行结果:

phoenix
spark
scala
presto
hbase

object ScalaDemo09 {
  def main(args: Array[String]): Unit = {
    val courses = mutable.HashSet[String]()
    courses += "hadoop"
    courses += "hive"
    courses ++= List("presto", "hbase", "phoenix", "scala", "spark")
    for (course <- courses) {
      println(course)
    }
  }
}

运行结果:

hive
phoenix
scala
spark
hadoop
hbase
presto

Map

object ScalaDemo10 {
  def main(args: Array[String]): Unit = {
    val a = Map("k1" -> "v1", "k2" -> "v2", "k3" -> "v3")
    println(a("k1"))
    // java.util.NoSuchElementException: key not found: k5
    // println(a("k5"))
    println(a.getOrElse("k1",null))
    println(a.getOrElse("k5",null))
  }
}

运行结果:

v1
v1
null
object ScalaDemo11 {
  def main(args: Array[String]): Unit = {
    val map = mutable.HashMap[String, String]()
    map += ("k1" -> "v1")
    println(map.getOrElse("k1", null))
  }
}

运行结果:

v1
object ScalaDemo12 {
  def main(args: Array[String]): Unit = {
    val map = mutable.HashMap[String, String]()
    map += ("k1" -> "v1")
    map += ("k2" -> "v2")
    map += ("k3" -> "v3")
    map += ("k4" -> "v4")
    map += ("k5" -> "v5")
    for ((key, value) <- map) {
      println(key + " >>> " + value)
    }
  }
}

运行结果:

k1 >>> v1
k2 >>> v2
k3 >>> v3
k4 >>> v4
k5 >>> v5

object ScalaDemo13 {
  def main(args: Array[String]): Unit = {
    val map = mutable.HashMap[String, String]()
    map += ("k1" -> "v1")
    map += ("k2" -> "v2")
    map += ("k3" -> "v3")
    map += ("k4" -> "v4")
    map += ("k5" -> "v5")
    val keySet = map.keySet
    for (key <- keySet) {
      println(key + " >>> " + map.getOrElse(key, null))
    }
  }
}

运行结果:

k1 >>> v1
k2 >>> v2
k3 >>> v3
k4 >>> v4
k5 >>> v5

object ScalaDemo13 {
  def main(args: Array[String]): Unit = {
    val map = mutable.HashMap[String, String]()
    map += ("k1" -> "v1")
    map += ("k2" -> "v2")
    map += ("k3" -> "v3")
    map += ("k4" -> "v4")
    map += ("k5" -> "v5")
    val values = map.values
    for (value <- values) {
      println(value)
    }
  }
}

运行结果:

v1
v2
v3
v4
v5

object ScalaDemo14 {
  def main(args: Array[String]): Unit = {
    val map = mutable.HashMap[String, String]()
    map += ("k1" -> "v1")
    map += ("k2" -> "v2")
    map += ("k3" -> "v3")
    map += ("k4" -> "v4")
    map += (### 数据结构

#### Array

```java
public class JavaDemo01 {
    public static void main(String[] args) {
        String[] courses = new String[5];
        courses[0] = "hadoop";
        System.out.println(courses[0]);
        String[] hobbies = {"swim", "walk", "photography"};
        for (String hobby : hobbies) {
            System.out.println(hobby);
        }
    }
}

运行结果:

hadoop
swim
walk
photography

object ScalaDemo01 {
  def main(args: Array[String]): Unit = {
    val courses = new Array[String](5)
    println(courses.length)
    courses(0) = "hadoop";
    println(courses(0))
    val hobbies = Array("swim", "walk", "photography")
    for (hobby <- hobbies) {
      println(hobby)
    }
  }
}

运行结果:

5
hadoop
swim
walk
photography

Array常用方法

object ScalaDemo02 {
  def main(args: Array[String]): Unit = {
    val numbers = Array(2,4,6,8,0,1,3,5,7,9)
    // 求和
    val sumResult = numbers.sum
    println("sumResult = " + sumResult)

    // 最大值
    val maxResult = numbers.max
    println("maxResult = " + maxResult)

    // 最小值
    val minResult = numbers.min
    println("minResult = " + minResult)

    // 排序
    val sortedNumbers = numbers.sorted
    for (number <- sortedNumbers){
      println(number)
    }
    println("++++++++++++++++++++")
    val reverseNumbers = sortedNumbers.reverse
    for (number <- reverseNumbers) {
      println(number)
    }
  }
}

运行结果:

sumResult = 45
maxResult = 9
minResult = 0
0
1
2
3
4
5
6
7
8
9
++++++++++++++++++++
9
8
7
6
5
4
3
2
1
0

可变长Array

object ScalaDemo03 {
  def main(args: Array[String]): Unit = {
    val courses = ArrayBuffer[String]()
    courses += "hadoop"
    courses += "hive"
    courses ++= Array("presto","hbase","phoenix","scala","spark")
    courses.insert(1,"logger")
    for (course <- courses) {
      println(course)
    }
  }
}

运行结果:

hadoop
logger
hive
presto
hbase
phoenix
scala
spark

object ScalaDemo03 {
  def main(args: Array[String]): Unit = {
    val courses = ArrayBuffer[String]()
    courses += "hadoop"
    courses += "hive"
    courses ++= Array("presto","hbase","phoenix","scala","spark")
    courses.insert(1,"logger")
    courses.remove(5)
    for (course <- courses) {
      println(course)
    }
  }
}

运行结果:

hadoop
logger
hive
presto
hbase
scala
spark

object ScalaDemo03 {
  def main(args: Array[String]): Unit = {
    val courses = ArrayBuffer[String]()
    courses += "hadoop"
    courses += "hive"
    courses ++= Array("presto","hbase","phoenix","scala","spark")
    courses.insert(1,"logger")
    courses.remove(5)
    courses.remove(2,3)
    for (course <- courses) {
      println(course)
    }
  }
}

运行结果:

hadoop
logger
scala
spark

List

Scala 列表类似于数组,它们所有元素的类型都相同,

但是它们也有所不同:列表是不可变的,值一旦被定义了就不能改变,

其次列表 具有递归的结构(也就是链接表结构)而数组不是。。

object ScalaDemo04 {
  def main(args: Array[String]): Unit = {
    val courses = List("presto","hbase","phoenix","scala","spark")
    for (course <- courses) {
      println(course)
    }
  }
}

运行结果:

presto
hbase
phoenix
scala
spark

bject ScalaDemo05 {
  def main(args: Array[String]): Unit = {
    val courses = ListBuffer[String]()
    courses += "hadoop"
    courses += "hive"
    courses ++= List("presto", "hbase", "phoenix", "scala", "spark")
    for (course <- courses) {
      println(course)
    }
  }
}

运行结果:

hadoop
hive
presto
hbase
phoenix
scala
spark

object ScalaDemo06 {
  def main(args: Array[String]): Unit = {
    val courses = ListBuffer[String]()
    courses += "hadoop"
    courses += "hive"
    courses ++= List("presto", "hbase", "phoenix", "scala", "spark")
    println(courses.head)
    println(courses.tail)
  }
}

运行结果:

hadoop
ListBuffer(hive, presto, hbase, phoenix, scala, spark)

Nil

object ScalaDemo07 {
  def main(args: Array[String]): Unit = {
    println(Nil)
    val courses = "hadoop" :: "presto" :: "hbase" :: Nil
    println("courses 元素数量 >>> " + courses.size)
    for (course <- courses) {
      println(course)
    }
  }
}

运行结果:

List()
courses 元素数量 >>> 3
hadoop
presto
hbase

Set

Scala Set(集合)是没有重复的对象集合,所有的元素都是唯一的。

Scala 集合分为可变的和不可变的集合。

默认情况下,Scala 使用的是不可变集合,如果你想使用可变集合,需要引用 scala.collection.mutable.Set 包。

object ScalaDemo08 {
  def main(args: Array[String]): Unit = {
    val courses = Set("presto","hbase","phoenix","scala","spark","hbase","scala")
    for (course <- courses) {
      println(course)
    }
  }
}

运行结果:

phoenix
spark
scala
presto
hbase

object ScalaDemo09 {
  def main(args: Array[String]): Unit = {
    val courses = mutable.HashSet[String]()
    courses += "hadoop"
    courses += "hive"
    courses ++= List("presto", "hbase", "phoenix", "scala", "spark")
    for (course <- courses) {
      println(course)
    }
  }
}

运行结果:

hive
phoenix
scala
spark
hadoop
hbase
presto

Map

object ScalaDemo10 {
  def main(args: Array[String]): Unit = {
    val a = Map("k1" -> "v1", "k2" -> "v2", "k3" -> "v3")
    println(a("k1"))
    // java.util.NoSuchElementException: key not found: k5
    // println(a("k5"))
    println(a.getOrElse("k1",null))
    println(a.getOrElse("k5",null))
  }
}

运行结果:

v1
v1
null
object ScalaDemo11 {
  def main(args: Array[String]): Unit = {
    val map = mutable.HashMap[String, String]()
    map += ("k1" -> "v1")
    println(map.getOrElse("k1", null))
  }
}

运行结果:

v1
object ScalaDemo12 {
  def main(args: Array[String]): Unit = {
    val map = mutable.HashMap[String, String]()
    map += ("k1" -> "v1")
    map += ("k2" -> "v2")
    map += ("k3" -> "v3")
    map += ("k4" -> "v4")
    map += ("k5" -> "v5")
    for ((key, value) <- map) {
      println(key + " >>> " + value)
    }
  }
}

运行结果:

k1 >>> v1
k2 >>> v2
k3 >>> v3
k4 >>> v4
k5 >>> v5

object ScalaDemo13 {
  def main(args: Array[String]): Unit = {
    val map = mutable.HashMap[String, String]()
    map += ("k1" -> "v1")
    map += ("k2" -> "v2")
    map += ("k3" -> "v3")
    map += ("k4" -> "v4")
    map += ("k5" -> "v5")
    val keySet = map.keySet
    for (key <- keySet) {
      println(key + " >>> " + map.getOrElse(key, null))
    }
  }
}

运行结果:

k1 >>> v1
k2 >>> v2
k3 >>> v3
k4 >>> v4
k5 >>> v5

object ScalaDemo13 {
  def main(args: Array[String]): Unit = {
    val map = mutable.HashMap[String, String]()
    map += ("k1" -> "v1")
    map += ("k2" -> "v2")
    map += ("k3" -> "v3")
    map += ("k4" -> "v4")
    map += ("k5" -> "v5")
    val values = map.values
    for (value <- values) {
      println(value)
    }
  }
}

运行结果:

v1
v2
v3
v4
v5

object ScalaDemo14 {
  def main(args: Array[String]): Unit = {
    val map = mutable.HashMap[String, String]()
    map += ("k1" -> "v1")
    map += ("k2" -> "v2")
    map += ("k3" -> "v3")
    map += ("k4" -> "v4")
    map += ("k5" -> "v5")
    for ((key, _) <- map) {
      println(key + " >>> " + map.getOrElse(key, null))
    }
  }
}

运行结果:

k1 >>> v1
k2 >>> v2
k3 >>> v3
k4 >>> v4
k5 >>> v5

Tuple

与列表一样,元组也是不可变的,但与列表不同的是元组可以包含不同类型的元素。

元组的值是通过将单个的值包含在圆括号中构成的

object ScalaDemo15 {
  def main(args: Array[String]): Unit = {
    val t1 = (1, 3.14, "李昊哲")
    for (i <- 0 until (t1.productArity)) {
      println(t1.productElement(i))
    }
    val protocol: String = "http"
    val domain: String = "localhost"
    val port: Int = 80
    val context: String = "lhz"
    val url = (protocol, domain, port, context)
    println(url._1)
    println(url._2)
    println(url._3)
    println(url._4)
  }
}

运行结果:

1
3.14
李昊哲
http
localhost
80
lhz

异常机制

object ScalaDemo01 {
  def main(args: Array[String]): Unit = {
    try {
      println(1 / 1)
      println(1 / 0)
    } catch {
      case e: ArithmeticException => println("数学异常")
    } finally {
      println("释放资源")
    }
  }
}

运行结果:

1
数学异常
释放资源

匹配模式

java switch

从java14开始, switch语句有了一个很大的调整, 这就让swicth语句有了更多的操作和选择,在代码上,更加的简便灵活.

  • switch 标准方式
  • switch - > 用法:
  • switch yield 用法:
标准方式
public class JavaDemo01 {
    public static void main(String[] args) {
        int dayOfWeek = 2;
        switch (dayOfWeek) {
            case 1:
                System.out.println("星期一");
            case 2:
                System.out.println("星期二");
            case 3:
                System.out.println("星期三");
            case 4:
                System.out.println("星期四");
            case 5:
                System.out.println("星期五");
            case 6:
                System.out.println("星期六");
            default:
                System.out.println("星期日");
        }
    }
}

輸出如下:

星期二
星期三
星期四
星期五
星期六
星期日

从输出结果发现case代码块被穿透了,使用break防止case代码执行穿透,代码如下:

public class JavaDemo02 {
    public static void main(String[] args) {
        int dayOfWeek = 2;
        switch (dayOfWeek) {
            case 1:
                System.out.println("星期一");
                break;
            case 2:
                System.out.println("星期二");
                break;
            case 3:
                System.out.println("星期三");
                break;
            case 4:
                System.out.println("星期四");
                break;
            case 5:
                System.out.println("星期五");
                break;
            case 6:
                System.out.println("星期六");
                break;
            default:
                System.out.println("星期日");
                break;
        }
    }
}

輸出如下:

星期二
switch - > 用法:

使用switch标准方式编写代码太多的break造成代码冗余可读性不高 可以借助函数式接口和lambda表达式简化书写

代码如下:

int dayOfWeek = 2;
switch (dayOfWeek) {
    case 1 -> System.out.println("星期一");
    case 2 -> System.out.println("星期二");
    case 3 -> System.out.println("星期三");
    case 4 -> System.out.println("星期四");
    case 5 -> System.out.println("星期五");
    case 6 -> System.out.println("星期六");
    default -> System.out.println("星期日");
}

输出结果如下:

星期二

switch yield 用法:返回值

先来开一段标准模式的代码:

public class JavaDemo04 {
    public static void main(String[] args) {
        String weekday = null;
        int dayOfWeek = 2;
        switch (dayOfWeek) {
            case 1:
                weekday = "星期一";
                break;
            case 2:
                weekday = "星期二";
                break;
            case 3:
                weekday = "星期三";
                break;
            case 4:
                weekday = "星期四";
                break;
            case 5:
                weekday = "星期五";
                break;
            case 6:
                weekday = "星期六";
                break;
            default:
                weekday = "星期日";
                break;
        }
        System.out.println(weekday);
    }
}

输出结果如下:

星期二

从上面代码不难看出我们通过条件匹配为变量赋值,再来看看switch yield的简化写法

public class JavaDemo05 {
    public static void main(String[] args) {
        int dayOfWeek = 2;
        String weekday = switch (dayOfWeek) {
            case 1:
                yield "星期一";
            case 2:
                yield "星期二";
            case 3:
                yield "星期三";
            case 4:
                yield "星期四";
            case 5:
                yield "星期五";
            case 6:
                yield "星期六";
            default:
                yield "星期日";
        };
        System.out.println(weekday);
    }
}

scala math

object ScalaDemo01 {
  def main(args: Array[String]): Unit = {
    val dayOfWeek: Int = 2
    dayOfWeek match {
      case 1 => System.out.println("星期一")
      case 2 => System.out.println("星期二")
      case 3 => System.out.println("星期三")
      case 4 => System.out.println("星期四")
      case 5 => System.out.println("星期五")
      case 6 => System.out.println("星期六")
      case _ => System.out.println("星期日")
    }
  }
}

输出结果:

星期二

偏函数

被包在花括号内没有match的一组case语句

输入参数的数据类型

输出参数的数据类型

/**
 * 偏函数
 * 被包在花括号内没有match的一组case语句
 *
 * @author 李昊哲
 * @version 1.0.0  2023/5/11 下午1:34
 */
object ScalaDemo02 {
  def main(args: Array[String]): Unit = {
    val weekDay = chime(2)
    println(weekDay)
  }

  /**
   * A 输入参数的数据类型
   * B 输出参数的数据类型
   * @return
   */
  def chime:PartialFunction[Int,String] = {
    case 1 => "星期一"
    case 2 => "星期二"
    case 3 => "星期三"
    case 4 => "星期四"
    case 5 => "星期五"
    case 6 => "星期六"
    case _ => "星期日"
  }
}

运行结果:

星期二

颗粒化函数

object ScalaDemo01 {
  def main(args: Array[String]): Unit = {
    def sum01(x: Int, y: Int) = x + y

    println(sum01(10, 20))

    def sum02(x: Int)(y: Int) = x + y

    println(sum02(10)(20))

  }
}

运行结果:

30
30

匿名函数

定义个没有名称的函数我们称之为匿名函数

箭头左边是参数列表,右边是函数体。

object ScalaDemo02 {
  def main(args: Array[String]): Unit = {
   val result = (x:Int) => x * 2
    println(result(10))
  }
}

运行结果:

20

高阶函数

foreach

遍历

public class JavaDemo03 {
    public static void main(String[] args) {
        List<Integer> nums = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
        nums.forEach(System.out::println);
    }
}

object ScalaDemo03 {
  def main(args: Array[String]): Unit = {
    val nums = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
    nums.foreach(num => println(num))
  }
}

运行结果:

1
2
3
4
5
6
7
8
9

map

逐个操作列表中每一个元素

public class JavaDemo04 {
    public static void main(String[] args) {
        List<Integer> nums = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
        List<Integer> numbers = nums.stream().map(num -> num * 2).toList();
        numbers.forEach(System.out::println);
    }
}

object ScalaDemo04 {
  def main(args: Array[String]): Unit = {
    val nums = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
    val numbers = nums.map(num => num * 2)
    numbers.foreach(num => println(num))
  }
}

object ScalaDemo05 {
  def main(args: Array[String]): Unit = {
    val nums = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
    val numbers = nums.map(_ * 2)
    numbers.foreach(num => println(num))
  }
}

运行结果:

2
4
6
8
10
12
14
16
18

flatten flatMap

flatMap

flatten

先对集合中每个元素进行map,再对map后的每个元素进行flatten

map后的每个元素也是一个集合

public class JavaDemo06 {
    public static void main(String[] args) {
        List<Integer> listA = new ArrayList<>();
        listA.add(1);
        listA.add(2);

        List<Integer> listB = new ArrayList<>();
        listA.add(5);
        listA.add(6);

        List<List<Integer>> listC = new ArrayList<>();
        listC.add(listA);
        listC.add(listB);

        List<Integer> numbers = listC.stream().flatMap(Collection::stream).toList();
        numbers.forEach(System.out::println);
    }
}

object ScalaDemo06 {
  def main(args: Array[String]): Unit = {
    val nums = List(List(1, 2), List(5, 6))
    val numbers = nums.flatten
    numbers.foreach(num => println(num))
  }
}

运行结果:

1
2
5
6

filter

过滤出符合规则的数据生成新的列表

public class JavaDemo07 {
    public static void main(String[] args) {
        List<Person> persons = new ArrayList<>();
        Person person;
        for (int i = 0; i < 10; i++) {
            person = new Person("user" + (i + 1), (i + 1) % 2);
            persons.add(person);
        }
        // 获取男性列表
        List<Person> manList = persons.stream().filter(user -> user.getGender() == 1).toList();

        // 获取女性列表
        List<Person> womanList = persons.stream().filter(user -> user.getGender() == 0).toList();

        manList.forEach(System.out::println);
        System.out.println("+++++++++++++++++++++++");
        womanList.forEach(System.out::println);
    }
}

class Person {
    private String name;
    private int gender;

    public Person() {
    }

    public Person(String name, int gender) {
        this.name = name;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", gender=" + gender +
                '}';
    }
}

运行结果:

Person{name='user1', gender=1}
Person{name='user3', gender=1}
Person{name='user5', gender=1}
Person{name='user7', gender=1}
Person{name='user9', gender=1}
+++++++++++++++++++++++
Person{name='user2', gender=0}
Person{name='user4', gender=0}
Person{name='user6', gender=0}
Person{name='user8', gender=0}
Person{name='user10', gender=0}

object ScalaDemo07 {
  def main(args: Array[String]): Unit = {
    val users = new ListBuffer[User]()
    var user: User = null
    for (i <- 0 until 10) {
      user = new User("user" + (i + 1),(i + 1) % 2)
      users += user
    }
    // 获取男性列表
    val manList = users.filter(ele => ele.getGender % 2 == 1)
    // 获取女性列表
    val womanList = users.filter(ele => ele.getGender % 2 == 0)

    manList.foreach(man => println(man))
    println("+++++++++++++++++++++++")
    womanList.foreach(woman => println(woman))
  }
}

class User {
  @BeanProperty
  var name: String = _
  @BeanProperty
  var gender: Int = _

  def this(name:String,gender:Int) = {
    this()
    this.name = name
    this.gender = gender
  }

  override def toString: String = "User{" + "name='" + name + '\'' + ", gender=" + gender + '}'
}


object ScalaDemo07 {
  def main(args: Array[String]): Unit = {
    val users = new ListBuffer[User]()
    var user: User = null
    for (i <- 0 until 10) {
      user = new User("user" + (i + 1),(i + 1) % 2)
      users += user
    }
    // 获取男性列表
    val manList = users.filter(ele => ele.getGender % 2 == 1)
    // 获取女性列表
    val womanList = users.filter(ele => ele.getGender % 2 == 0)

    manList.foreach(man => println(man))
    println("+++++++++++++++++++++++")
    womanList.foreach(woman => println(woman))
  }
}

class User {
  @BeanProperty
  var name: String = _
  @BeanProperty
  var gender: Int = _

  def this(name:String,gender:Int) = {
    this()
    this.name = name
    this.gender = gender
  }

  override def toString: String = "User{" + "name='" + name + '\'' + ", gender=" + gender + '}'
}

运行结果:

User{name='user1', gender=1}
User{name='user3', gender=1}
User{name='user5', gender=1}
User{name='user7', gender=1}
User{name='user9', gender=1}
+++++++++++++++++++++++
User{name='user2', gender=0}
User{name='user4', gender=0}
User{name='user6', gender=0}
User{name='user8', gender=0}
User{name='user10', gender=0}

reduce

相邻两个元素转为一个元素

列表中的所有元素转为一个元素

1, 2, 3, 4, 5, 6, 7, 8, 9
-1, 3, 4, 5, 6, 7, 8, 9
-4, 4, 5, 6, 7, 8, 9
-8, 5, 6, 7, 8, 9
-13, 6, 7, 8, 9
-19, 7, 8, 9
-26, 8, 9
-34, 9
-43

public class JavaDemo08 {
    public static void main(String[] args) {
        List<Integer> nums = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
        List<Integer> numbers = nums.stream().reduce((a, b) -> a - b).stream().toList();
        numbers.forEach(System.out::println);
    }
}

object ScalaDemo08 {
  def main(args: Array[String]): Unit = {
    val nums = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
    // val num = nums.reduce((a, b) => a - b)
    val num = nums.reduceLeft(_ - _)
    println(num)
  }
}

运行结果:

-43

reduceLeft

object ScalaDemo09 {
  def main(args: Array[String]): Unit = {
    val nums = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
    // val num = nums.reduceLeft((a, b) => a - b)
    val num = nums.reduceLeft(_ - _)
    println(num)
  }
}

运行结果:

-43

reduceRight

1, 2, 3, 4, 5, 6, 7, 8, 9
1, 2, 3, 4, 5, 6, 7, 1
1, 2, 3, 4, 5, 6, -6
1, 2, 3, 4, 5, -12
1, 2, 3, 4, -17
1, 2, 3, -21
1, 2, -24
1, -26
-27
object ScalaDemo10 {
  def main(args: Array[String]): Unit = {
    val nums = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
    val num = nums.reduceRight((a, b) => b - a)
    println(num)
  }
}

运行结果:

-27

1, 2, 3, 4, 5, 6, 7, 8, 9
1, 2, 3, 4, 5, 6, 7, -1
1, 2, 3, 4, 5, 6, 8
1, 2, 3, 4, 5, -2
1, 2, 3, 4, 7
1, 2, 3, -3
1, 2, 6
1, -4
5
object ScalaDemo10 {
  def main(args: Array[String]): Unit = {
    val nums = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
    val num = nums.reduceRight((a, b) => _ - _)
    println(num)
  }
}

运行结果:

-27

count

计数器

public class JavaDemo12 {
    public static void main(String[] args) {
        List<Integer> nums = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
        long count = nums.stream().filter(num -> num % 2 == 0).count();
        System.out.println("列表中偶数是数量 >>> " + count);
    }
}

object ScalaDemo12 {
  def main(args: Array[String]): Unit = {
    val nums = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
    val count = nums.count(num => num % 2 == 0)
    println(count)
  }
}

运行结果:

4

distinct

去重

public class JavaDemo13 {
    public static void main(String[] args) {
        List<Integer> nums = List.of(1, 2, 3, 5, 6, 7, 8, 9, 3, 4, 5, 7, 8, 2, 3);
        List<Integer> numbers = nums.stream().distinct().toList();
        numbers.forEach(System.out::println);
    }
}

bject ScalaDemo13 {
  def main(args: Array[String]): Unit = {
    val nums = List(1, 2, 3, 5, 6, 7, 8, 9, 3, 4, 5, 7, 8, 2, 3);
    val numbers = nums.distinct
    numbers.foreach(num => println(num))
  }
}

运行结果:

1
2
3
5
6
7
8
9
4

sorted

排序类需要实现 Comparable 接口

public class JavaDemo14 {
    public static void main(String[] args) {
        List<Dog> dogs = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            dogs.add(new Dog("dog" + (i + 1), ThreadLocalRandom.current().nextInt(1, 10)));
        }
        dogs.forEach(System.out::println);
        List<Dog> newDogs = dogs.stream().sorted((a, b) -> a.getAge() - b.getAge()).toList();
        System.out.println("++++++++++++++");
        newDogs.forEach(System.out::println);
    }
}

class Dog {
    private String name;
    private int age;

    public Dog() {
    }

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

运行结果:

Dog{name='dog1', age=3}
Dog{name='dog2', age=3}
Dog{name='dog3', age=3}
Dog{name='dog4', age=3}
Dog{name='dog5', age=7}
Dog{name='dog6', age=6}
Dog{name='dog7', age=4}
Dog{name='dog8', age=8}
Dog{name='dog9', age=7}
Dog{name='dog10', age=2}
++++++++++++++
Dog{name='dog10', age=2}
Dog{name='dog1', age=3}
Dog{name='dog2', age=3}
Dog{name='dog3', age=3}
Dog{name='dog4', age=3}
Dog{name='dog7', age=4}
Dog{name='dog6', age=6}
Dog{name='dog5', age=7}
Dog{name='dog9', age=7}
Dog{name='dog8', age=8}

object ScalaDemo14 {
  def main(args: Array[String]): Unit = {
    val random = new Random()
    val cats = List(
      new Cat("cat1", random.between(1, 10)),
      new Cat("cat2", random.between(1, 10)),
      new Cat("cat3", random.between(1, 10)),
      new Cat("cat4", random.between(1, 10)),
      new Cat("cat5", random.between(1, 10)),
      new Cat("cat6", random.between(1, 10)),
      new Cat("cat7", random.between(1, 10)),
      new Cat("cat8", random.between(1, 10)),
      new Cat("cat9", random.between(1, 10)),
      new Cat("cat10", random.between(1, 10)),
    )
    cats.foreach(cat => println(cat))
    println("++++++++++++++++++++++")
    val newCats = cats.sorted
    newCats.foreach(cat => println(cat))
  }
}

class Cat extends Comparable[Cat] {
  @BeanProperty
  var name: String = _
  @BeanProperty
  var age: Int = _

  def this(name: String, age: Int) = {
    this()
    this.name = name
    this.age = age
  }

  override def compareTo(o: Cat): Int = this.getAge - o.getAge

  override def toString = s"Cat(name=$name, age=$age)"
}

运行结果:

Cat(name=cat1, age=8)
Cat(name=cat2, age=4)
Cat(name=cat3, age=5)
Cat(name=cat4, age=1)
Cat(name=cat5, age=5)
Cat(name=cat6, age=5)
Cat(name=cat7, age=2)
Cat(name=cat8, age=3)
Cat(name=cat9, age=8)
Cat(name=cat10, age=5)
++++++++++++++++++++++
Cat(name=cat4, age=1)
Cat(name=cat7, age=2)
Cat(name=cat8, age=3)
Cat(name=cat2, age=4)
Cat(name=cat3, age=5)
Cat(name=cat5, age=5)
Cat(name=cat6, age=5)
Cat(name=cat10, age=5)
Cat(name=cat1, age=8)
Cat(name=cat9, age=8)

sortBy

不需要写代码逻辑 只需要制定排序指标即可

object ScalaDemo15 {
  def main(args: Array[String]): Unit = {
    val random = new Random()
    val pigs = List(
      new Pig("pig1", random.between(1, 10)),
      new Pig("pig2", random.between(1, 10)),
      new Pig("pig3", random.between(1, 10)),
      new Pig("pig4", random.between(1, 10)),
      new Pig("pig5", random.between(1, 10)),
      new Pig("pig6", random.between(1, 10)),
      new Pig("pig7", random.between(1, 10)),
      new Pig("pig8", random.between(1, 10)),
      new Pig("pig9", random.between(1, 10)),
      new Pig("pig10", random.between(1, 10)),
    )
    pigs.foreach(pig => println(pig))
    println("++++++++++++++++++++++")
    // 不需要写代码逻辑 只需要制定排序指标即可
    val newPigs = pigs.sortBy(pig => pig.getAge)
    newPigs.foreach(pig => println(pig))
  }
}

class Pig {
  @BeanProperty
  var name: String = _
  @BeanProperty
  var age: Int = _

  def this(name: String, age: Int) = {
    this()
    this.name = name
    this.age = age
  }

  override def toString = s"Pig(name=$name, age=$age)"
}

运行结果:

Pig(name=pig2, age=8)
Pig(name=pig3, age=8)
Pig(name=pig4, age=3)
Pig(name=pig5, age=8)
Pig(name=pig6, age=1)
Pig(name=pig7, age=7)
Pig(name=pig8, age=8)
Pig(name=pig9, age=3)
Pig(name=pig10, age=1)
++++++++++++++++++++++
Pig(name=pig6, age=1)
Pig(name=pig10, age=1)
Pig(name=pig1, age=2)
Pig(name=pig4, age=3)
Pig(name=pig9, age=3)
Pig(name=pig7, age=7)
Pig(name=pig2, age=8)
Pig(name=pig3, age=8)
Pig(name=pig5, age=8)
Pig(name=pig8, age=8)

sortWtih

传递一个函数 然后在函数内编写排序规则

object ScalaDemo16  {
  def main(args: Array[String]): Unit = {
    val random = new Random()
    val birds = List(
      new Bird("bird1", random.between(1, 10)),
      new Bird("bird2", random.between(1, 10)),
      new Bird("bird3", random.between(1, 10)),
      new Bird("bird4", random.between(1, 10)),
      new Bird("bird5", random.between(1, 10)),
      new Bird("bird6", random.between(1, 10)),
      new Bird("bird7", random.between(1, 10)),
      new Bird("bird8", random.between(1, 10)),
      new Bird("bird9", random.between(1, 10)),
      new Bird("bird10", random.between(1, 10)),
    )
    birds.foreach(Bird => println(Bird))
    println("++++++++++++++++++++++")
    // 传递一个函数 然后在函数内编写排序规则
    val newBirds = birds.sortWith((a, b) => a.getAge - b.getAge <= 0).toList
    newBirds.foreach(bird => println(bird))
  }
}

class Bird {
  @BeanProperty
  var name: String = _
  @BeanProperty
  var age: Int = _

  def this(name: String, age: Int) = {
    this()
    this.name = name
    this.age = age
  }

  override def toString = s"Bird(name=$name, age=$age)"
}

运行结果:

Bird(name=bird1, age=7)
Bird(name=bird2, age=1)
Bird(name=bird3, age=9)
Bird(name=bird4, age=2)
Bird(name=bird5, age=5)
Bird(name=bird6, age=2)
Bird(name=bird7, age=4)
Bird(name=bird8, age=1)
Bird(name=bird9, age=9)
Bird(name=bird10, age=9)
++++++++++++++++++++++
Bird(name=bird8, age=1)
Bird(name=bird2, age=1)
Bird(name=bird6, age=2)
Bird(name=bird4, age=2)
Bird(name=bird7, age=4)
Bird(name=bird5, age=5)
Bird(name=bird1, age=7)
Bird(name=bird10, age=9)
Bird(name=bird9, age=9)
Bird(name=bird3, age=9)

take

TopN

public class JavaDemo17 {
    public static void main(String[] args) {
        int base = 1000;
        ArrayList<Video> videos = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            videos.add(new Video(base + i + 1, ThreadLocalRandom.current().nextInt(100, 1000)));
        }
        videos.forEach(System.out::println);
        System.out.println("+++++++++++++");
        videos.sort((a, b) -> b.getGold() - a.getGold());
        List<Video> topList = videos.stream().limit(3).toList();
        topList.forEach(System.out::println);
    }
}

class Video {
    private int vid;
    private int gold;

    public Video() {
    }

    public Video(int vid, int gold) {
        this.vid = vid;
        this.gold = gold;
    }


    public int getVid() {
        return vid;
    }

    public void setVid(int vid) {
        this.vid = vid;
    }

    public int getGold() {
        return gold;
    }

    public void setGold(int gold) {
        this.gold = gold;
    }

    @Override
    public String toString() {
        return "Video{" +
                "vid='" + vid + '\'' +
                ", gold=" + gold +
                '}';
    }
}

运行结果:

Video{vid='1001', gold=566}
Video{vid='1002', gold=548}
Video{vid='1003', gold=620}
Video{vid='1004', gold=897}
Video{vid='1005', gold=264}
Video{vid='1006', gold=311}
Video{vid='1007', gold=882}
Video{vid='1008', gold=870}
Video{vid='1009', gold=106}
Video{vid='1010', gold=185}
+++++++++++++
Video{vid='1004', gold=897}
Video{vid='1007', gold=882}
Video{vid='1008', gold=870}

object ScalaDemo17 {
  def main(args: Array[String]): Unit = {
    val random = new Random()
    val videos = List(
      new VideoInfo(1001, random.between(100, 1000)),
      new VideoInfo(1001, random.between(100, 1000)),
      new VideoInfo(1001, random.between(100, 1000)),
      new VideoInfo(1001, random.between(100, 1000)),
      new VideoInfo(1001, random.between(100, 1000)),
      new VideoInfo(1001, random.between(100, 1000)),
      new VideoInfo(1001, random.between(100, 1000)),
      new VideoInfo(1001, random.between(100, 1000)),
      new VideoInfo(1001, random.between(100, 1000)),
      new VideoInfo(1001, random.between(100, 1000))
    )
    videos.foreach(video => println(video))
    println("++++++++++++++++++++++++++++")
    val videoSortList = videos.sortWith((a, b) => a.getGold - b.getGold >= 0).toList
    val videoTopList = videoSortList.take(3).toList
    videoTopList.foreach(video => println(video))
  }

}

class VideoInfo {
  @BeanProperty
  var name: Int = _
  @BeanProperty
  var gold: Int = _

  def this(name: Int, gold: Int) = {
    this()
    this.name = name
    this.gold = gold
  }

  override def toString = s"VideoInfo(name=$name, gold=$gold)"
}

运行结果:

VideoInfo(name=1001, gold=660)
VideoInfo(name=1002, gold=870)
VideoInfo(name=1003, gold=405)
VideoInfo(name=1004, gold=356)
VideoInfo(name=1005, gold=165)
VideoInfo(name=1006, gold=452)
VideoInfo(name=1007, gold=698)
VideoInfo(name=1008, gold=138)
VideoInfo(name=1009, gold=839)
VideoInfo(name=1010, gold=211)
++++++++++++++++++++++++++++
VideoInfo(name=1002, gold=870)
VideoInfo(name=1009, gold=839)
VideoInfo(name=1007, gold=698)

groupBy

分组

public class JavaDemo18 {
    public static void main(String[] args) {
        List<Animal> animals = new ArrayList<>();
        Animal animal;
        for (int i = 0; i < 10; i++) {
            animal = new Animal("animal" + (i + 1), (i + 1) % 2);
            animals.add(animal);
        }
        // 按照性别分组
        Map<Integer, List<Animal>> animalGenderGroup = animals.stream().collect(Collectors.groupingBy(Animal::getGender));
        animalGenderGroup.forEach((key, value) -> System.out.println((key == 1 ? "雄性" : "雌性") + " >>> " + value));
    }
}

class Animal {
    private String name;
    private int gender;

    public Animal() {
    }

    public Animal(String name, int gender) {
        this.name = name;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + '\'' +
                ", gender=" + gender +
                '}';
    }
}

运行结果:

雌性 >>> [Animal{name='animal2', gender=0}, Animal{name='animal4', gender=0}, Animal{name='animal6', gender=0}, Animal{name='animal8', gender=0}, Animal{name='animal10', gender=0}]
雄性 >>> [Animal{name='animal1', gender=1}, Animal{name='animal3', gender=1}, Animal{name='animal5', gender=1}, Animal{name='animal7', gender=1}, Animal{name='animal9', gender=1}]

object ScalaDemo18 {
  def main(args: Array[String]): Unit = {
    val baseName: String = "emp"
    val emps = List(
      new Emp(baseName + 1001, 1),
      new Emp(baseName + 1002, 2),
      new Emp(baseName + 1003, 1),
      new Emp(baseName + 1004, 2),
      new Emp(baseName + 1005, 1),
      new Emp(baseName + 1006, 2),
      new Emp(baseName + 1007, 2),
      new Emp(baseName + 1008, 1),
      new Emp(baseName + 1009, 1),
      new Emp(baseName + 1010, 1)
    )
    val deptMap: Map[Int, List[Emp]] = emps.groupBy(_.getDeptId)
    for ((deptId, empList) <- deptMap) {
      println(deptId + " >>> " + empList)
    }
  }
}

class Emp {
  @BeanProperty
  var name: String = _
  @BeanProperty
  var deptId: Int = _

  def this(name: String, deptId: Int) {
    this()
    this.name = name
    this.deptId = deptId
  }

  override def toString = s"Emp(name=$name, deptId=$deptId)"
}

运行结果:

1 >>> List(Emp(name=emp1001, deptId=1), Emp(name=emp1003, deptId=1), Emp(name=emp1005, deptId=1), Emp(name=emp1008, deptId=1), Emp(name=emp1009, deptId=1), Emp(name=emp1010, deptId=1))
2 >>> List(Emp(name=emp1002, deptId=2), Emp(name=emp1004, deptId=2), Emp(name=emp1006, deptId=2), Emp(name=emp1007, deptId=2))

隐式转换

object ImplicitApp {
  def main(args: Array[String]): Unit = {
    new Man().eat()

    // 定义隐式转换函数
    implicit def manToSuperman(man: Man): Superman = new Superman

    new Man().fly()
  }
}

class Man {
  def eat(): Unit = {
    println("吃核桃")
  }
}

class Superman {
  def fly(): Unit = {
    println("See Me Fly")
  }
}

运行结果:

吃核桃
See Me Fly

File

course.txt

linux shell
java mysql jdbc
hadoop hdfs mapreduce
hive presto
flume kafka
hbase phoenix
scala spark
sqoop flink

object FileTest {
  def main(args: Array[String]): Unit = {
    val file = Source.fromFile("course.txt")(scala.io.Codec.UTF8)
    file.getLines().foreach(line => println(line))
  }
}

运行结果:

linux shell
java mysql jdbc
hadoop hdfs mapreduce
hive presto
flume kafka
hbase phoenix
scala spark
sqoop flink

WordCount

  1. 读取文件

  2. 将读取的每行字符串存储到字符串列表中 List(“java mysql”,“hadoop hdfs mapreduce”)

  3. 遍历字符串列表(每行的数据)按照空格 切分 生成新的列表

    PS:二维列表 List(List(“java”,“mysql”), List(“hadoop”,“hdfs”,“mapreduce”))

  4. flapMap flatten List(“java”,“mysql”,“hadoop”,“hdfs”,“mapreduce”)

  5. groupBy

  6. count

  7. 保存数据到文件

words.txt

linux shell
java mysql jdbc
hadoop hdfs mapreduce
hive presto
flume kafka
hbase phoenix
scala spark
sqoop flink
linux shell
java mysql jdbc
hadoop hdfs mapreduce
hive presto
flume kafka
hbase phoenix
scala spark
sqoop flink
base phoenix
scala spark
sqoop flink
linux shell
java mysql jdbc
hadoop hdfs mapreduce
java mysql jdbc
hadoop hdfs mapreduce
hive presto
flume kafka
hbase phoenix
scala spark
java mysql jdbc
hadoop hdfs mapreduce
java mysql jdbc
hadoop hdfs mapreduce
hive presto

object WordCount01 {
  def main(args: Array[String]): Unit = {
    // 1、读取文件
    val file = Source.fromFile("words.txt")
    // 2、将读取的每行字符串存储到字符串列表中 List("java mysql","hadoop hdfs mapreduce")
    val words = file.getLines().toList
    // words.foreach(println)
    // 3、遍历字符串列表(每行的数据)按照空格 切分 生成新的列表
    // PS:二维列表 List(List("java","mysql"), List("hadoop","hdfs","mapreduce"))
    // 4、flapMap flatten List("java","mysql","hadoop","hdfs","mapreduce")
    val flatList = words.flatMap(_.split(" "))
    //   flatList.foreach(println)
    // 5、groupBy
    // List((phoenix,List(phoenix, phoenix, phoenix, phoenix)), (shell,List(shell, shell, shell)))
    val groupList = flatList.groupBy(word => word).toList
    // println(groupList)
    // 6、count
    // List(phoenix 4, shell 3, sqoop 3)
    val result = groupList.map(kv => kv._1 + " " + kv._2.size)
    //  println(result)
    // 7、保存数据到文件
    val writer = new FileWriter("result.txt")
    result.foreach(content => writer.write(content + "\n"))
    writer.close()
  }
}

object WordCount02 {
  def main(args: Array[String]): Unit = {
    val file = Source.fromFile("words.txt")
    val writer = new FileWriter("result.txt")
    file
      .getLines().toList
      .flatMap(_.split(" "))
      .groupBy(word => word).toList
      .map(kv => kv._1 + " " + kv._2.size)
      .foreach(content => writer.write(content + "\n"))
    writer.close()
  }
}

运行结果:

result.txt

phoenix 4
shell 3
sqoop 3
java 6
kafka 3
hadoop 6
spark 4
hive 4
scala 4
linux 3
mysql 6
hbase 3
hdfs 6
mapreduce 6
jdbc 6
flink 3
base 1
presto 4
flume 3

JDBC

运行结果:


判断数组中只出现一次的元素数量 判断数组中只出现次数最多的前三个元素

package com.lihaozhe.util;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 工具类
 *
 * @author 李昊哲
 * @version 1.0.0
 */
public class Util {
    /**
     * 该数组只出现一次的数字数量 如果没有返回 0
     *
     * @param nums 数组参数
     * @return 只出现一次的数量
     */
    public static int fun01(int... nums) {
        if (nums == null || nums.length == 0) {
            // 如果传入空数组 直接返回 0
            return 0;
        }
        // 将传入的数组存入 预设的map中
        Map<Integer, List<Integer>> map = wrapper(nums);
        // 1、map中获取entrySet
        // 2、entrySet转为stream
        // 3、过滤出 value 中只有一个元素的 entry
        // 4、统计数量
        return (int) map.entrySet().stream().filter(entry -> entry.getValue().size() == 1).count();
    }

    /**
     * 该数组只出现次数最多的前三个数字
     *
     * @param nums 数组参数
     * @return 只出现一次的数量
     */
    public static List<Integer> fun02(int... nums) {
        if (nums == null || nums.length == 0) {
            // 如果传入空数组 直接返回 空集合
            return new ArrayList<>();
        }
        // 将传入的数组存入 预设的map中
        Map<Integer, List<Integer>> map = wrapper(nums);
        // 1、map中获取entrySet
        // 2、entrySet转为stream
        // 3、按照 entry 的 value 中元素的数量降序排序
        // 4、截取前三个 entry
        // 5、截取后 返回集合中
        List<Map.Entry<Integer, List<Integer>>> collect = map.entrySet().stream().sorted((o1, o2) -> o2.getValue().size() - o1.getValue().size()).limit(3).collect(Collectors.toList());
        // 获取集合中 entry 的 key 存储集合中并返回
        return collect.stream().map(Map.Entry::getKey).collect(Collectors.toList());
    }

    /**
     * 将数据封装到固定格式的map中
     * 数组中的元素作为key 每出现一次 将该元素存到集合中并将该集合作为 该元素的 key 的 value
     *
     * @param nums 数组参数
     * @return 封装好的数据
     */
    private static Map<Integer, List<Integer>> wrapper(int... nums) {
        // 存储容器
        Map<Integer, List<Integer>> map = new HashMap<>();
        // 遍历参数数组
        for (int num : nums) {
            // 声明 value 容器
            List<Integer> list;
            if (map.containsKey(num)) {
                // 如果 该元素在容器中存在 那就将该元素的 value 取出
                list = map.get(num);
                // 将新元素添加刀集合中
                list.add(num);
            } else {
                // 如果 该元素在容器中不存在 那就将该元素的 存储 集合中 等待向容器中存储
                list = new ArrayList<Integer>() {{
                    add(num);
                }};
            }
            // 将新元素存储到容器中
            map.put(num, list);
        }
        // 返回 容器数据
        return map;
    }
}

package com.lihaozhe.util;

import org.junit.jupiter.api.Test;

/**
 * @author 李昊哲
 * @version 1.0.0
 */
public class UtilTest {
    @Test
    public void test01() {
        int[] arr = {1, 2, 3, 3};
        System.out.println(Util.fun01(arr));
    }

    @Test
    public void test02() {
        int[] arr = {1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5};
        System.out.println(Util.fun02(arr));
    }
}

你可能感兴趣的:(Java,大数据,云计算,scala,java,开发语言)