JDK 8新特性-MAP集合または配列valueソート実装

3429 ワード

Map collect = noWhiteListMap.entrySet().stream()
        .sorted(Comparator.comparing(entry -> entry.getValue().split(",").length,Comparator.reverseOrder()))
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
                (oldValue, newValue) -> oldValue, LinkedHashMap::new));

Java 8ではこの手順に従ってmapをソートする.
  • MapをStream
  • に変換
  • これを並べ替える
  • .
  • Collectt and return a new LinkedHashMap(保持順序)
  • Comparatorインタフェースには2つのcomparingメソッドがあります
    /**
         * Accepts a function that extracts a sort key from a type {@code T}, and
         * returns a {@code Comparator} that compares by that sort key using
         * the specified {@link Comparator}.
          *
         * 

    The returned comparator is serializable if the specified function * and comparator are both serializable. * * @apiNote * For example, to obtain a {@code Comparator} that compares {@code * Person} objects by their last name ignoring case differences, * *

    {@code
         *     Comparator cmp = Comparator.comparing(
         *             Person::getLastName,
         *             String.CASE_INSENSITIVE_ORDER);
         * }
    *
    * @param the type of element to be compared
    * @param the type of the sort key
    * @param keyExtractor the function used to extract the sort key
    * @param keyComparator the {@code Comparator} used to compare the sort key
    * @return a comparator that compares by an extracted key using the
    * specified {@code Comparator}
    * @throws NullPointerException if either argument is null
    * @since 1.8
    */
    public static Comparator comparing(
    Function super T, ? extends U> keyExtractor,
    Comparator super U> keyComparator)
    {
    Objects.requireNonNull(keyExtractor);
    Objects.requireNonNull(keyComparator);
    return (Comparator & Serializable)
    (c1, c2) -> keyComparator.compare(keyExtractor.apply(c1),
    keyExtractor.apply(c2));
    }
    /**
    * Accepts a function that extracts a {@link java.lang.Comparable
    * Comparable} sort key from a type {@code T}, and returns a {@code
    * Comparator} that compares by that sort key.
    *
    * The returned comparator is serializable if the specified function
    * is also serializable.
    *
    * @apiNote
    * For example, to obtain a {@code Comparator} that compares {@code
    * Person} objects by their last name,
    *
    *
    {@code
         *     Comparator byLastName = Comparator.comparing(Person::getLastName);
         * }
    *
    * @param the type of element to be compared
    * @param the type of the {@code Comparable} sort key
    * @param keyExtractor the function used to extract the {@link
    * Comparable} sort key
    * @return a comparator that compares by an extracted key
    * @throws NullPointerException if the argument is null
    * @since 1.8
    */
    public static > Comparator comparing(
    Function super T, ? extends U> keyExtractor)
    {
    Objects.requireNonNull(keyExtractor);
    return (Comparator & Serializable)
    (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));
    }
       comparing                               LinkedHashMap