kotlin collections
kotlin collections
List, 一个有序集合, 通过下标访问元素, 元素可以重复
Set, 一个数学抽象集合, 无序, 唯一. 一般Map的key就是set. 默认LinkedHashSet, HashSet
Map(dictionary), key-value pairs. key是唯一的, 并且会关联一个value. value可以重复. 默认LinkedHashMap, HashMap
kotlin有 read-only 和 mutable
read-only 集合是convariant; mutable 不是convariant的
Constructing collections
Construct from elemen...
macos install 2.7
macos install 2.7
brew install openssl openssl@1.1 readline sqlite3 xz zlib pyenv
export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include"
pyenv install 2.7.18
Reference
Java to Kotlin migration guides
Java to Kotlin migration guides
String
连接字符串:
Java: “1” + “b”
kotlin: “$a$b”
生成字符串:
Java: StringBuilder
kotlin: buildString() // 底层也是使用StringBuilder
通过集合创建字符串:
Java: Stream API
// Java
List numbers = List.of(1, 2, 3, 4, 5, 6);
String invertedOddNumbers = numbers
.stream()
...
kotlin jvm
kotlin jvm
kotlin多平台开发支持:
Android and iOS (在Alpha阶段)
JVM
JavaScript
Native
Scripting
Comparison to java
Some Java issues addressed in Kotlin:
Null references are controlled by the type system.
No raw types
Arrays in Kotlin are invariant
Kotlin has proper function types, as op...
kotlin reflection
kotlin reflection
dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect:1.7.20")
}
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
</dependencies>
references
class references
...
kotlin destructuring declarations
kotlin destructuring declarations
val (name, age) = person
被翻译成:
val name = person.component1()
val age = person.component2()
for ((a, b) in collection) { ... }
Reference
kotlin this
kotlin this
To denote the current receiver, you use this expressions:
* In a member of a class, this refers to the current object of that class.
* In an extension function or a function literal with receiver this denotes the receiver parameter that is passed on the left-hand side of a dot.
If this has no qualifiers, it refers to the innermo...
453 post articles, 57 pages.