Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

669
Visualizações
Kotlin Stream peek(...) method

What is the best alternative in Kotlin to java.util.stream.Stream<>.peek(...)?

https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#peek-java.util.function.Consumer-

Seems there are no alternative intermediate operations:

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.streams/index.html

I found only terminating forEach(...)

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The Stream alternative in Kotlin is Sequences.

 listOf(1, 2, 3, 4, 5)
    .asSequence()
    .filter { it < 3 }
    .onEach { println("filtered $it") }
    .map { it * 10 }
    .forEach { println("final: $it") }

There's onEach to do what peek does.

Fun fact: Kotlin also wanted to call their sequences "Streams" before it was clear that Java would do the same, so they renamed it to "Sequences".

over 4 years ago · Santiago Trujillo Relatório

0

Firstly, unlike Java in Kotlin, you can perform stream processing (map/reduce operations) on any type of collection for example:

val list = ArrayList<Int>()
list.forEach {  }
list.onEach {  } 

However the operations defined in this way are not lazily evaluated and if we need lazy evaluation by applying the method .asSequence() and generate a stream from collection.

Finally to answer your question onEach() is the equivalent of peek()

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda