Limit function in Kotlin

2019-02-16 14:35发布

问题:

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?

回答1:

Based on the documentation:

list.take(3).forEach(::System.out.println)