Java8 Stream : Collect elements after a condition is met
Clash Royale CLAN TAG#URR8PPP
up vote
24
down vote
favorite
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
add a comment |Â
up vote
24
down vote
favorite
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
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 byeventId
(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
add a comment |Â
up vote
24
down vote
favorite
up vote
24
down vote
favorite
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
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
java java-8 java-stream
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 byeventId
(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
add a comment |Â
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 byeventId
(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
add a comment |Â
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());
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 forcesequential
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 befinal
or effectively final. Thefound
variable can't be modified inside the lambda. It needs to be wrapped in aboolean[1]
orAtomicBoolean
or similar.
â Christoffer Hammarström
Sep 11 at 11:02
 |Â
show 10 more comments
up vote
18
down vote
Use "dropWhile" from Java 9.
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 usingdropWhile
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
add a comment |Â
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());
1
This should be the accepted answer, though I'd useList.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
 |Â
show 1 more comment
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
add a comment |Â
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());
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 forcesequential
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 befinal
or effectively final. Thefound
variable can't be modified inside the lambda. It needs to be wrapped in aboolean[1]
orAtomicBoolean
or similar.
â Christoffer Hammarström
Sep 11 at 11:02
 |Â
show 10 more comments
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());
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 forcesequential
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 befinal
or effectively final. Thefound
variable can't be modified inside the lambda. It needs to be wrapped in aboolean[1]
orAtomicBoolean
or similar.
â Christoffer Hammarström
Sep 11 at 11:02
 |Â
show 10 more comments
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());
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());
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 forcesequential
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 befinal
or effectively final. Thefound
variable can't be modified inside the lambda. It needs to be wrapped in aboolean[1]
orAtomicBoolean
or similar.
â Christoffer Hammarström
Sep 11 at 11:02
 |Â
show 10 more comments
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 forcesequential
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 befinal
or effectively final. Thefound
variable can't be modified inside the lambda. It needs to be wrapped in aboolean[1]
orAtomicBoolean
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
 |Â
show 10 more comments
up vote
18
down vote
Use "dropWhile" from Java 9.
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 usingdropWhile
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
add a comment |Â
up vote
18
down vote
Use "dropWhile" from Java 9.
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 usingdropWhile
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
add a comment |Â
up vote
18
down vote
up vote
18
down vote
Use "dropWhile" from Java 9.
Use "dropWhile" from Java 9.
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 usingdropWhile
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
add a comment |Â
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 usingdropWhile
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
add a comment |Â
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());
1
This should be the accepted answer, though I'd useList.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
 |Â
show 1 more comment
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());
1
This should be the accepted answer, though I'd useList.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
 |Â
show 1 more comment
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());
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());
answered Sep 11 at 5:59
S.K.
1,721717
1,721717
1
This should be the accepted answer, though I'd useList.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
 |Â
show 1 more comment
1
This should be the accepted answer, though I'd useList.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
 |Â
show 1 more comment
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
add a comment |Â
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
add a comment |Â
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
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
answered Sep 11 at 6:07
jdev
1,9581815
1,9581815
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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