随笔分类 -  Kotlin

摘要:Let's see following code: println(colors.reduce { acc, curr -> "$acc, $curr" }) // red, blue, green, black val myMap = mapOf(1 to "one", 2 to "two", 3 阅读全文
posted @ 2020-10-26 16:22 Zhentiw 阅读(109) 评论(0) 推荐(0) 编辑
摘要:In kotlin, "fold" is same in Javascript's reduce. "fold" has initial value "reduce" in kotlin, you don't give initial value val colors = listOf("red", 阅读全文
posted @ 2020-10-26 16:14 Zhentiw 阅读(200) 评论(0) 推荐(0) 编辑
摘要:Java way: package com.rsk.java; import java.util.function.Function; public class FPExample { public static Function<String, String> toSentenceCase = x 阅读全文
posted @ 2020-10-21 02:07 Zhentiw 阅读(83) 评论(0) 推荐(0) 编辑
摘要:For example you have the Java class: package com.rsk.java; import com.rsk.kotlin.Customer; import com.rsk.kotlin.CustomerDatabase; import java.util.*; 阅读全文
posted @ 2020-10-21 01:46 Zhentiw 阅读(93) 评论(0) 推荐(0) 编辑
摘要:buildscript { ext.kotlin_version = '1.1-M01' ext.kotlin_gradle_version = '1.1-M01' ext.mokito_kotlin_version = '0.6.0' ext.spek_version = '1.0.89' ext 阅读全文
posted @ 2020-10-18 21:38 Zhentiw 阅读(208) 评论(0) 推荐(0) 编辑
摘要:Create a Kotlin class: package com.rsk.kotlin class Meeting(val title: String) { // in Java, you can use getLocation and setLocation var location = "" 阅读全文
posted @ 2020-10-15 21:21 Zhentiw 阅读(103) 评论(0) 推荐(0) 编辑
摘要:Create a Java class: package com.rsk.java; import org.jetbrains.annotations.Nullable; public class Person { private String name; private int age; priv 阅读全文
posted @ 2020-10-15 21:04 Zhentiw 阅读(87) 评论(0) 推荐(0) 编辑
摘要:package com.rsk interface Signatory { fun sign() } open class Person(val name: String, var age: Int, var isMarried: Boolean = false): Signatory { over 阅读全文
posted @ 2020-10-15 20:48 Zhentiw 阅读(90) 评论(0) 推荐(0) 编辑
摘要:If your method is just a single line, it is good to use method expressions: interface Signatory { fun sign() } class Person: Signatory { override fun 阅读全文
posted @ 2020-10-15 18:35 Zhentiw 阅读(70) 评论(0) 推荐(0) 编辑
摘要:In Kotlin, there is no static methods, but we can use companion object which works the same as static methods. For example, a class: package com.rsk i 阅读全文
posted @ 2020-10-15 18:30 Zhentiw 阅读(138) 评论(0) 推荐(0) 编辑
摘要:First version: fun main(args: Array<String>) { val providers = Providers() val details = providers.getAllProviders() details.forEach { detail -> print 阅读全文
posted @ 2020-10-15 00:08 Zhentiw 阅读(113) 评论(0) 推荐(0) 编辑
摘要:fun getAllProviders(fn: (String, String) -> Unit) { // Unit is just like void val allProviders = Providers.getProviders() val it = allProviders.iterat 阅读全文
posted @ 2020-10-14 16:05 Zhentiw 阅读(67) 评论(0) 推荐(0) 编辑
摘要:fun main(args: Array<String>) { val ipToCount = mutableMapOf<String, Int>() File("src/ips.txt").forEachLine { val previous = ipToCount.getOrDefault(it 阅读全文
posted @ 2020-10-14 15:26 Zhentiw 阅读(117) 评论(0) 推荐(0) 编辑
摘要:fun main(args: Array<String>) { val namesToAges = mapOf(Pair("Peter", 24), Pair("Roger", 42)) val namesToAges2 = mapOf( "Peter" to 24, "Roger" to 42 ) 阅读全文
posted @ 2020-10-14 15:20 Zhentiw 阅读(92) 评论(0) 推荐(0) 编辑
摘要:import java.io.File fun main(args: Array<String>) { File("src/inputFile.txt").forEachLine { print(it) // it refers to each line's content } } 阅读全文
posted @ 2020-10-14 15:06 Zhentiw 阅读(170) 评论(0) 推荐(0) 编辑
摘要:var letters = str.toLowerCase().toCharArray().toHashSet() 阅读全文
posted @ 2020-10-14 15:04 Zhentiw 阅读(79) 评论(0) 推荐(0) 编辑
摘要:import java.util.* fun main(args: Array<String>) { val number = Random().nextInt(100) + 1 var input: String? var guess = -1 while(guess!=number) { pri 阅读全文
posted @ 2020-10-14 02:54 Zhentiw 阅读(110) 评论(0) 推荐(0) 编辑
摘要://vararg: just like ...args in js class Stack<T>(vararg val items: T) { val elements = items.toMutableList() fun push(element: T) { elements.add(eleme 阅读全文
posted @ 2020-10-14 02:46 Zhentiw 阅读(91) 评论(0) 推荐(0) 编辑
摘要://vararg: just like ...args in js class Stack<T>(vararg val items: T) { val elements = items.toMutableList() fun push(element: T) { elements.add(eleme 阅读全文
posted @ 2020-10-14 02:40 Zhentiw 阅读(47) 评论(0) 推荐(0) 编辑
摘要:private protected internal: accessable for the project / module public https://kotlinlang.org/docs/reference/visibility-modifiers.html 阅读全文
posted @ 2020-10-13 02:33 Zhentiw 阅读(62) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示