Java 8中有一个流方法limit:

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?

推荐答案

Based on the documentation:

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

Kotlin相关问答推荐

在KMP中使用koin将来自Android的上下文注入到SQLDelight Driver中

我需要后台工作才能使用卡夫卡的消息吗?

为什么Kotlin有次构造函数和init块?

如何将 `when` 与 2 个密封类一起使用并获取内部值?

为什么 trySend 会发出假数据?

如何在 Kotlin 中实现 Composition UML 关系?

关于 Kotlin 函数类型转换的问题

如何退出 Kotlinc 命令行编译器

Kotlin - mutableMapOf() 会保留我输入的顺序

Android Room - error: Cannot figure out how to save this field into database

无法为 retrofit2.Call 调用无参数构造函数

Kotlin如何分派invoke操作符?

Jetpack Compose – LazyColumn 不重组

如何在 Jetpack Compose 的 LazyColumn/LazyRow 中禁用和启用滚动?

我们如何在Java注释声明中引用Kotlin常量?

Kotlin 中更好的回调方法是什么?侦听器与高阶函数

Kotlin数据类打包

Android:Exoplayer - ExtractorMediaSource 已弃用

如何在伴随对象中使用泛型

Kotlin反射不可用