← Back to list

Advanced Kotlin Part 13: Generics in Kotlin — Write Once, Use Everywhere

Generics let you write flexible, reusable code that works with any type. Let’s master them!

Kishor Ramani · 2026-04-04 03:31 · 0 claps · 1.3 min read
#mobile-app-development #kotlin-app-development #kotlin-development #mobile-app-developers #kotlin-multiplatform
Open on Medium ↗
Wiki topics: 📱 · Mobile Development

Advanced Kotlin Part 13: Generics in Kotlin — Write Once, Use Everywhere

Generics let you write flexible, reusable code that works with any type. Let’s master them!

The Problem Without Generics

// Works only with Int - not reusable!
class ArrayUtil(private val array: Array<Int>) {
    fun findElement(element: Int, foundElement: (index: Int, element: Int?) -> Unit) {
        for (i in array.indices) {
            if (array[i] == element) {
                foundElement(i, array[i])
                return
            }
        }
        foundElement(-1, null)
    }
}

// Generic version - works with any type!
class ArrayUtil1<T>(private val array: Array<T>) {
    fun findElement(element: T, foundElement: (index: Int, element: T?) -> Unit) {
        for (i in array.indices) {
            if (array[i] == element) {
                foundElement(i, array[i])
                return
            }
        }
        foundElement(-1, null)
    }
}

// Generic function
fun <T> findElement(array: Array<T>, element: T, foundElement: (index: Int, element: T?) -> Unit) {
    for (i in array.indices) {
        if (array[i] == element) {
            foundElement(i, array[i])
            return
        }
    }
    foundElement(-1, null)
}

// Multiple type parameters
fun <X, Y> justForTesting(param1: X, param2: Y) {
    println("param1: $param1, param2: $param2")
}

fun main() {
    // Non-generic - only Int
    val arrayUtil = ArrayUtil(arrayOf(1, 2, 3, 4, 5))

    // Generic class with Int
    val arrayUtil1 = ArrayUtil1<Int>(arrayOf(1, 2, 3, 4, 5, 6))

    // Generic class with String
    val arrayUtil2 = ArrayUtil1<String>(arrayOf("1", "2", "3", "4", "5", "6"))

    arrayUtil1.findElement(4) { index, element ->
        println("index: $index, element: $element")
    }

    arrayUtil2.findElement("2") { index, element ->
        println("index: $index, element: $element")
    }

    // Generic function
    findElement(arrayOf(1, 2, 3, 4, 5, 6), 6) { index, element ->
        println("index: $index, element: $element")
    }

    // Multiple type parameters
    justForTesting("kishor", 27)
}

Key Points:

  • <T> defines a type parameter
  • You can have multiple type parameters: <T, X, Y>
  • Type inference often lets you omit the type specification

💡 Pro Tip: Use generics whenever you find yourself duplicating code for different types!

#Generics #AdvancedKotlin #KotlinBasics #Kotlin #AndroidDev #MobileDevelopment #KotlinFundamentals #KMP #CMP #KotlinDeveloper

🔗 Resources 💻 Code: GitHub Repository (Recommended — Open in IntelliJ IDEA) 🎮 Try it: Kotlin Playground 💼 Connect: LinkedIn Profile 📝 More Posts: My Medium Profile

This is Part 13 of Advanced Kotlin series. Next: Post 14: Nested and Inner Classes in Kotlin


메타데이터
post_id
9b92c01f8281
slug
advanced-kotlin-part-13-generics-in-kotlin-write-once-use-everywhere-9b92c01f8281
url
https://medium.com/@kishorramani/advanced-kotlin-part-13-generics-in-kotlin-write-once-use-everywhere-9b92c01f8281
canonical_url
https://medium.com/@kishorramani/advanced-kotlin-part-13-generics-in-kotlin-write-once-use-everywhere-9b92c01f8281
author_url
https://medium.com/@kishorramani
status
ok
fetched_at
2026-07-24 09:03:45