Class MapUtils


  • public class MapUtils
    extends Object
    Utility methods for Map conversions.
    • Method Detail

      • newHashMap

        @NotNull
        public static <K,​V> @NotNull Map<K,​V> newHashMap​(int capacity)
        Creates a new, empty HashMap with expected capacity.

        The returned map uses the default load factor of 0.75, and its capacity is large enough to add expected number of elements without resizing.

        Parameters:
        capacity - the expected number of elements
        Throws:
        IllegalArgumentException - if capacity is negative
        See Also:
        SetUtils.newHashSet(int), SetUtils.newLinkedHashSet(int)
      • fromProperties

        @NotNull
        public static @NotNull Map<String,​String> fromProperties​(@NotNull
                                                                       @NotNull Properties properties)
        Converts a Properties object to an unmodifiable Map with string keys and values.
        Parameters:
        properties - the Properties object to convert, must not be null
        Returns:
        an unmodifiable Map containing the same entries as the given Properties object
        Throws:
        NullPointerException - if the properties parameter is null
      • filterKeys

        @NotNull
        public static <K,​V> @NotNull Map<K,​V> filterKeys​(@NotNull
                                                                     @NotNull Map<K,​V> map,
                                                                     @NotNull
                                                                     @NotNull Predicate<? super K> predicate)
        Create a new Map after filtering the entries of the given map based on the specified predicate applied to the keys.
        Type Parameters:
        K - the type of keys in the map
        V - the type of values in the map
        Parameters:
        map - the map to filter, must not be null
        predicate - the predicate to apply to the keys, must not be null
        Returns:
        a new map containing only the entries whose keys match the predicate
        Throws:
        NullPointerException - if the map or predicate is null
        See Also:
        filterValues(Map, Predicate), filterEntries(Map, Predicate)
      • filterValues

        @NotNull
        public static <K,​V> @NotNull Map<K,​V> filterValues​(@NotNull
                                                                       @NotNull Map<K,​V> map,
                                                                       @NotNull
                                                                       @NotNull Predicate<? super V> predicate)
        Create a new Map after filtering the entries of the given map based on the specified predicate applied to the values.
        Type Parameters:
        K - the type of keys in the map
        V - the type of values in the map
        Parameters:
        map - the map to filter, must not be null
        predicate - the predicate to apply to the values, must not be null
        Returns:
        a new map containing only the entries whose values match the predicate
        Throws:
        NullPointerException - if the map or predicate is null
        See Also:
        filterKeys(Map, Predicate), filterEntries(Map, Predicate)
      • filterEntries

        @NotNull
        public static <K,​V> @NotNull Map<K,​V> filterEntries​(@NotNull
                                                                        @NotNull Map<K,​V> map,
                                                                        @NotNull
                                                                        @NotNull Predicate<? super Map.Entry<K,​V>> predicate)
        Create a new Map after filtering the entries of the given map based on the specified predicate applied to the Map.Entry.
        Type Parameters:
        K - the type of keys in the map
        V - the type of values in the map
        Parameters:
        map - the map to filter, must not be null
        predicate - the predicate to apply to the Map.Entry, must not be null
        Returns:
        a new map containing only the entries whose values match the predicate
        Throws:
        NullPointerException - if the map or predicate is null
        See Also:
        filterKeys(Map, Predicate), filterValues(Map, Predicate)