Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit function in Kotlin

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?

like image 931
Letfar Avatar asked Feb 06 '16 21:02

Letfar


People also ask

What is downTo in Kotlin?

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.

Is Kotlin downTo inclusive?

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.

What is basic difference between fold and reduce in Kotlin?

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.


1 Answers

Based on the documentation:

list.take(3).forEach(::System.out.println)
like image 148
Jean Logeart Avatar answered Sep 17 '22 12:09

Jean Logeart