changeset 7786:34443a93970a

8011137: Comparators.byKey(Comparator) behavior contradicts the spec Summary: Revert change breaks serializability and added a intermediate Function object
author henryjen
date Mon, 01 Apr 2013 13:41:33 -0700
parents e274aca6852b
children 62d2a1511961
files src/share/classes/java/util/Comparators.java
diffstat 1 files changed, 6 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/Comparators.java	Mon Apr 01 15:25:22 2013 -0400
+++ b/src/share/classes/java/util/Comparators.java	Mon Apr 01 13:41:33 2013 -0700
@@ -125,7 +125,9 @@
      * @return A comparator compares {@link Map.Entry} by the key.
      */
     public static <K, V> Comparator<Map.Entry<K, V>> byKey(Comparator<? super K> cmp) {
-        return comparing(Map.Entry<K, V>::getKey, cmp);
+        Objects.requireNonNull(cmp);
+        return (Comparator<Map.Entry<K, V>> & Serializable)
+            (c1, c2) -> cmp.compare(c1.getKey(), c2.getKey());
     }
 
     /**
@@ -138,7 +140,9 @@
      * @return A comparator compares {@link Map.Entry} by the value.
      */
     public static <K, V> Comparator<Map.Entry<K, V>> byValue(Comparator<? super V> cmp) {
-        return comparing(Map.Entry<K, V>::getValue, cmp);
+        Objects.requireNonNull(cmp);
+        return (Comparator<Map.Entry<K, V>> & Serializable)
+            (c1, c2) -> cmp.compare(c1.getValue(), c2.getValue());
     }
 
     /**