Class Maps


  • public final class Maps
    extends Object

    Convenience methods for creating, reading, and manipulating maps.

    Author:
    David B. Bracewell
    • Method Detail

      • asHashMap

        public static <K,​V> Map<K,​V> asHashMap​(@NonNull
                                                           @NonNull Iterable<? extends K> keys,
                                                           @NonNull
                                                           @NonNull Function<? super K,​? extends V> valueMapper)
        Creates a map from an iterable of keys and a function that returns a value given the key.
        Type Parameters:
        K - the key type parameter
        V - the value type parameter
        Parameters:
        keys - the keys
        valueMapper - the function to use to generate values from keys
        Returns:
        the map
      • create

        public static <K,​V> Map<K,​V> create​(@NonNull
                                                        @NonNull Class<? extends Map> clazz)
        Creates an instance of the given map class.
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        clazz - the map class
        Returns:
        An instance of the specified map class
      • hashMapOf

        @SafeVarargs
        public static <K,​V> Map<K,​V> hashMapOf​(@NonNull
                                                           @NonNull Map.Entry<? extends K,​? extends V>... objects)
        Creates a HashMap from entries
        Type Parameters:
        K - the key type parameter
        V - the value type parameter
        Parameters:
        objects - the entries
        Returns:
        the map
      • mapOf

        @SafeVarargs
        public static <K,​V> Map<K,​V> mapOf​(@NonNull
                                                       @NonNull Supplier<? extends Map<K,​V>> supplier,
                                                       @NonNull
                                                       @NonNull Map.Entry<? extends K,​? extends V>... objects)
        Creates a map of type returned via the givne supplier from the given entries
        Type Parameters:
        K - the key type parameter
        V - the value type parameter
        Parameters:
        supplier - the new map supplier
        objects - the entries
        Returns:
        the map
      • putAll

        @SafeVarargs
        public static <K,​V> Map<K,​V> putAll​(@NonNull
                                                        @NonNull Map<K,​V> map,
                                                        @NonNull
                                                        @NonNull Map.Entry<? extends K,​? extends V>... entries)
        Puts all given entries into the given map
        Type Parameters:
        K - the key type parameter
        V - the value type parameter
        Parameters:
        map - the map to add the entries to
        entries - the entries to add
      • sortEntries

        public static <K,​V> List<Map.Entry<K,​V>> sortEntries​(@NonNull
                                                                         @NonNull Map<K,​V> map,
                                                                         @NonNull
                                                                         @NonNull Comparator<Map.Entry<K,​V>> comparator)
        Sorts the entries in the map
        Type Parameters:
        K - the key type parameter
        V - the value type parameter
        Parameters:
        map - the map to sort
        comparator - The comparator to use when comparing entries.
        Returns:
        the list of sorted map entries
      • sortEntriesByKey

        public static <K extends Comparable<? super K>,​V> List<Map.Entry<K,​V>> sortEntriesByKey​(@NonNull
                                                                                                            @NonNull Map<K,​V> map,
                                                                                                            boolean ascending)
        Sorts the entries in the map by key
        Type Parameters:
        K - the key type parameter
        V - the value type parameter
        Parameters:
        map - the map to sort
        ascending - True sort in ascending order, False in descending order
        Returns:
        the list of sorted map entries
      • sortEntriesByValue

        public static <K,​V extends Comparable<? super V>> List<Map.Entry<K,​V>> sortEntriesByValue​(@NonNull
                                                                                                              @NonNull Map<K,​V> map,
                                                                                                              boolean ascending)
        Sorts the entries in the map by value
        Type Parameters:
        K - the key type parameter
        V - the value type parameter
        Parameters:
        map - the map to sort
        ascending - True sort in ascending order, False in descending order
        Returns:
        the list of sorted map entries
      • sortedMapOf

        @SafeVarargs
        public static <K,​V> Map<K,​V> sortedMapOf​(@NonNull
                                                             @NonNull Map.Entry<? extends K,​? extends V>... objects)
        Creates a TreeMap from entries
        Type Parameters:
        K - the key type parameter
        V - the value type parameter
        Parameters:
        objects - the entries
        Returns:
        the map
      • tailKeyIterator

        public static <K> Iterator<K> tailKeyIterator​(@NonNull
                                                      @NonNull NavigableMap<K,​?> map,
                                                      @NonNull
                                                      K key)
        Creates an iterator that traverses a NavigableMap using ceilingKey starting from the given key.
        Type Parameters:
        K - the type parameter
        Parameters:
        map - the map
        key - the starting point key
        Returns:
        the iterator