Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

173
Vistas
Any difference between String.getOrElse() and String.elementAtOrElse()?

As the title states:

Is there any difference between String.getOrElse() and String.elementAtOrElse()? From a functional point of view they seem completely identical, maybe some performance difference?

Same question accounts to String.getOrNull() and String.elementAtOrNull().

over 4 years ago · Hanz Gallego
3 Respuestas
Responde la pregunta

0

The very links you included in your question allow you to see the source code of each implementation which tells you that, no, there is no difference.

In fact elementAtOrNull literally just calls getOrNull.

over 4 years ago · Hanz Gallego Denunciar

0

Looking at the implementation in https://github.com/JetBrains/kotlin/blame/master/libraries/stdlib/common/src/generated/_Strings.kt they look identical.

/**
 * Returns a character at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this char sequence.
 * 
 * @sample samples.collections.Collections.Elements.elementAtOrElse
 */
@kotlin.internal.InlineOnly
public inline fun CharSequence.elementAtOrElse(index: Int, defaultValue: (Int) -> Char): Char {
    return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}
/**
 * Returns a character at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this char sequence.
 */
@kotlin.internal.InlineOnly
public inline fun CharSequence.getOrElse(index: Int, defaultValue: (Int) -> Char): Char {
    return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}

I hope somebody else can provide details about the history of this.

over 4 years ago · Hanz Gallego Denunciar

0

To explain the why:

From the issue that added these over at https://youtrack.jetbrains.com/issue/KT-6952 it appears that elementAtOrElse() was added first and named such for compatibility with Iterables, while getOrElse() was added later for compatibility with Lists.

over 4 years ago · Hanz Gallego Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda