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?
Returns a progression from this value down to the specified to value with the step -1. The to value should be less than or equal to this value. If the to value is greater than this value the returned progression is empty.
operator or with the rangeTo and downTo functions. Kotlin ranges are inclusive by default; that is, 1.. 3 creates a range of 1, 2, 3 values. The distance between two values is defined by the step; the default step is 1.
Fold and reduce The difference between the two functions is that fold() takes an initial value and uses it as the accumulated value on the first step, whereas the first step of reduce() uses the first and the second elements as operation arguments on the first step.
Based on the documentation:
list.take(3).forEach(::System.out.println)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With