What is the type of Map.Entry.comparingByValue().reversed()? [duplicate]
This question already has an answer here:
Generic type inference not working with method chaining?
1 answer
I have a list of map entries
Map<String, Integer> map = new HashMap<>();
...(fill the map)...
List<Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
and I want to sort it by values according to this answer, but in reverse order. When I do
Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue();
entries.sort(cmp.reversed());
everything works fine. But when I try to shorten the above two lines to
entries.sort(Entry.comparingByValue().reversed());
the compiler returns
error: incompatible types: Comparator<Entry<Object,V>> cannot be converted to Comparator<? super Entry<String,Integer>>
entries.sort(Entry.comparingByValue().reversed());
where V is a type-variable:
V extends Comparable<? super V>
This seems strange because the implementation of Comparator's reversed()
default method simply transfers it to Collections.reverseOrder()
and I can change the above line to
entries.sort(Collections.reverseOrder(Entry.comparingByValue()));
to work. But what is the correct type of Entry.comparingByValue().reversed()
?
Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue().reversed();
doesn't seem to work. Omitting the type parameter Entry<String, Integer>
works, but this results in "unchecked method invocation" warning from the compiler later.
java generics comparator
marked as duplicate by Michael
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 15 '18 at 18:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Generic type inference not working with method chaining?
1 answer
I have a list of map entries
Map<String, Integer> map = new HashMap<>();
...(fill the map)...
List<Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
and I want to sort it by values according to this answer, but in reverse order. When I do
Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue();
entries.sort(cmp.reversed());
everything works fine. But when I try to shorten the above two lines to
entries.sort(Entry.comparingByValue().reversed());
the compiler returns
error: incompatible types: Comparator<Entry<Object,V>> cannot be converted to Comparator<? super Entry<String,Integer>>
entries.sort(Entry.comparingByValue().reversed());
where V is a type-variable:
V extends Comparable<? super V>
This seems strange because the implementation of Comparator's reversed()
default method simply transfers it to Collections.reverseOrder()
and I can change the above line to
entries.sort(Collections.reverseOrder(Entry.comparingByValue()));
to work. But what is the correct type of Entry.comparingByValue().reversed()
?
Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue().reversed();
doesn't seem to work. Omitting the type parameter Entry<String, Integer>
works, but this results in "unchecked method invocation" warning from the compiler later.
java generics comparator
marked as duplicate by Michael
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 15 '18 at 18:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
Check answers to this question: stackoverflow.com/questions/24794924/…
– ernest_k
Nov 15 '18 at 18:34
@ernest_k Well it seems like you are. I've marked my answer as community wiki just in case anyone thinks I closed this for any "competitive advantage".
– Michael
Nov 15 '18 at 18:41
Thanks @Michael
– ernest_k
Nov 15 '18 at 18:42
add a comment |
This question already has an answer here:
Generic type inference not working with method chaining?
1 answer
I have a list of map entries
Map<String, Integer> map = new HashMap<>();
...(fill the map)...
List<Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
and I want to sort it by values according to this answer, but in reverse order. When I do
Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue();
entries.sort(cmp.reversed());
everything works fine. But when I try to shorten the above two lines to
entries.sort(Entry.comparingByValue().reversed());
the compiler returns
error: incompatible types: Comparator<Entry<Object,V>> cannot be converted to Comparator<? super Entry<String,Integer>>
entries.sort(Entry.comparingByValue().reversed());
where V is a type-variable:
V extends Comparable<? super V>
This seems strange because the implementation of Comparator's reversed()
default method simply transfers it to Collections.reverseOrder()
and I can change the above line to
entries.sort(Collections.reverseOrder(Entry.comparingByValue()));
to work. But what is the correct type of Entry.comparingByValue().reversed()
?
Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue().reversed();
doesn't seem to work. Omitting the type parameter Entry<String, Integer>
works, but this results in "unchecked method invocation" warning from the compiler later.
java generics comparator
This question already has an answer here:
Generic type inference not working with method chaining?
1 answer
I have a list of map entries
Map<String, Integer> map = new HashMap<>();
...(fill the map)...
List<Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
and I want to sort it by values according to this answer, but in reverse order. When I do
Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue();
entries.sort(cmp.reversed());
everything works fine. But when I try to shorten the above two lines to
entries.sort(Entry.comparingByValue().reversed());
the compiler returns
error: incompatible types: Comparator<Entry<Object,V>> cannot be converted to Comparator<? super Entry<String,Integer>>
entries.sort(Entry.comparingByValue().reversed());
where V is a type-variable:
V extends Comparable<? super V>
This seems strange because the implementation of Comparator's reversed()
default method simply transfers it to Collections.reverseOrder()
and I can change the above line to
entries.sort(Collections.reverseOrder(Entry.comparingByValue()));
to work. But what is the correct type of Entry.comparingByValue().reversed()
?
Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue().reversed();
doesn't seem to work. Omitting the type parameter Entry<String, Integer>
works, but this results in "unchecked method invocation" warning from the compiler later.
This question already has an answer here:
Generic type inference not working with method chaining?
1 answer
java generics comparator
java generics comparator
asked Nov 15 '18 at 18:20
John McClaneJohn McClane
1,5602421
1,5602421
marked as duplicate by Michael
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 15 '18 at 18:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Michael
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 15 '18 at 18:39
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
Check answers to this question: stackoverflow.com/questions/24794924/…
– ernest_k
Nov 15 '18 at 18:34
@ernest_k Well it seems like you are. I've marked my answer as community wiki just in case anyone thinks I closed this for any "competitive advantage".
– Michael
Nov 15 '18 at 18:41
Thanks @Michael
– ernest_k
Nov 15 '18 at 18:42
add a comment |
2
Check answers to this question: stackoverflow.com/questions/24794924/…
– ernest_k
Nov 15 '18 at 18:34
@ernest_k Well it seems like you are. I've marked my answer as community wiki just in case anyone thinks I closed this for any "competitive advantage".
– Michael
Nov 15 '18 at 18:41
Thanks @Michael
– ernest_k
Nov 15 '18 at 18:42
2
2
Check answers to this question: stackoverflow.com/questions/24794924/…
– ernest_k
Nov 15 '18 at 18:34
Check answers to this question: stackoverflow.com/questions/24794924/…
– ernest_k
Nov 15 '18 at 18:34
@ernest_k Well it seems like you are. I've marked my answer as community wiki just in case anyone thinks I closed this for any "competitive advantage".
– Michael
Nov 15 '18 at 18:41
@ernest_k Well it seems like you are. I've marked my answer as community wiki just in case anyone thinks I closed this for any "competitive advantage".
– Michael
Nov 15 '18 at 18:41
Thanks @Michael
– ernest_k
Nov 15 '18 at 18:42
Thanks @Michael
– ernest_k
Nov 15 '18 at 18:42
add a comment |
2 Answers
2
active
oldest
votes
The compiler is not clever enough to infer the generic type parameters in this case. You can specify them explicitly:
entries.sort(Entry.<String, Integer>comparingByValue().reversed());
The issue is that you first need the type of the comparator returned by comparingByValue
to know the type of the one returned by reversed
.
I don't see any good reason why a clever compiler couldn't work backwards and know that sort
requires a particular type, so reversed
must be a particular type, so comparingByValue
must be a particular type. But that's not how things are.
Type inference is still being worked on (as recently as Java 10), so maybe in the future, who knows.
add a comment |
When you shorten down
Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue();
entries.sort(cmp.reversed());
to
entries.sort(Entry.comparingByValue().reversed());
you remove the type information gleaned from cmp
’s declaration. The compiler still needs to know that information, so you need to add the generic typing to Entry
:
entries.sort(Entry.<String, Integer>comparingByValue().reversed());
Oops, looks like I put the.
in the wrong place. Fixed now.
– MTCoster
Nov 15 '18 at 18:41
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The compiler is not clever enough to infer the generic type parameters in this case. You can specify them explicitly:
entries.sort(Entry.<String, Integer>comparingByValue().reversed());
The issue is that you first need the type of the comparator returned by comparingByValue
to know the type of the one returned by reversed
.
I don't see any good reason why a clever compiler couldn't work backwards and know that sort
requires a particular type, so reversed
must be a particular type, so comparingByValue
must be a particular type. But that's not how things are.
Type inference is still being worked on (as recently as Java 10), so maybe in the future, who knows.
add a comment |
The compiler is not clever enough to infer the generic type parameters in this case. You can specify them explicitly:
entries.sort(Entry.<String, Integer>comparingByValue().reversed());
The issue is that you first need the type of the comparator returned by comparingByValue
to know the type of the one returned by reversed
.
I don't see any good reason why a clever compiler couldn't work backwards and know that sort
requires a particular type, so reversed
must be a particular type, so comparingByValue
must be a particular type. But that's not how things are.
Type inference is still being worked on (as recently as Java 10), so maybe in the future, who knows.
add a comment |
The compiler is not clever enough to infer the generic type parameters in this case. You can specify them explicitly:
entries.sort(Entry.<String, Integer>comparingByValue().reversed());
The issue is that you first need the type of the comparator returned by comparingByValue
to know the type of the one returned by reversed
.
I don't see any good reason why a clever compiler couldn't work backwards and know that sort
requires a particular type, so reversed
must be a particular type, so comparingByValue
must be a particular type. But that's not how things are.
Type inference is still being worked on (as recently as Java 10), so maybe in the future, who knows.
The compiler is not clever enough to infer the generic type parameters in this case. You can specify them explicitly:
entries.sort(Entry.<String, Integer>comparingByValue().reversed());
The issue is that you first need the type of the comparator returned by comparingByValue
to know the type of the one returned by reversed
.
I don't see any good reason why a clever compiler couldn't work backwards and know that sort
requires a particular type, so reversed
must be a particular type, so comparingByValue
must be a particular type. But that's not how things are.
Type inference is still being worked on (as recently as Java 10), so maybe in the future, who knows.
edited Nov 15 '18 at 18:33
community wiki
2 revs
Michael
add a comment |
add a comment |
When you shorten down
Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue();
entries.sort(cmp.reversed());
to
entries.sort(Entry.comparingByValue().reversed());
you remove the type information gleaned from cmp
’s declaration. The compiler still needs to know that information, so you need to add the generic typing to Entry
:
entries.sort(Entry.<String, Integer>comparingByValue().reversed());
Oops, looks like I put the.
in the wrong place. Fixed now.
– MTCoster
Nov 15 '18 at 18:41
add a comment |
When you shorten down
Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue();
entries.sort(cmp.reversed());
to
entries.sort(Entry.comparingByValue().reversed());
you remove the type information gleaned from cmp
’s declaration. The compiler still needs to know that information, so you need to add the generic typing to Entry
:
entries.sort(Entry.<String, Integer>comparingByValue().reversed());
Oops, looks like I put the.
in the wrong place. Fixed now.
– MTCoster
Nov 15 '18 at 18:41
add a comment |
When you shorten down
Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue();
entries.sort(cmp.reversed());
to
entries.sort(Entry.comparingByValue().reversed());
you remove the type information gleaned from cmp
’s declaration. The compiler still needs to know that information, so you need to add the generic typing to Entry
:
entries.sort(Entry.<String, Integer>comparingByValue().reversed());
When you shorten down
Comparator<Entry<String, Integer>> cmp = Entry.comparingByValue();
entries.sort(cmp.reversed());
to
entries.sort(Entry.comparingByValue().reversed());
you remove the type information gleaned from cmp
’s declaration. The compiler still needs to know that information, so you need to add the generic typing to Entry
:
entries.sort(Entry.<String, Integer>comparingByValue().reversed());
edited Nov 15 '18 at 18:41
answered Nov 15 '18 at 18:24
MTCosterMTCoster
3,85922141
3,85922141
Oops, looks like I put the.
in the wrong place. Fixed now.
– MTCoster
Nov 15 '18 at 18:41
add a comment |
Oops, looks like I put the.
in the wrong place. Fixed now.
– MTCoster
Nov 15 '18 at 18:41
Oops, looks like I put the
.
in the wrong place. Fixed now.– MTCoster
Nov 15 '18 at 18:41
Oops, looks like I put the
.
in the wrong place. Fixed now.– MTCoster
Nov 15 '18 at 18:41
add a comment |
2
Check answers to this question: stackoverflow.com/questions/24794924/…
– ernest_k
Nov 15 '18 at 18:34
@ernest_k Well it seems like you are. I've marked my answer as community wiki just in case anyone thinks I closed this for any "competitive advantage".
– Michael
Nov 15 '18 at 18:41
Thanks @Michael
– ernest_k
Nov 15 '18 at 18:42