Package com.gengoai.collection
Class Maps
- java.lang.Object
-
- com.gengoai.collection.Maps
-
public final class Maps extends Object
Convenience methods for creating, reading, and manipulating maps.
- Author:
- David B. Bracewell
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <K,V>
Map<K,V>asHashMap(@NonNull Iterable<? extends K> keys, @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.static <K,V>
Map<K,V>create(@NonNull Class<? extends Map> clazz)
Creates an instance of the given map class.static <K,V>
Map<K,V>hashMapOf(@NonNull Map.Entry<? extends K,? extends V>... objects)
Creates aHashMap
from entriesstatic <K,V>
LinkedHashMap<K,V>linkedHashMapOf(@NonNull Map.Entry<? extends K,? extends V>... objects)
static <K,V>
Map<K,V>mapOf(@NonNull Supplier<? extends Map<K,V>> supplier, @NonNull Map.Entry<? extends K,? extends V>... objects)
Creates a map of type returned via the givne supplier from the given entriesstatic <K,V>
Map<K,V>putAll(@NonNull Map<K,V> map, @NonNull Map.Entry<? extends K,? extends V>... entries)
Puts all given entries into the given mapstatic <K,V>
Map<K,V>sortedMapOf(@NonNull Map.Entry<? extends K,? extends V>... objects)
Creates aTreeMap
from entriesstatic <K,V>
List<Map.Entry<K,V>>sortEntries(@NonNull Map<K,V> map, @NonNull Comparator<Map.Entry<K,V>> comparator)
Sorts the entries in the mapstatic <K extends Comparable<? super K>,V>
List<Map.Entry<K,V>>sortEntriesByKey(@NonNull Map<K,V> map, boolean ascending)
Sorts the entries in the map by keystatic <K,V extends Comparable<? super V>>
List<Map.Entry<K,V>>sortEntriesByValue(@NonNull Map<K,V> map, boolean ascending)
Sorts the entries in the map by valuestatic <K> Iterator<K>
tailKeyIterator(@NonNull NavigableMap<K,?> map, K key)
Creates an iterator that traverses a NavigableMap usingceilingKey
starting from the given key.
-
-
-
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 parameterV
- the value type parameter- Parameters:
keys
- the keysvalueMapper
- 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 typeV
- 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 aHashMap
from entries- Type Parameters:
K
- the key type parameterV
- 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 parameterV
- the value type parameter- Parameters:
supplier
- the new map supplierobjects
- 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 parameterV
- the value type parameter- Parameters:
map
- the map to add the entries toentries
- 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 parameterV
- the value type parameter- Parameters:
map
- the map to sortcomparator
- 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 parameterV
- the value type parameter- Parameters:
map
- the map to sortascending
- 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 parameterV
- the value type parameter- Parameters:
map
- the map to sortascending
- 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 aTreeMap
from entries- Type Parameters:
K
- the key type parameterV
- the value type parameter- Parameters:
objects
- the entries- Returns:
- the map
-
linkedHashMapOf
@SafeVarargs public static <K,V> LinkedHashMap<K,V> linkedHashMapOf(@NonNull @NonNull Map.Entry<? extends K,? extends V>... objects)
-
tailKeyIterator
public static <K> Iterator<K> tailKeyIterator(@NonNull @NonNull NavigableMap<K,?> map, @NonNull K key)
Creates an iterator that traverses a NavigableMap usingceilingKey
starting from the given key.- Type Parameters:
K
- the type parameter- Parameters:
map
- the mapkey
- the starting point key- Returns:
- the iterator
-
-