Des Dulianto
Sysadmin and hobbyist coder, lifelong learning
Kongkow IT Medan Meetup
@desdulianto
data class KotlinPerson @JvmOverloads constructor(val name: String,
val gender: String = "Pria",
var age: Int)
public class TestPersonJava {
public static void main(String []args) {
JavaPerson jvPerson = new JavaPerson("Budi", 25);
KotlinPerson ktPerson = new KotlinPerson("Budi", 25);
System.out.println("Java Person");
System.out.println("Name: " + jvPerson.getName());
System.out.println("Gender: " + jvPerson.getGender());
System.out.println("Age: " + jvPerson.getAge());
System.out.println("Java Person: " + jvPerson.toString());
System.out.println("Kotlin Person");
System.out.println("Name: " + ktPerson.getName());
System.out.println("Gender: " + ktPerson.getGender());
System.out.println("Age: " + ktPerson.getAge());
System.out.println("Kotlin Person: " + ktPerson.toString());
}
}
data class KotlinPerson @JvmOverloads constructor(val name: String,
val gender: String = "Pria",
var age: Int)
fun main(args: Array<String>) {
val jvPerson = JavaPerson("Budi", 25)
val ktPerson = KotlinPerson(name = "Budi", age = 25)
println("Java Person")
println("Name: ${jvPerson.name}")
println("Gender: ${jvPerson.gender}")
println("Age: ${jvPerson.age}")
println("Java Person: $jvPerson")
println("Kotlin Person")
println("Name: ${ktPerson.name}")
println("Gender: ${ktPerson.gender}")
println("Age: ${ktPerson.age}")
println("Kotlin Person: $ktPerson")
}
Destructuring, Smart casting, If, when & try expression, Expression function, Infix function, Extension function
infix fun KotlinPerson.sayHello(other: KotlinPerson) =
println("${this.name}: Hello, ${other.name}")
fun main(args: Array<String>) {
val people = listOf(KotlinPerson(name = "Budi", age = 25),
KotlinPerson(name = "Susi", gender = "Wanita", age = 25),
KotlinPerson(name = "Tuti", gender = "Wanita", age = 20),
KotlinPerson(name = "Ali", gender = "Pria", age = 30),
KotlinPerson(name = "Rudi", gender = "Pria", age = 27))
// destructuring
for ((name, gender, age) in people) {
val genderString = if (gender == "Wanita") "woman" else "man"
println("$name is $age years old $genderString")
}
val budi = people[0]
val susi = people[1]
budi.sayHello(susi)
susi sayHello budi
}
data class KotlinPerson @JvmOverloads constructor(val name: String,
val gender: String = "Pria",
var age: Int)
fun prettyPrint(person: KotlinPerson?) {
person?.let {
println("Name: ${it.name}")
println("Gender: ${it.gender}")
println("Age: ${it.age}")
} ?: println("No person found!")
}
fun main(args: Array<String>) {
val budi = KotlinPerson("Budi", "Pria", 25)
// cannot change name, because it is declared as val
// budi.name = "Rudi"
// can change age, because it is declared as var
budi.age = 27
prettyPrint(budi)
prettyPrint(null)
}
fun displayPeople(people: List<KotlinPerson>, label: String = "",
filter: (KotlinPerson) -> Boolean) {
println(if (label == "") "Kotlin Person" else label)
for (person in people) {
if (filter(person)) {
println(person)
}
}
}
fun main(args: Array<String>) {
val people = listOf(...)
displayPeople(people, "All Pria", {p -> p.gender == "Pria"})
displayPeople(people, "Equal or above 25 years old") { it -> it.age >= 25 }
displayPeople(people, "All Wanita above 20 years old") {
it -> it.gender == "Wanita" && it.age > 20
}
}
fun main(args: Array<String>) {
val people = listOf(...)
// print all person name and age
people.forEach { println("Name: ${it.name}, age: ${it.age} years old") }
// all people sorted by age (ascending)
people.sortedBy { it.age }
// all Pria
people.filter { it.gender == "Pria" }
// first wanita
people.first { it.gender == "Wanita"}
// oldest wanita
people.filter {it.gender == "Wanita" }.maxBy { it.age }
// group people by age
people.groupBy { it.age }.maxBy { it.value.count() }
// age 10 years from now
people.map { it.copy(age = it.age + 10) }
// partition people with age below and equal or above 25
people.partition { it.age <= 25}
// each gender count
people.groupingBy { it.gender }.eachCount()
// all people where age below average age
val averageAge = people.map { it.age }.average()
people.filter { it.age <= averageAge }
}
By Des Dulianto