17

There is a stream method limit in Java 8:

package com.concretepage.util.stream;
import java.util.Arrays;
import java.util.List;
public class LimitDemo {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("AA","BB","CC","DD","EE");    
        list.stream().limit(3).forEach(s->System.out.println(s));
    }        
} 

output:

AA
BB
CC 

What is the name of analog in Kotlin, or how to do it better by another way?

Letfar
  • 3,253
  • 6
  • 25
  • 35
  • 3
    See http://stackoverflow.com/questions/34642254/what-java-8-stream-collect-equivalents-are-available-in-the-standard-kotlin-libr – Alexis C. Feb 06 '16 at 22:20
  • If you view the API reference you can see extension functions on collections, which include the answer. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/index.html, other collection docs are new at https://kotlinlang.org/docs/reference/collections.html – Jayson Minard Feb 10 '16 at 16:44

1 Answers1

51

Based on the documentation:

list.take(3).forEach(::System.out.println)
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118