Ashika Umanga Umagiliya
Ashika Umanga Umagiliya

Reputation: 9158

Java8 Stream : Collect elements after a condition is met

My POJO is as follows

class EventUser {
  private id;
  private userId;
  private eventId;
}

I retrieve EventUser object as follows:

List<EventUser> eventUsers = eventUserRepository.findByUserId(userId);

Say the 'eventUsers' is as follows:

[
{"id":"id200","userId":"001","eventId":"1010"},
{"id":"id101","userId":"001","eventId":"4212"},
{"id":"id402","userId":"001","eventId":"1221"},
{"id":"id301","userId":"001","eventId":"2423"},
{"id":"id701","userId":"001","eventId":"5423"},
{"id":"id601","userId":"001","eventId":"7423"}
]

Using streaming, and without using any intermediate variable , how can I filter and collect events after a given EventUser.id: ex:

List<EventUser> filteredByOffSet = eventUsers.stream.SOMEFILTER_AND_COLLECT("id301");

the result should be :

[{"id":"id301","userId":"001","eventId":"2423"},
{"id":"id701","userId":"001","eventId":"5423"},
{"id":"id601","userId":"001","eventId":"7423"}]

Upvotes: 29

Views: 15163

Answers (4)

Peter Lawrey
Peter Lawrey

Reputation: 533482

In Java 8 you need a stateful filter

public static <T> Predicate<T> from(Predicate<T> test) {
    boolean[] found = { false };
    // once found, always true
    return t -> found[0] || (found[0] = test.test(t));
}

NOTE: this only makes sense for single threaded streams.

List<EventUser> filteredByOffSet = 
     eventUsers.stream()
               .filter(from(e -> "id301".equals(e.getId()))
               .collect(Collectors.toList());

Upvotes: 26

jdev
jdev

Reputation: 5622

You cant do that without using any intermediate variables. finding the position and iterate it to the end (see this question below that answer it more precisely) enter link description here

Upvotes: 1

S.K.
S.K.

Reputation: 3677

Find the index of the search item first:

int asInt = IntStream.range(0, list.size())
    .filter(userInd-> list.get(userInd).equals(<criteria>))
    .findFirst()
    .getAsInt();

Get items on and after the index:

list.stream().skip(asInt).collect(Collectors.toList());

Upvotes: 16

Dmitry Gorkovets
Dmitry Gorkovets

Reputation: 2276

Use "dropWhile" from Java 9.

Upvotes: 23

Related Questions