Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
General sample code with generics in Kotlin
data class Course(val name: String)
class OddList<T>(val list: List<T>) {
fun oddItems(): List<T> {
return list.filterIndexed { index, _ -> index % 2 == 1 }
}
}
fun main() {
val listOfStrings = listOf("Kotlin", "Java", "C#")
val resultOfStrings: OddList<String> = OddList(listOfStrings)
println(resultOfStrings.oddItems())
val listOfInts = listOf(1, 7, 8, 9, 12, 45)
val resultOfInts = OddList(listOfInts)
println(resultOfInts.oddItems())
val courses = listOf(
Course("Kotlin"),
Course("Java"),
Course("C#"),
Course("PHP"),
Course("C++")
)
var resultCourses = OddList(courses).oddItems()
println(resultCourses)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.