Last active
February 17, 2025 08:07
-
-
Save driessamyn/bfd2b194ec927d599d345869c3444a45 to your computer and use it in GitHub Desktop.
Kotlin/Java comparison
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // from: https://gist.github.com/MichaelDrogalis/37af55273ea17f3255c750ae60fc98d4 | |
| import java.util.List; | |
| public class Java23Example { | |
| record Person(String name, int age) {} | |
| public static void main(String[] args) { | |
| var people = List.of( | |
| new Person("Alice", 30), | |
| new Person("Bob", 25), | |
| new Person("Charlie", 40), | |
| new Person("David", 18) | |
| ); | |
| var processedPeople = people.stream() | |
| .filter(person -> !person.name().equals("Bob")) | |
| .map(person -> new Person(person.name().toUpperCase(), person.age())) | |
| .toList(); | |
| for (var person : processedPeople) { | |
| System.out.println(personInfo(person)); | |
| } | |
| } | |
| static String personInfo(Person person) { | |
| return switch (person) { | |
| case Person(String name, int age) | |
| when age < 20 -> name + " is a teenager."; | |
| case Person(String name, int age) | |
| when age >= 20 && age < 40 -> name + " is an adult."; | |
| case Person(String name, int age) -> name + " is middle-aged."; | |
| }; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| data class Person(val name: String, val age: Int) | |
| fun listPeople() = | |
| listOf( | |
| Person("Alice", 30), | |
| Person("Bob", 25), | |
| Person("Charlie", 40), | |
| Person("David", 18) | |
| ).filterNot { it.name == "Bob" } | |
| .map { it.copy(name = it.name.uppercase()) } | |
| .forEach { println(personInfo(it)) } | |
| fun personInfo(person: Person) = when { | |
| person.age < 20 -> "${person.name} is a teenager." | |
| person.age in 20..39 -> "${person.name} is an adult." | |
| else -> "${person.name} is middle-aged." | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| data class Person(val name: String, val age: Int) | |
| fun listPeople() = | |
| listOf( | |
| Person("Alice", 30), | |
| Person("Bob", 25), | |
| Person("Charlie", 40), | |
| Person("David", 18) | |
| ).filterNot { it.name == "Bob" } | |
| .map { it.copy(name = it.name.uppercase()) } | |
| .forEach { | |
| when { | |
| it.age < 20 -> "${it.name} is a teenager." | |
| it.age in 20..39 -> "${it.name} is an adult." | |
| else -> "${it.name} is middle-aged." | |
| }.also { | |
| println(it) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment