Home > Web Front-end > JS Tutorial > Kotlin From Scratch: Ranges and Collections

Kotlin From Scratch: Ranges and Collections

Lisa Kudrow
Release: 2025-03-15 10:33:10
Original
886 people have browsed it

Kotlin From Scratch: Ranges and Collections

Kotlin, a modern, open-source language compiling to Java bytecode, enhances Android development. Building on a previous tutorial covering nullability, loops, and conditions, this guide explores Kotlin's ranges and collections API.

1. Ranges

A Kotlin range defines a closed interval between a start and end value (inclusive). Here's how to create ranges:

  • until() and downTo(): These infix functions create number ranges with a specified step. Infix functions allow function calls without dot or parenthesis notation.

    For example:

     // 1 3 5 7
     for (i in 1 until 8 step 2) {
         print("$i ")
     }
    Copy after login

2. Collections

Collections store groups of related objects. Kotlin's collections API, built upon Java's, offers various interfaces linked to their implementations at compile time. Examples include:

  • ArrayList: The add() function inserts elements; elements at specific indices can be directly modified.

     val stringList: ArrayList<string> = arrayListOf("Hello", "You", "There")
     stringList[2] = "Here"
     stringList.add(1, "Are")
     // Prints: [Hello, Are, You, Here]
     println(stringList)</string>
    Copy after login
  • LinkedHashSet: This mutable set maintains insertion order.

     val intsLinkedHashSet: LinkedHashSet<int> = linkedSetOf(5, 2, 7, 2, 5)
     intsLinkedHashSet.add(4)
     intsLinkedHashSet.remove(2)
     // [5, 7, 4]
     println(intsLinkedHashSet)</int>
    Copy after login
  • LinkedHashMap: A mutable map maintaining entry iteration order using a doubly linked list.

     val postalCodesHashMap: LinkedHashMap<string string> =
         linkedMapOf("NG" to "Nigeria", "AU" to "Australia", "CA" to "Canada")
     postalCodesHashMap.put("NA", "Namibia")
     postalCodesHashMap.remove("AU")
     // {NG=Nigeria, CA=Canada, NA=Namibia}
     println(postalCodesHashMap)</string>
    Copy after login
  • maxOrNull(): Returns the maximum element (note: max() is deprecated).

     val intList: List<int> = listOf(1, 3, 4)
     print(intList.maxOrNull()) // will print 4</int>
    Copy after login
  • drop(): Returns a new collection excluding the first n elements.

     print(stringList.drop(2)) // will print [You, Here]
    Copy after login
  • plus(): Adds an element to the collection, returning a new collection.

     print(intList.plus(6)) // will print [1, 3, 4, 6]
    Copy after login
  • minus(): Removes an element, returning a new collection.

     print(intList.minus(3)) // will print [1, 4]
    Copy after login
  • average(): Calculates the average of elements.

     print(intList.average()) // will print 2.6666666666666665
    Copy after login

Many more extension functions are available in Kotlin's standard library. Consult the documentation for a comprehensive list.

Conclusion

This tutorial covered Kotlin's ranges and collections API. The next tutorial in this series will introduce Kotlin functions. For further Kotlin learning, refer to the official Kotlin documentation.

The above is the detailed content of Kotlin From Scratch: Ranges and Collections. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template