1.apply函数详解
1.1. DSL
/**
* 为什么要传入扩展函数(泛型),而不是一个普通的匿名函数
* T.()->Unit
* 扩展函数里自带了接收者对象的this隐式调用
* 为什么是泛型的扩展函数?
* 因为是由this 隐式调用 this 类型就是泛型类型, 相当于this的扩展函数,就把this的作用域放进去了.
*
* 匿名函数,也可以是扩展函数
*
*
*/
2.函数式编程
3.变换函数
3.1 map函数
fun main() {
val animals:List<String> = listOf("zebra", "giraffe", "elephant", "rat")
val map:List<String> = animals.map { animals -> "A baby $animals" } //相当于stream流 map 然后自动collectList
println(animals)//不会改变原来的集合
println(map)
}
3.2 flapMap
fun main() {
val flatMap =
listOf(listOf(1, 2, 3), listOf(4, 5, 6, 7, 8, 9)).flatMap { it -> it.map { it + 1 } } //flatMap是合并泛型里同类型的集合
println(flatMap)
}
3.3 filter
和Java一样,不需要过多重复
3.4 合并函数
zip
fun main() {
val employees = listOf("Jack", "Rose", "Jason")
val shirtSize = listOf("red", "green", "yellow")
val map:Map<String,String> = employees.zip(shirtSize).toMap()
println(map)
}
fold
fun main() {
//将每个元素乘以三叠加起来
val fold = listOf(1, 2, 3, 4).fold(0) { acc, i ->
println("acc = $acc")
acc + (i * 3)
}
println("sum = $fold")
}
4.序列
none 是 若有一个为 true 则为false ,全为false 则为true
fun Int.isPrime():Boolean{
( 2 until this).map {
if (this % it == 0){
return false
}
}
return true
}
fun main() {
//假定0-5000之内,可以找到1000个素数
val toList = (1..5000).toList().filter { it.isPrime() }.take(1000)//take 拿去前1000个
println(toList)
//就是不知道有多少个可变的序列
val oneTousandPrimes = generateSequence(2) { v ->
v + 1
}.filter { it.isPrime() }.take(1000)
println(oneTousandPrimes.toList().size)
}
5. Java与Kotlin互操作性和可空性
package javahuixang
fun main() {
val adversary = Jhava()
val determineGreeting = adversary.determineGreeting()
//String! 平台类型 有可能会有空指针 ,需要处理一下
println(determineGreeting?.toLowerCase())
}
package javahuixang;
public class Jhava {
public String utterGreeting(){
return "Hello";
}
public String determineGreeting(){
return null;
}
}
6.类型映射
package javahuixang;
public class Jhava {
public int hitPoints = 3322;
public String utterGreeting(){
return "Hello";
}
public String determineGreeting(){
return null;
}
}
package javahuixang
fun main() {
val jhava = Jhava()
println(jhava.hitPoints.javaClass)
}
7.属性访问
package javahuixang
fun main() {
val jhava = Jhava()
println(jhava.hitPoints)
}
package javahuixang;
public class Jhava {
private int hitPoints = 3322;
public String utterGreeting(){
return "Hello";
}
public String determineGreeting(){
return null;
}
public int getHitPoints() {
System.out.println("get");
return hitPoints;
}
public void setHitPoints(int hitPoints) {
this.hitPoints = hitPoints;
}
}
8.Java调用kotlin
package javahuixang
fun main() {
val adversary = Jhava()
val determineGreeting = adversary.determineGreeting()
//String! 平台类型 有可能会有空指针 ,需要处理一下
println(determineGreeting?.toLowerCase())
}
fun makeProclamation() = "Greetings,beast!"
public static void main(String[] args) {
String s = HeroKt.makeProclamation();
System.out.println(s);
}
9.@file:JvmName 给kotlin文件起别名
@file:JvmName("Hero")
package javahuixang
fun main() {
val adversary = Jhava()
val determineGreeting = adversary.determineGreeting()
//String! 平台类型 有可能会有空指针 ,需要处理一下
println(determineGreeting?.toLowerCase())
}
fun makeProclamation() = "Greetings,beast!"
public static void main(String[] args) {
String s = Hero.makeProclamation();
System.out.println(s);
}
10.@JvmField
package javahuixang
class Spellbook{
@JvmField
val spells = listOf("a","b")
}
public static void main(String[] args) {
Spellbook spellbook = new Spellbook();//加了 @JvmField 自动调用 kotlin的get方法 ,给Java提供了Kotlin的语法
List<String> spells = spellbook.spells;
}
11.@JvmOverload
@JvmOverloads
fun handOverFood(leftHand:String="berries",rightHand:String = "beef"){
println("Mmmm... you hand over some delicious $leftHand and $rightHand")
}
public static void main(String[] args) {
//Java要支持 用默认值 得重载
Hero.handOverFood();
}
kotlin最大的优势就是可以和Java互编
12. @JvmStatic
package javahuixang
class Spellbook{
@JvmField
val spells = listOf("a","b")
companion object{
@JvmField
val MX=10
@JvmStatic
fun getSpellGreeting() = println("i am a good")
}
}
public static void main(String[] args) {
//Java要支持 用默认值 得重载
// Spellbook.Companion.getMX();
System.out.println(Spellbook.MX);
//让JAVA支持kotlin的写法
Spellbook.getSpellGreeting();
}
类加载就加载出来的,在JVM加载不会有线程安全问题
13.Throws
Kotlin抛出的异常,让JAVA能够catch到
想要Java 能够catch kotlin 抛出的异常,必须用 这个注解
@kotlin.jvm.Throws(IOException::class)
fun acceptApology() {
throw IOException()
}
Java
try {
Hero.acceptApology();
} catch (IOException e) {
System.out.println("catch");
}
14.函数类型操作
Java调用kotlin 匿名函数
val translator= {utterance:String ->
println(utterance.toLowerCase().capitalize())
}
public static void main(String[] args) {
Function1<String, Unit> translator = Hero.getTranslator();
translator.invoke("TRUCE");
}