Java 8 Streams reduce remove duplicates keeping the most recent entry
I have a Java bean, like
class EmployeeContract {
Long id;
Date date;
getter/setter
}
If a have a long list of these, in which we have duplicates by id but with different date, such as:
1, 2015/07/07
1, 2018/07/08
2, 2015/07/08
2, 2018/07/09
How can I reduce such a list keeping only the entries with the most recent date, such as:
1, 2018/07/08
2, 2018/07/09
?
Preferably using Java 8...
I've started with something like:
contract.stream()
.collect(Collectors.groupingBy(EmployeeContract::getId, Collectors.mapping(EmployeeContract::getId, Collectors.toList())))
.entrySet().stream().findFirst();
That gives me the mapping within individual groups, but I'm stuck as to how to collect that into a result list - my streams are not too strong I'm afraid...
java java-8 java-stream reduction
|
show 2 more comments
I have a Java bean, like
class EmployeeContract {
Long id;
Date date;
getter/setter
}
If a have a long list of these, in which we have duplicates by id but with different date, such as:
1, 2015/07/07
1, 2018/07/08
2, 2015/07/08
2, 2018/07/09
How can I reduce such a list keeping only the entries with the most recent date, such as:
1, 2018/07/08
2, 2018/07/09
?
Preferably using Java 8...
I've started with something like:
contract.stream()
.collect(Collectors.groupingBy(EmployeeContract::getId, Collectors.mapping(EmployeeContract::getId, Collectors.toList())))
.entrySet().stream().findFirst();
That gives me the mapping within individual groups, but I'm stuck as to how to collect that into a result list - my streams are not too strong I'm afraid...
java java-8 java-stream reduction
2
I wanted to post an answer but this one was closed too fast...yourList.stream() .collect(Collectors.toMap( EmployeeContract::getId, Function.identity(), BinaryOperator.maxBy(Comparator.comparing(EmployeeContract::getDate).reversed())) ) .values();
– Eugene
Nov 27 '18 at 17:22
3
@Eugene instead ofBinaryOperator.maxBy( … .reversed())
, you can useBinaryOperator.minBy(…)
. Though in this case, it looks like the OP wantsmaxBy
, without.reversed()
.
– Holger
Nov 27 '18 at 17:43
2
@Holger and given that this(values()
) would return aCollection<EmployeeContract>
and not precisely aList<EmployeeContract>
, is there a concise way to resolve that?
– nullpointer
Nov 27 '18 at 17:51
1
Given there is a valid discussion and it's a bona fide quiestion, perhaps it's worth to un-hold this question?
– Nestor Milyaev
Nov 27 '18 at 17:53
4
@nullpointer if it really needs to be aList
, you can a) wrap the entire expression in anew ArrayList<>( … )
or b) wrap the collector in aCollectors.collectingAndThen( …, m -> new ArrayList<>(m.values()))
.
– Holger
Nov 27 '18 at 17:57
|
show 2 more comments
I have a Java bean, like
class EmployeeContract {
Long id;
Date date;
getter/setter
}
If a have a long list of these, in which we have duplicates by id but with different date, such as:
1, 2015/07/07
1, 2018/07/08
2, 2015/07/08
2, 2018/07/09
How can I reduce such a list keeping only the entries with the most recent date, such as:
1, 2018/07/08
2, 2018/07/09
?
Preferably using Java 8...
I've started with something like:
contract.stream()
.collect(Collectors.groupingBy(EmployeeContract::getId, Collectors.mapping(EmployeeContract::getId, Collectors.toList())))
.entrySet().stream().findFirst();
That gives me the mapping within individual groups, but I'm stuck as to how to collect that into a result list - my streams are not too strong I'm afraid...
java java-8 java-stream reduction
I have a Java bean, like
class EmployeeContract {
Long id;
Date date;
getter/setter
}
If a have a long list of these, in which we have duplicates by id but with different date, such as:
1, 2015/07/07
1, 2018/07/08
2, 2015/07/08
2, 2018/07/09
How can I reduce such a list keeping only the entries with the most recent date, such as:
1, 2018/07/08
2, 2018/07/09
?
Preferably using Java 8...
I've started with something like:
contract.stream()
.collect(Collectors.groupingBy(EmployeeContract::getId, Collectors.mapping(EmployeeContract::getId, Collectors.toList())))
.entrySet().stream().findFirst();
That gives me the mapping within individual groups, but I'm stuck as to how to collect that into a result list - my streams are not too strong I'm afraid...
java java-8 java-stream reduction
java java-8 java-stream reduction
edited Nov 27 '18 at 17:54
nullpointer
43.8k10102201
43.8k10102201
asked Nov 27 '18 at 17:12
Nestor MilyaevNestor Milyaev
1,1191216
1,1191216
2
I wanted to post an answer but this one was closed too fast...yourList.stream() .collect(Collectors.toMap( EmployeeContract::getId, Function.identity(), BinaryOperator.maxBy(Comparator.comparing(EmployeeContract::getDate).reversed())) ) .values();
– Eugene
Nov 27 '18 at 17:22
3
@Eugene instead ofBinaryOperator.maxBy( … .reversed())
, you can useBinaryOperator.minBy(…)
. Though in this case, it looks like the OP wantsmaxBy
, without.reversed()
.
– Holger
Nov 27 '18 at 17:43
2
@Holger and given that this(values()
) would return aCollection<EmployeeContract>
and not precisely aList<EmployeeContract>
, is there a concise way to resolve that?
– nullpointer
Nov 27 '18 at 17:51
1
Given there is a valid discussion and it's a bona fide quiestion, perhaps it's worth to un-hold this question?
– Nestor Milyaev
Nov 27 '18 at 17:53
4
@nullpointer if it really needs to be aList
, you can a) wrap the entire expression in anew ArrayList<>( … )
or b) wrap the collector in aCollectors.collectingAndThen( …, m -> new ArrayList<>(m.values()))
.
– Holger
Nov 27 '18 at 17:57
|
show 2 more comments
2
I wanted to post an answer but this one was closed too fast...yourList.stream() .collect(Collectors.toMap( EmployeeContract::getId, Function.identity(), BinaryOperator.maxBy(Comparator.comparing(EmployeeContract::getDate).reversed())) ) .values();
– Eugene
Nov 27 '18 at 17:22
3
@Eugene instead ofBinaryOperator.maxBy( … .reversed())
, you can useBinaryOperator.minBy(…)
. Though in this case, it looks like the OP wantsmaxBy
, without.reversed()
.
– Holger
Nov 27 '18 at 17:43
2
@Holger and given that this(values()
) would return aCollection<EmployeeContract>
and not precisely aList<EmployeeContract>
, is there a concise way to resolve that?
– nullpointer
Nov 27 '18 at 17:51
1
Given there is a valid discussion and it's a bona fide quiestion, perhaps it's worth to un-hold this question?
– Nestor Milyaev
Nov 27 '18 at 17:53
4
@nullpointer if it really needs to be aList
, you can a) wrap the entire expression in anew ArrayList<>( … )
or b) wrap the collector in aCollectors.collectingAndThen( …, m -> new ArrayList<>(m.values()))
.
– Holger
Nov 27 '18 at 17:57
2
2
I wanted to post an answer but this one was closed too fast...
yourList.stream() .collect(Collectors.toMap( EmployeeContract::getId, Function.identity(), BinaryOperator.maxBy(Comparator.comparing(EmployeeContract::getDate).reversed())) ) .values();
– Eugene
Nov 27 '18 at 17:22
I wanted to post an answer but this one was closed too fast...
yourList.stream() .collect(Collectors.toMap( EmployeeContract::getId, Function.identity(), BinaryOperator.maxBy(Comparator.comparing(EmployeeContract::getDate).reversed())) ) .values();
– Eugene
Nov 27 '18 at 17:22
3
3
@Eugene instead of
BinaryOperator.maxBy( … .reversed())
, you can use BinaryOperator.minBy(…)
. Though in this case, it looks like the OP wants maxBy
, without .reversed()
.– Holger
Nov 27 '18 at 17:43
@Eugene instead of
BinaryOperator.maxBy( … .reversed())
, you can use BinaryOperator.minBy(…)
. Though in this case, it looks like the OP wants maxBy
, without .reversed()
.– Holger
Nov 27 '18 at 17:43
2
2
@Holger and given that this(
values()
) would return a Collection<EmployeeContract>
and not precisely a List<EmployeeContract>
, is there a concise way to resolve that?– nullpointer
Nov 27 '18 at 17:51
@Holger and given that this(
values()
) would return a Collection<EmployeeContract>
and not precisely a List<EmployeeContract>
, is there a concise way to resolve that?– nullpointer
Nov 27 '18 at 17:51
1
1
Given there is a valid discussion and it's a bona fide quiestion, perhaps it's worth to un-hold this question?
– Nestor Milyaev
Nov 27 '18 at 17:53
Given there is a valid discussion and it's a bona fide quiestion, perhaps it's worth to un-hold this question?
– Nestor Milyaev
Nov 27 '18 at 17:53
4
4
@nullpointer if it really needs to be a
List
, you can a) wrap the entire expression in a new ArrayList<>( … )
or b) wrap the collector in a Collectors.collectingAndThen( …, m -> new ArrayList<>(m.values()))
.– Holger
Nov 27 '18 at 17:57
@nullpointer if it really needs to be a
List
, you can a) wrap the entire expression in a new ArrayList<>( … )
or b) wrap the collector in a Collectors.collectingAndThen( …, m -> new ArrayList<>(m.values()))
.– Holger
Nov 27 '18 at 17:57
|
show 2 more comments
3 Answers
3
active
oldest
votes
Well, I am just going to put my comment here in the shape of an answer:
yourList.stream()
.collect(Collectors.toMap(
EmployeeContract::getId,
Function.identity(),
BinaryOperator.maxBy(Comparator.comparing(EmployeeContract::getDate)))
)
.values();
This will give you a Collection
instead of a List
, if you really care about this.
add a comment |
You can do it in two steps as follows :
List<EmployeeContract> finalContract = contract.stream() // Stream<EmployeeContract>
.collect(Collectors.toMap(EmployeeContract::getId,
EmployeeContract::getDate, (a, b) -> a.after(b) ? a : b)) // Map<Long, Date> (Step 1)
.entrySet().stream() // Stream<Entry<Long, Date>>
.map(a -> new EmployeeContract(a.getKey(), a.getValue())) // Stream<EmployeeContract>
.collect(Collectors.toList()); // Step 2
First step: ensures the comparison of date
s with the most recent one mapped to an id
.
Second step: maps these key, value pairs to a final List<EmployeeContract>
as a result.
why(a, b) -> a.after(b) ? a : b)
whenDate
already implementComparable
?
– Eugene
Nov 27 '18 at 19:34
no specific reason @Eugene, I was just looking into theDate
API for comparison and found usingafter
a little better in terms of readability.
– nullpointer
Nov 28 '18 at 1:37
1
Good answer, and with step-by step explanation, much appreciated!
– Nestor Milyaev
Nov 28 '18 at 17:36
add a comment |
With vavr.io you can do it like this:
var finalContract = Stream.ofAll(contract) //create io.vavr.collection.Stream
.groupBy(EmployeeContract::getId)
.map(tuple -> tuple._2.maxBy(EmployeeContract::getDate))
.collect(Collectors.toList()); //result is list from java.util package
Not sure to understand the difference with a classic Stream from java.util.stream ?
– azro
Nov 27 '18 at 22:35
in io.vavr collections objects are immutable and they have map(), filter() etc methods
– jker
Nov 27 '18 at 22:39
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53504817%2fjava-8-streams-reduce-remove-duplicates-keeping-the-most-recent-entry%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Well, I am just going to put my comment here in the shape of an answer:
yourList.stream()
.collect(Collectors.toMap(
EmployeeContract::getId,
Function.identity(),
BinaryOperator.maxBy(Comparator.comparing(EmployeeContract::getDate)))
)
.values();
This will give you a Collection
instead of a List
, if you really care about this.
add a comment |
Well, I am just going to put my comment here in the shape of an answer:
yourList.stream()
.collect(Collectors.toMap(
EmployeeContract::getId,
Function.identity(),
BinaryOperator.maxBy(Comparator.comparing(EmployeeContract::getDate)))
)
.values();
This will give you a Collection
instead of a List
, if you really care about this.
add a comment |
Well, I am just going to put my comment here in the shape of an answer:
yourList.stream()
.collect(Collectors.toMap(
EmployeeContract::getId,
Function.identity(),
BinaryOperator.maxBy(Comparator.comparing(EmployeeContract::getDate)))
)
.values();
This will give you a Collection
instead of a List
, if you really care about this.
Well, I am just going to put my comment here in the shape of an answer:
yourList.stream()
.collect(Collectors.toMap(
EmployeeContract::getId,
Function.identity(),
BinaryOperator.maxBy(Comparator.comparing(EmployeeContract::getDate)))
)
.values();
This will give you a Collection
instead of a List
, if you really care about this.
answered Nov 27 '18 at 19:33
EugeneEugene
70.8k9102169
70.8k9102169
add a comment |
add a comment |
You can do it in two steps as follows :
List<EmployeeContract> finalContract = contract.stream() // Stream<EmployeeContract>
.collect(Collectors.toMap(EmployeeContract::getId,
EmployeeContract::getDate, (a, b) -> a.after(b) ? a : b)) // Map<Long, Date> (Step 1)
.entrySet().stream() // Stream<Entry<Long, Date>>
.map(a -> new EmployeeContract(a.getKey(), a.getValue())) // Stream<EmployeeContract>
.collect(Collectors.toList()); // Step 2
First step: ensures the comparison of date
s with the most recent one mapped to an id
.
Second step: maps these key, value pairs to a final List<EmployeeContract>
as a result.
why(a, b) -> a.after(b) ? a : b)
whenDate
already implementComparable
?
– Eugene
Nov 27 '18 at 19:34
no specific reason @Eugene, I was just looking into theDate
API for comparison and found usingafter
a little better in terms of readability.
– nullpointer
Nov 28 '18 at 1:37
1
Good answer, and with step-by step explanation, much appreciated!
– Nestor Milyaev
Nov 28 '18 at 17:36
add a comment |
You can do it in two steps as follows :
List<EmployeeContract> finalContract = contract.stream() // Stream<EmployeeContract>
.collect(Collectors.toMap(EmployeeContract::getId,
EmployeeContract::getDate, (a, b) -> a.after(b) ? a : b)) // Map<Long, Date> (Step 1)
.entrySet().stream() // Stream<Entry<Long, Date>>
.map(a -> new EmployeeContract(a.getKey(), a.getValue())) // Stream<EmployeeContract>
.collect(Collectors.toList()); // Step 2
First step: ensures the comparison of date
s with the most recent one mapped to an id
.
Second step: maps these key, value pairs to a final List<EmployeeContract>
as a result.
why(a, b) -> a.after(b) ? a : b)
whenDate
already implementComparable
?
– Eugene
Nov 27 '18 at 19:34
no specific reason @Eugene, I was just looking into theDate
API for comparison and found usingafter
a little better in terms of readability.
– nullpointer
Nov 28 '18 at 1:37
1
Good answer, and with step-by step explanation, much appreciated!
– Nestor Milyaev
Nov 28 '18 at 17:36
add a comment |
You can do it in two steps as follows :
List<EmployeeContract> finalContract = contract.stream() // Stream<EmployeeContract>
.collect(Collectors.toMap(EmployeeContract::getId,
EmployeeContract::getDate, (a, b) -> a.after(b) ? a : b)) // Map<Long, Date> (Step 1)
.entrySet().stream() // Stream<Entry<Long, Date>>
.map(a -> new EmployeeContract(a.getKey(), a.getValue())) // Stream<EmployeeContract>
.collect(Collectors.toList()); // Step 2
First step: ensures the comparison of date
s with the most recent one mapped to an id
.
Second step: maps these key, value pairs to a final List<EmployeeContract>
as a result.
You can do it in two steps as follows :
List<EmployeeContract> finalContract = contract.stream() // Stream<EmployeeContract>
.collect(Collectors.toMap(EmployeeContract::getId,
EmployeeContract::getDate, (a, b) -> a.after(b) ? a : b)) // Map<Long, Date> (Step 1)
.entrySet().stream() // Stream<Entry<Long, Date>>
.map(a -> new EmployeeContract(a.getKey(), a.getValue())) // Stream<EmployeeContract>
.collect(Collectors.toList()); // Step 2
First step: ensures the comparison of date
s with the most recent one mapped to an id
.
Second step: maps these key, value pairs to a final List<EmployeeContract>
as a result.
edited Nov 28 '18 at 8:40
answered Nov 27 '18 at 18:50
nullpointernullpointer
43.8k10102201
43.8k10102201
why(a, b) -> a.after(b) ? a : b)
whenDate
already implementComparable
?
– Eugene
Nov 27 '18 at 19:34
no specific reason @Eugene, I was just looking into theDate
API for comparison and found usingafter
a little better in terms of readability.
– nullpointer
Nov 28 '18 at 1:37
1
Good answer, and with step-by step explanation, much appreciated!
– Nestor Milyaev
Nov 28 '18 at 17:36
add a comment |
why(a, b) -> a.after(b) ? a : b)
whenDate
already implementComparable
?
– Eugene
Nov 27 '18 at 19:34
no specific reason @Eugene, I was just looking into theDate
API for comparison and found usingafter
a little better in terms of readability.
– nullpointer
Nov 28 '18 at 1:37
1
Good answer, and with step-by step explanation, much appreciated!
– Nestor Milyaev
Nov 28 '18 at 17:36
why
(a, b) -> a.after(b) ? a : b)
when Date
already implement Comparable
?– Eugene
Nov 27 '18 at 19:34
why
(a, b) -> a.after(b) ? a : b)
when Date
already implement Comparable
?– Eugene
Nov 27 '18 at 19:34
no specific reason @Eugene, I was just looking into the
Date
API for comparison and found using after
a little better in terms of readability.– nullpointer
Nov 28 '18 at 1:37
no specific reason @Eugene, I was just looking into the
Date
API for comparison and found using after
a little better in terms of readability.– nullpointer
Nov 28 '18 at 1:37
1
1
Good answer, and with step-by step explanation, much appreciated!
– Nestor Milyaev
Nov 28 '18 at 17:36
Good answer, and with step-by step explanation, much appreciated!
– Nestor Milyaev
Nov 28 '18 at 17:36
add a comment |
With vavr.io you can do it like this:
var finalContract = Stream.ofAll(contract) //create io.vavr.collection.Stream
.groupBy(EmployeeContract::getId)
.map(tuple -> tuple._2.maxBy(EmployeeContract::getDate))
.collect(Collectors.toList()); //result is list from java.util package
Not sure to understand the difference with a classic Stream from java.util.stream ?
– azro
Nov 27 '18 at 22:35
in io.vavr collections objects are immutable and they have map(), filter() etc methods
– jker
Nov 27 '18 at 22:39
add a comment |
With vavr.io you can do it like this:
var finalContract = Stream.ofAll(contract) //create io.vavr.collection.Stream
.groupBy(EmployeeContract::getId)
.map(tuple -> tuple._2.maxBy(EmployeeContract::getDate))
.collect(Collectors.toList()); //result is list from java.util package
Not sure to understand the difference with a classic Stream from java.util.stream ?
– azro
Nov 27 '18 at 22:35
in io.vavr collections objects are immutable and they have map(), filter() etc methods
– jker
Nov 27 '18 at 22:39
add a comment |
With vavr.io you can do it like this:
var finalContract = Stream.ofAll(contract) //create io.vavr.collection.Stream
.groupBy(EmployeeContract::getId)
.map(tuple -> tuple._2.maxBy(EmployeeContract::getDate))
.collect(Collectors.toList()); //result is list from java.util package
With vavr.io you can do it like this:
var finalContract = Stream.ofAll(contract) //create io.vavr.collection.Stream
.groupBy(EmployeeContract::getId)
.map(tuple -> tuple._2.maxBy(EmployeeContract::getDate))
.collect(Collectors.toList()); //result is list from java.util package
answered Nov 27 '18 at 19:56
jkerjker
15010
15010
Not sure to understand the difference with a classic Stream from java.util.stream ?
– azro
Nov 27 '18 at 22:35
in io.vavr collections objects are immutable and they have map(), filter() etc methods
– jker
Nov 27 '18 at 22:39
add a comment |
Not sure to understand the difference with a classic Stream from java.util.stream ?
– azro
Nov 27 '18 at 22:35
in io.vavr collections objects are immutable and they have map(), filter() etc methods
– jker
Nov 27 '18 at 22:39
Not sure to understand the difference with a classic Stream from java.util.stream ?
– azro
Nov 27 '18 at 22:35
Not sure to understand the difference with a classic Stream from java.util.stream ?
– azro
Nov 27 '18 at 22:35
in io.vavr collections objects are immutable and they have map(), filter() etc methods
– jker
Nov 27 '18 at 22:39
in io.vavr collections objects are immutable and they have map(), filter() etc methods
– jker
Nov 27 '18 at 22:39
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53504817%2fjava-8-streams-reduce-remove-duplicates-keeping-the-most-recent-entry%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
I wanted to post an answer but this one was closed too fast...
yourList.stream() .collect(Collectors.toMap( EmployeeContract::getId, Function.identity(), BinaryOperator.maxBy(Comparator.comparing(EmployeeContract::getDate).reversed())) ) .values();
– Eugene
Nov 27 '18 at 17:22
3
@Eugene instead of
BinaryOperator.maxBy( … .reversed())
, you can useBinaryOperator.minBy(…)
. Though in this case, it looks like the OP wantsmaxBy
, without.reversed()
.– Holger
Nov 27 '18 at 17:43
2
@Holger and given that this(
values()
) would return aCollection<EmployeeContract>
and not precisely aList<EmployeeContract>
, is there a concise way to resolve that?– nullpointer
Nov 27 '18 at 17:51
1
Given there is a valid discussion and it's a bona fide quiestion, perhaps it's worth to un-hold this question?
– Nestor Milyaev
Nov 27 '18 at 17:53
4
@nullpointer if it really needs to be a
List
, you can a) wrap the entire expression in anew ArrayList<>( … )
or b) wrap the collector in aCollectors.collectingAndThen( …, m -> new ArrayList<>(m.values()))
.– Holger
Nov 27 '18 at 17:57