jdk/src/share/classes/java/util/Map.java
changeset 20868 dcad3ccb889a
parent 20491 eb2dfc7436af
child 21339 20e8b81964d5
equal deleted inserted replaced
20867:38b088040f4f 20868:dcad3ccb889a
   463          *
   463          *
   464          * @param  <K> the {@link Comparable} type of then map keys
   464          * @param  <K> the {@link Comparable} type of then map keys
   465          * @param  <V> the type of the map values
   465          * @param  <V> the type of the map values
   466          * @return a comparator that compares {@link Map.Entry} in natural order on key.
   466          * @return a comparator that compares {@link Map.Entry} in natural order on key.
   467          * @see Comparable
   467          * @see Comparable
       
   468          * @since 1.8
   468          */
   469          */
   469         public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey() {
   470         public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey() {
   470             return (Comparator<Map.Entry<K, V>> & Serializable)
   471             return (Comparator<Map.Entry<K, V>> & Serializable)
   471                 (c1, c2) -> c1.getKey().compareTo(c2.getKey());
   472                 (c1, c2) -> c1.getKey().compareTo(c2.getKey());
   472         }
   473         }
   479          *
   480          *
   480          * @param <K> the type of the map keys
   481          * @param <K> the type of the map keys
   481          * @param <V> the {@link Comparable} type of the map values
   482          * @param <V> the {@link Comparable} type of the map values
   482          * @return a comparator that compares {@link Map.Entry} in natural order on value.
   483          * @return a comparator that compares {@link Map.Entry} in natural order on value.
   483          * @see Comparable
   484          * @see Comparable
       
   485          * @since 1.8
   484          */
   486          */
   485         public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue() {
   487         public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue() {
   486             return (Comparator<Map.Entry<K, V>> & Serializable)
   488             return (Comparator<Map.Entry<K, V>> & Serializable)
   487                 (c1, c2) -> c1.getValue().compareTo(c2.getValue());
   489                 (c1, c2) -> c1.getValue().compareTo(c2.getValue());
   488         }
   490         }
   496          *
   498          *
   497          * @param  <K> the type of the map keys
   499          * @param  <K> the type of the map keys
   498          * @param  <V> the type of the map values
   500          * @param  <V> the type of the map values
   499          * @param  cmp the key {@link Comparator}
   501          * @param  cmp the key {@link Comparator}
   500          * @return a comparator that compares {@link Map.Entry} by the key.
   502          * @return a comparator that compares {@link Map.Entry} by the key.
       
   503          * @since 1.8
   501          */
   504          */
   502         public static <K, V> Comparator<Map.Entry<K, V>> comparingByKey(Comparator<? super K> cmp) {
   505         public static <K, V> Comparator<Map.Entry<K, V>> comparingByKey(Comparator<? super K> cmp) {
   503             Objects.requireNonNull(cmp);
   506             Objects.requireNonNull(cmp);
   504             return (Comparator<Map.Entry<K, V>> & Serializable)
   507             return (Comparator<Map.Entry<K, V>> & Serializable)
   505                 (c1, c2) -> cmp.compare(c1.getKey(), c2.getKey());
   508                 (c1, c2) -> cmp.compare(c1.getKey(), c2.getKey());
   514          *
   517          *
   515          * @param  <K> the type of the map keys
   518          * @param  <K> the type of the map keys
   516          * @param  <V> the type of the map values
   519          * @param  <V> the type of the map values
   517          * @param  cmp the value {@link Comparator}
   520          * @param  cmp the value {@link Comparator}
   518          * @return a comparator that compares {@link Map.Entry} by the value.
   521          * @return a comparator that compares {@link Map.Entry} by the value.
       
   522          * @since 1.8
   519          */
   523          */
   520         public static <K, V> Comparator<Map.Entry<K, V>> comparingByValue(Comparator<? super V> cmp) {
   524         public static <K, V> Comparator<Map.Entry<K, V>> comparingByValue(Comparator<? super V> cmp) {
   521             Objects.requireNonNull(cmp);
   525             Objects.requireNonNull(cmp);
   522             return (Comparator<Map.Entry<K, V>> & Serializable)
   526             return (Comparator<Map.Entry<K, V>> & Serializable)
   523                 (c1, c2) -> cmp.compare(c1.getValue(), c2.getValue());
   527                 (c1, c2) -> cmp.compare(c1.getValue(), c2.getValue());
   556     int hashCode();
   560     int hashCode();
   557 
   561 
   558     // Defaultable methods
   562     // Defaultable methods
   559 
   563 
   560     /**
   564     /**
   561     *  Returns the value to which the specified key is mapped,
   565      * Returns the value to which the specified key is mapped,
   562     *  or {@code defaultValue} if this map contains no mapping
   566      * or {@code defaultValue} if this map contains no mapping
   563     *  for the key.
   567      * for the key.
   564     *
   568      *
   565     * <p>The default implementation makes no guarantees about synchronization
   569      * <p>The default implementation makes no guarantees about synchronization
   566     * or atomicity properties of this method. Any implementation providing
   570      * or atomicity properties of this method. Any implementation providing
   567     * atomicity guarantees must override this method and document its
   571      * atomicity guarantees must override this method and document its
   568     * concurrency properties.
   572      * concurrency properties.
   569     *
   573      *
   570     * @param key the key whose associated value is to be returned
   574      * @param key the key whose associated value is to be returned
   571     * @param defaultValue the default mapping of the key
   575      * @param defaultValue the default mapping of the key
   572     * @return the value to which the specified key is mapped, or
   576      * @return the value to which the specified key is mapped, or
   573     * {@code defaultValue} if this map contains no mapping for the key
   577      * {@code defaultValue} if this map contains no mapping for the key
   574     * @throws ClassCastException if the key is of an inappropriate type for
   578      * @throws ClassCastException if the key is of an inappropriate type for
   575     * this map
   579      * this map
   576     * (<a href="Collection.html#optional-restrictions">optional</a>)
   580      * (<a href="Collection.html#optional-restrictions">optional</a>)
   577     * @throws NullPointerException if the specified key is null and this map
   581      * @throws NullPointerException if the specified key is null and this map
   578     * does not permit null keys
   582      * does not permit null keys
   579     * (<a href="Collection.html#optional-restrictions">optional</a>)
   583      * (<a href="Collection.html#optional-restrictions">optional</a>)
   580     */
   584      * @since 1.8
       
   585      */
   581     default V getOrDefault(Object key, V defaultValue) {
   586     default V getOrDefault(Object key, V defaultValue) {
   582         V v;
   587         V v;
   583         return (((v = get(key)) != null) || containsKey(key))
   588         return (((v = get(key)) != null) || containsKey(key))
   584             ? v
   589             ? v
   585             : defaultValue;
   590             : defaultValue;