Java8 Stream : Collect elements after a condition is met

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
24
down vote

favorite
3












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"]









share|improve this question



















  • 1




    @ernest_k resulting Collection should after have elements after "id301" (not before)
    – Ashika Umanga Umagiliya
    Sep 11 at 5:56










  • Will the input list (and thereby initial stream) always be sorted by eventId (the example leaves that impression)?
    – Janus Varmarken
    Sep 11 at 6:15











  • @JanusVarmarken not sorted by eventId..
    – Ashika Umanga Umagiliya
    Sep 11 at 6:18






  • 1




    Possible duplicate of Equivalent of Scala dropWhile
    – Olivier Grégoire
    Sep 11 at 8:24














up vote
24
down vote

favorite
3












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"]









share|improve this question



















  • 1




    @ernest_k resulting Collection should after have elements after "id301" (not before)
    – Ashika Umanga Umagiliya
    Sep 11 at 5:56










  • Will the input list (and thereby initial stream) always be sorted by eventId (the example leaves that impression)?
    – Janus Varmarken
    Sep 11 at 6:15











  • @JanusVarmarken not sorted by eventId..
    – Ashika Umanga Umagiliya
    Sep 11 at 6:18






  • 1




    Possible duplicate of Equivalent of Scala dropWhile
    – Olivier Grégoire
    Sep 11 at 8:24












up vote
24
down vote

favorite
3









up vote
24
down vote

favorite
3






3





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"]









share|improve this question















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"]






java java-8 java-stream






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 11 at 7:50









Hulk

2,14411432




2,14411432










asked Sep 11 at 5:49









Ashika Umanga Umagiliya

2,8311775150




2,8311775150







  • 1




    @ernest_k resulting Collection should after have elements after "id301" (not before)
    – Ashika Umanga Umagiliya
    Sep 11 at 5:56










  • Will the input list (and thereby initial stream) always be sorted by eventId (the example leaves that impression)?
    – Janus Varmarken
    Sep 11 at 6:15











  • @JanusVarmarken not sorted by eventId..
    – Ashika Umanga Umagiliya
    Sep 11 at 6:18






  • 1




    Possible duplicate of Equivalent of Scala dropWhile
    – Olivier Grégoire
    Sep 11 at 8:24












  • 1




    @ernest_k resulting Collection should after have elements after "id301" (not before)
    – Ashika Umanga Umagiliya
    Sep 11 at 5:56










  • Will the input list (and thereby initial stream) always be sorted by eventId (the example leaves that impression)?
    – Janus Varmarken
    Sep 11 at 6:15











  • @JanusVarmarken not sorted by eventId..
    – Ashika Umanga Umagiliya
    Sep 11 at 6:18






  • 1




    Possible duplicate of Equivalent of Scala dropWhile
    – Olivier Grégoire
    Sep 11 at 8:24







1




1




@ernest_k resulting Collection should after have elements after "id301" (not before)
– Ashika Umanga Umagiliya
Sep 11 at 5:56




@ernest_k resulting Collection should after have elements after "id301" (not before)
– Ashika Umanga Umagiliya
Sep 11 at 5:56












Will the input list (and thereby initial stream) always be sorted by eventId (the example leaves that impression)?
– Janus Varmarken
Sep 11 at 6:15





Will the input list (and thereby initial stream) always be sorted by eventId (the example leaves that impression)?
– Janus Varmarken
Sep 11 at 6:15













@JanusVarmarken not sorted by eventId..
– Ashika Umanga Umagiliya
Sep 11 at 6:18




@JanusVarmarken not sorted by eventId..
– Ashika Umanga Umagiliya
Sep 11 at 6:18




1




1




Possible duplicate of Equivalent of Scala dropWhile
– Olivier Grégoire
Sep 11 at 8:24




Possible duplicate of Equivalent of Scala dropWhile
– Olivier Grégoire
Sep 11 at 8:24












4 Answers
4






active

oldest

votes

















up vote
21
down vote



accepted










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]


NOTE: this only makes sense for single threaded streams.



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





share|improve this answer


















  • 2




    I like this one - it's a lot more concise than your answer on this question
    – Hulk
    Sep 11 at 7:36






  • 1




    @PeterLawrey yeah i prefer this !
    – Ashika Umanga Umagiliya
    Sep 11 at 7:45






  • 4




    @PeterLawrey this is a really bad idea, I would at least force sequential here
    – Eugene
    Sep 11 at 8:34






  • 2




    @PeterLawrey btw there is back-port for java-8 for dropWhile
    – Eugene
    Sep 11 at 8:38






  • 2




    This does not work as written. Variables in a lambda need to be final or effectively final. The found variable can't be modified inside the lambda. It needs to be wrapped in a boolean[1] or AtomicBoolean or similar.
    – Christoffer Hammarström
    Sep 11 at 11:02


















up vote
18
down vote













Use "dropWhile" from Java 9.






share|improve this answer




















  • thanks but sadly I am on java 8
    – Ashika Umanga Umagiliya
    Sep 11 at 5:58










  • @AshikaUmangaUmagiliya thx to all mighty Holger this is possible in java-8 too. Dmitry just link that to your answer and this should be the accepted one IMO. here is the link
    – Eugene
    Sep 11 at 8:37






  • 7




    Please add an example using dropWhile in your answer. Right now this is a link-only answer and at risk of being deleted :)
    – Jesse de Bruijne
    Sep 11 at 13:53






  • 1




    @DmitryGorkovets consider adding the stream should be ordered when using this method. If it's unordered the behavior is non-deterministic.
    – Brad Cupit
    Sep 11 at 14:19







  • 3




    @AshikaUmangaUmagiliya if you're stuck with Java8, I recommend StreamEx library which offers dropWhile (among many others).
    – Jean-François Savard
    Sep 11 at 14:58

















up vote
13
down vote













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());





share|improve this answer
















  • 1




    This should be the accepted answer, though I'd use List.subList(from, to) instead of skip
    – Federico Peralta Schaffner
    Sep 11 at 12:15










  • @FedericoPeraltaSchaffner Why go through the work of creating a sub list when you can just skip?
    – Tim B
    Sep 11 at 12:28






  • 1




    @TimB subList doesn't create a new list, it's just a view of a range of the original list
    – Federico Peralta Schaffner
    Sep 11 at 12:33










  • @FedericoPeraltaSchaffner And the view is an object. I agree it's not as much work as copying the list, it's still greater than zero.
    – Tim B
    Sep 11 at 14:51










  • @TimB maybe I wasn't clear enough. I meant subList instead of stream, skip and collect to a new list
    – Federico Peralta Schaffner
    Sep 11 at 14:56

















up vote
1
down vote













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






share|improve this answer




















    Your Answer





    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52269422%2fjava8-stream-collect-elements-after-a-condition-is-met%23new-answer', 'question_page');

    );

    Post as a guest






























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    21
    down vote



    accepted










    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]


    NOTE: this only makes sense for single threaded streams.



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





    share|improve this answer


















    • 2




      I like this one - it's a lot more concise than your answer on this question
      – Hulk
      Sep 11 at 7:36






    • 1




      @PeterLawrey yeah i prefer this !
      – Ashika Umanga Umagiliya
      Sep 11 at 7:45






    • 4




      @PeterLawrey this is a really bad idea, I would at least force sequential here
      – Eugene
      Sep 11 at 8:34






    • 2




      @PeterLawrey btw there is back-port for java-8 for dropWhile
      – Eugene
      Sep 11 at 8:38






    • 2




      This does not work as written. Variables in a lambda need to be final or effectively final. The found variable can't be modified inside the lambda. It needs to be wrapped in a boolean[1] or AtomicBoolean or similar.
      – Christoffer Hammarström
      Sep 11 at 11:02















    up vote
    21
    down vote



    accepted










    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]


    NOTE: this only makes sense for single threaded streams.



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





    share|improve this answer


















    • 2




      I like this one - it's a lot more concise than your answer on this question
      – Hulk
      Sep 11 at 7:36






    • 1




      @PeterLawrey yeah i prefer this !
      – Ashika Umanga Umagiliya
      Sep 11 at 7:45






    • 4




      @PeterLawrey this is a really bad idea, I would at least force sequential here
      – Eugene
      Sep 11 at 8:34






    • 2




      @PeterLawrey btw there is back-port for java-8 for dropWhile
      – Eugene
      Sep 11 at 8:38






    • 2




      This does not work as written. Variables in a lambda need to be final or effectively final. The found variable can't be modified inside the lambda. It needs to be wrapped in a boolean[1] or AtomicBoolean or similar.
      – Christoffer Hammarström
      Sep 11 at 11:02













    up vote
    21
    down vote



    accepted







    up vote
    21
    down vote



    accepted






    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]


    NOTE: this only makes sense for single threaded streams.



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





    share|improve this answer














    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]


    NOTE: this only makes sense for single threaded streams.



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






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Sep 12 at 10:04

























    answered Sep 11 at 7:30









    Peter Lawrey

    430k54539917




    430k54539917







    • 2




      I like this one - it's a lot more concise than your answer on this question
      – Hulk
      Sep 11 at 7:36






    • 1




      @PeterLawrey yeah i prefer this !
      – Ashika Umanga Umagiliya
      Sep 11 at 7:45






    • 4




      @PeterLawrey this is a really bad idea, I would at least force sequential here
      – Eugene
      Sep 11 at 8:34






    • 2




      @PeterLawrey btw there is back-port for java-8 for dropWhile
      – Eugene
      Sep 11 at 8:38






    • 2




      This does not work as written. Variables in a lambda need to be final or effectively final. The found variable can't be modified inside the lambda. It needs to be wrapped in a boolean[1] or AtomicBoolean or similar.
      – Christoffer Hammarström
      Sep 11 at 11:02













    • 2




      I like this one - it's a lot more concise than your answer on this question
      – Hulk
      Sep 11 at 7:36






    • 1




      @PeterLawrey yeah i prefer this !
      – Ashika Umanga Umagiliya
      Sep 11 at 7:45






    • 4




      @PeterLawrey this is a really bad idea, I would at least force sequential here
      – Eugene
      Sep 11 at 8:34






    • 2




      @PeterLawrey btw there is back-port for java-8 for dropWhile
      – Eugene
      Sep 11 at 8:38






    • 2




      This does not work as written. Variables in a lambda need to be final or effectively final. The found variable can't be modified inside the lambda. It needs to be wrapped in a boolean[1] or AtomicBoolean or similar.
      – Christoffer Hammarström
      Sep 11 at 11:02








    2




    2




    I like this one - it's a lot more concise than your answer on this question
    – Hulk
    Sep 11 at 7:36




    I like this one - it's a lot more concise than your answer on this question
    – Hulk
    Sep 11 at 7:36




    1




    1




    @PeterLawrey yeah i prefer this !
    – Ashika Umanga Umagiliya
    Sep 11 at 7:45




    @PeterLawrey yeah i prefer this !
    – Ashika Umanga Umagiliya
    Sep 11 at 7:45




    4




    4




    @PeterLawrey this is a really bad idea, I would at least force sequential here
    – Eugene
    Sep 11 at 8:34




    @PeterLawrey this is a really bad idea, I would at least force sequential here
    – Eugene
    Sep 11 at 8:34




    2




    2




    @PeterLawrey btw there is back-port for java-8 for dropWhile
    – Eugene
    Sep 11 at 8:38




    @PeterLawrey btw there is back-port for java-8 for dropWhile
    – Eugene
    Sep 11 at 8:38




    2




    2




    This does not work as written. Variables in a lambda need to be final or effectively final. The found variable can't be modified inside the lambda. It needs to be wrapped in a boolean[1] or AtomicBoolean or similar.
    – Christoffer Hammarström
    Sep 11 at 11:02





    This does not work as written. Variables in a lambda need to be final or effectively final. The found variable can't be modified inside the lambda. It needs to be wrapped in a boolean[1] or AtomicBoolean or similar.
    – Christoffer Hammarström
    Sep 11 at 11:02













    up vote
    18
    down vote













    Use "dropWhile" from Java 9.






    share|improve this answer




















    • thanks but sadly I am on java 8
      – Ashika Umanga Umagiliya
      Sep 11 at 5:58










    • @AshikaUmangaUmagiliya thx to all mighty Holger this is possible in java-8 too. Dmitry just link that to your answer and this should be the accepted one IMO. here is the link
      – Eugene
      Sep 11 at 8:37






    • 7




      Please add an example using dropWhile in your answer. Right now this is a link-only answer and at risk of being deleted :)
      – Jesse de Bruijne
      Sep 11 at 13:53






    • 1




      @DmitryGorkovets consider adding the stream should be ordered when using this method. If it's unordered the behavior is non-deterministic.
      – Brad Cupit
      Sep 11 at 14:19







    • 3




      @AshikaUmangaUmagiliya if you're stuck with Java8, I recommend StreamEx library which offers dropWhile (among many others).
      – Jean-François Savard
      Sep 11 at 14:58














    up vote
    18
    down vote













    Use "dropWhile" from Java 9.






    share|improve this answer




















    • thanks but sadly I am on java 8
      – Ashika Umanga Umagiliya
      Sep 11 at 5:58










    • @AshikaUmangaUmagiliya thx to all mighty Holger this is possible in java-8 too. Dmitry just link that to your answer and this should be the accepted one IMO. here is the link
      – Eugene
      Sep 11 at 8:37






    • 7




      Please add an example using dropWhile in your answer. Right now this is a link-only answer and at risk of being deleted :)
      – Jesse de Bruijne
      Sep 11 at 13:53






    • 1




      @DmitryGorkovets consider adding the stream should be ordered when using this method. If it's unordered the behavior is non-deterministic.
      – Brad Cupit
      Sep 11 at 14:19







    • 3




      @AshikaUmangaUmagiliya if you're stuck with Java8, I recommend StreamEx library which offers dropWhile (among many others).
      – Jean-François Savard
      Sep 11 at 14:58












    up vote
    18
    down vote










    up vote
    18
    down vote









    Use "dropWhile" from Java 9.






    share|improve this answer












    Use "dropWhile" from Java 9.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Sep 11 at 5:57









    Dmitry Gorkovets

    1,420211




    1,420211











    • thanks but sadly I am on java 8
      – Ashika Umanga Umagiliya
      Sep 11 at 5:58










    • @AshikaUmangaUmagiliya thx to all mighty Holger this is possible in java-8 too. Dmitry just link that to your answer and this should be the accepted one IMO. here is the link
      – Eugene
      Sep 11 at 8:37






    • 7




      Please add an example using dropWhile in your answer. Right now this is a link-only answer and at risk of being deleted :)
      – Jesse de Bruijne
      Sep 11 at 13:53






    • 1




      @DmitryGorkovets consider adding the stream should be ordered when using this method. If it's unordered the behavior is non-deterministic.
      – Brad Cupit
      Sep 11 at 14:19







    • 3




      @AshikaUmangaUmagiliya if you're stuck with Java8, I recommend StreamEx library which offers dropWhile (among many others).
      – Jean-François Savard
      Sep 11 at 14:58
















    • thanks but sadly I am on java 8
      – Ashika Umanga Umagiliya
      Sep 11 at 5:58










    • @AshikaUmangaUmagiliya thx to all mighty Holger this is possible in java-8 too. Dmitry just link that to your answer and this should be the accepted one IMO. here is the link
      – Eugene
      Sep 11 at 8:37






    • 7




      Please add an example using dropWhile in your answer. Right now this is a link-only answer and at risk of being deleted :)
      – Jesse de Bruijne
      Sep 11 at 13:53






    • 1




      @DmitryGorkovets consider adding the stream should be ordered when using this method. If it's unordered the behavior is non-deterministic.
      – Brad Cupit
      Sep 11 at 14:19







    • 3




      @AshikaUmangaUmagiliya if you're stuck with Java8, I recommend StreamEx library which offers dropWhile (among many others).
      – Jean-François Savard
      Sep 11 at 14:58















    thanks but sadly I am on java 8
    – Ashika Umanga Umagiliya
    Sep 11 at 5:58




    thanks but sadly I am on java 8
    – Ashika Umanga Umagiliya
    Sep 11 at 5:58












    @AshikaUmangaUmagiliya thx to all mighty Holger this is possible in java-8 too. Dmitry just link that to your answer and this should be the accepted one IMO. here is the link
    – Eugene
    Sep 11 at 8:37




    @AshikaUmangaUmagiliya thx to all mighty Holger this is possible in java-8 too. Dmitry just link that to your answer and this should be the accepted one IMO. here is the link
    – Eugene
    Sep 11 at 8:37




    7




    7




    Please add an example using dropWhile in your answer. Right now this is a link-only answer and at risk of being deleted :)
    – Jesse de Bruijne
    Sep 11 at 13:53




    Please add an example using dropWhile in your answer. Right now this is a link-only answer and at risk of being deleted :)
    – Jesse de Bruijne
    Sep 11 at 13:53




    1




    1




    @DmitryGorkovets consider adding the stream should be ordered when using this method. If it's unordered the behavior is non-deterministic.
    – Brad Cupit
    Sep 11 at 14:19





    @DmitryGorkovets consider adding the stream should be ordered when using this method. If it's unordered the behavior is non-deterministic.
    – Brad Cupit
    Sep 11 at 14:19





    3




    3




    @AshikaUmangaUmagiliya if you're stuck with Java8, I recommend StreamEx library which offers dropWhile (among many others).
    – Jean-François Savard
    Sep 11 at 14:58




    @AshikaUmangaUmagiliya if you're stuck with Java8, I recommend StreamEx library which offers dropWhile (among many others).
    – Jean-François Savard
    Sep 11 at 14:58










    up vote
    13
    down vote













    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());





    share|improve this answer
















    • 1




      This should be the accepted answer, though I'd use List.subList(from, to) instead of skip
      – Federico Peralta Schaffner
      Sep 11 at 12:15










    • @FedericoPeraltaSchaffner Why go through the work of creating a sub list when you can just skip?
      – Tim B
      Sep 11 at 12:28






    • 1




      @TimB subList doesn't create a new list, it's just a view of a range of the original list
      – Federico Peralta Schaffner
      Sep 11 at 12:33










    • @FedericoPeraltaSchaffner And the view is an object. I agree it's not as much work as copying the list, it's still greater than zero.
      – Tim B
      Sep 11 at 14:51










    • @TimB maybe I wasn't clear enough. I meant subList instead of stream, skip and collect to a new list
      – Federico Peralta Schaffner
      Sep 11 at 14:56














    up vote
    13
    down vote













    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());





    share|improve this answer
















    • 1




      This should be the accepted answer, though I'd use List.subList(from, to) instead of skip
      – Federico Peralta Schaffner
      Sep 11 at 12:15










    • @FedericoPeraltaSchaffner Why go through the work of creating a sub list when you can just skip?
      – Tim B
      Sep 11 at 12:28






    • 1




      @TimB subList doesn't create a new list, it's just a view of a range of the original list
      – Federico Peralta Schaffner
      Sep 11 at 12:33










    • @FedericoPeraltaSchaffner And the view is an object. I agree it's not as much work as copying the list, it's still greater than zero.
      – Tim B
      Sep 11 at 14:51










    • @TimB maybe I wasn't clear enough. I meant subList instead of stream, skip and collect to a new list
      – Federico Peralta Schaffner
      Sep 11 at 14:56












    up vote
    13
    down vote










    up vote
    13
    down vote









    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());





    share|improve this answer












    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());






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Sep 11 at 5:59









    S.K.

    1,721717




    1,721717







    • 1




      This should be the accepted answer, though I'd use List.subList(from, to) instead of skip
      – Federico Peralta Schaffner
      Sep 11 at 12:15










    • @FedericoPeraltaSchaffner Why go through the work of creating a sub list when you can just skip?
      – Tim B
      Sep 11 at 12:28






    • 1




      @TimB subList doesn't create a new list, it's just a view of a range of the original list
      – Federico Peralta Schaffner
      Sep 11 at 12:33










    • @FedericoPeraltaSchaffner And the view is an object. I agree it's not as much work as copying the list, it's still greater than zero.
      – Tim B
      Sep 11 at 14:51










    • @TimB maybe I wasn't clear enough. I meant subList instead of stream, skip and collect to a new list
      – Federico Peralta Schaffner
      Sep 11 at 14:56












    • 1




      This should be the accepted answer, though I'd use List.subList(from, to) instead of skip
      – Federico Peralta Schaffner
      Sep 11 at 12:15










    • @FedericoPeraltaSchaffner Why go through the work of creating a sub list when you can just skip?
      – Tim B
      Sep 11 at 12:28






    • 1




      @TimB subList doesn't create a new list, it's just a view of a range of the original list
      – Federico Peralta Schaffner
      Sep 11 at 12:33










    • @FedericoPeraltaSchaffner And the view is an object. I agree it's not as much work as copying the list, it's still greater than zero.
      – Tim B
      Sep 11 at 14:51










    • @TimB maybe I wasn't clear enough. I meant subList instead of stream, skip and collect to a new list
      – Federico Peralta Schaffner
      Sep 11 at 14:56







    1




    1




    This should be the accepted answer, though I'd use List.subList(from, to) instead of skip
    – Federico Peralta Schaffner
    Sep 11 at 12:15




    This should be the accepted answer, though I'd use List.subList(from, to) instead of skip
    – Federico Peralta Schaffner
    Sep 11 at 12:15












    @FedericoPeraltaSchaffner Why go through the work of creating a sub list when you can just skip?
    – Tim B
    Sep 11 at 12:28




    @FedericoPeraltaSchaffner Why go through the work of creating a sub list when you can just skip?
    – Tim B
    Sep 11 at 12:28




    1




    1




    @TimB subList doesn't create a new list, it's just a view of a range of the original list
    – Federico Peralta Schaffner
    Sep 11 at 12:33




    @TimB subList doesn't create a new list, it's just a view of a range of the original list
    – Federico Peralta Schaffner
    Sep 11 at 12:33












    @FedericoPeraltaSchaffner And the view is an object. I agree it's not as much work as copying the list, it's still greater than zero.
    – Tim B
    Sep 11 at 14:51




    @FedericoPeraltaSchaffner And the view is an object. I agree it's not as much work as copying the list, it's still greater than zero.
    – Tim B
    Sep 11 at 14:51












    @TimB maybe I wasn't clear enough. I meant subList instead of stream, skip and collect to a new list
    – Federico Peralta Schaffner
    Sep 11 at 14:56




    @TimB maybe I wasn't clear enough. I meant subList instead of stream, skip and collect to a new list
    – Federico Peralta Schaffner
    Sep 11 at 14:56










    up vote
    1
    down vote













    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






    share|improve this answer
























      up vote
      1
      down vote













      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






      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        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






        share|improve this answer












        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 11 at 6:07









        jdev

        1,9581815




        1,9581815



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52269422%2fjava8-stream-collect-elements-after-a-condition-is-met%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay