-
Enhancement
-
Resolution: Won't Fix
-
P4
-
None
-
8, 9
A DESCRIPTION OF THE REQUEST :
Java Version:
java version "1.8.0_112"
Java(TM) SE Runtime Environment (build 1.8.0_112-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.112-b16, mixed mode)
Suggest we have
Map<String, Integer> map
And we want to have a list of it's elements sorted by Integer ascending
map.entrySet().stream().sorted((o1, o2) -> o1.getValue() - o2.getValue())
Which can be replaced with
map.entrySet().stream().sorted(Comparator.comparingInt(Map.Entry::getValue))
Now we want to sort it by Integer desc
map.entrySet().stream().sorted((o1, o2) -> o2.getValue() - o1.getValue())
Which we assume is obvious to replace with
map.entrySet().stream().sorted(Comparator.comparingInt(Map.Entry::getValue).reversed())
That gives us compilation error: Not-static method cannot be referenced from a static context.
The reason is that Comparator.reversed() method calls static Collections.reverseOrder inside.
Reproducibility:
Can be reproduced anytime
JUSTIFICATION :
This realization of Comparator.reverse() method breaks the opportunity to use Method Reference inside. There is no natural conditions which restricts doing this.
It's also illogically that we can write:
sorted(Comparator.comparingInt(Map.Entry::getValue))
but cannot:
.sorted(Comparator.comparingInt(Map.Entry::getValue).reversed())
That makes writing functional code in Java more complicated.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
sorted(Comparator.comparingInt(Map.Entry::getValue).reversed())
is reverse sorting for
sorted(Comparator.comparingInt(Map.Entry::getValue))
ACTUAL -
sorted(Comparator.comparingInt(Map.Entry::getValue).reversed()) do not compile
---------- BEGIN SOURCE ----------
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
class Example {
void bug() {
Map<String, Integer> map = new HashMap();
map.entrySet().stream().sorted(Comparator.comparingInt(Map.Entry::getValue).reversed());
}
}
---------- END SOURCE ----------
Java Version:
java version "1.8.0_112"
Java(TM) SE Runtime Environment (build 1.8.0_112-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.112-b16, mixed mode)
Suggest we have
Map<String, Integer> map
And we want to have a list of it's elements sorted by Integer ascending
map.entrySet().stream().sorted((o1, o2) -> o1.getValue() - o2.getValue())
Which can be replaced with
map.entrySet().stream().sorted(Comparator.comparingInt(Map.Entry::getValue))
Now we want to sort it by Integer desc
map.entrySet().stream().sorted((o1, o2) -> o2.getValue() - o1.getValue())
Which we assume is obvious to replace with
map.entrySet().stream().sorted(Comparator.comparingInt(Map.Entry::getValue).reversed())
That gives us compilation error: Not-static method cannot be referenced from a static context.
The reason is that Comparator.reversed() method calls static Collections.reverseOrder inside.
Reproducibility:
Can be reproduced anytime
JUSTIFICATION :
This realization of Comparator.reverse() method breaks the opportunity to use Method Reference inside. There is no natural conditions which restricts doing this.
It's also illogically that we can write:
sorted(Comparator.comparingInt(Map.Entry::getValue))
but cannot:
.sorted(Comparator.comparingInt(Map.Entry::getValue).reversed())
That makes writing functional code in Java more complicated.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
sorted(Comparator.comparingInt(Map.Entry::getValue).reversed())
is reverse sorting for
sorted(Comparator.comparingInt(Map.Entry::getValue))
ACTUAL -
sorted(Comparator.comparingInt(Map.Entry::getValue).reversed()) do not compile
---------- BEGIN SOURCE ----------
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
class Example {
void bug() {
Map<String, Integer> map = new HashMap();
map.entrySet().stream().sorted(Comparator.comparingInt(Map.Entry::getValue).reversed());
}
}
---------- END SOURCE ----------