Sunil Prabakar
Sunil Prabakar

Reputation: 442

Skip and Take option for java arraylist?

I am very new to java, i know skip and take are very easy using linq in dot net. But i dont know how to achieve skip and take process in java for array list. any linq like option available in java?

Upvotes: 3

Views: 3895

Answers (2)

Squimmy
Squimmy

Reputation: 587

Arnaud Denoyelle is correct; you want to use streams.

The Java 8 equivalents of .NET's .skip() and .take() are .skip() and .limit()

Upvotes: 8

Arnaud Denoyelle
Arnaud Denoyelle

Reputation: 31245

I looked at this link to understand what are "skip and take".

From Java 8, you can do such things with Streams. Stream.filter() enables you to define a Predicate which is your equivalent of take(). You can obtain skip by filtering on the opposite Predicate :

List<Integer> list = [...];

List<Integer> result = list.stream()
    .filter(i -> i % 2 == 0) //Use any Predicate you want.
    .collect(Collectors.toList()); //Convert the Stream back to a list

Upvotes: 5

Related Questions