net.dongliu.commons.collection.Maps Maven / Gradle / Ivy
package net.dongliu.commons.collection;
import javax.annotation.Nullable;
import java.util.*;
import java.util.function.Function;
/**
* Utils method for map
*
* @author Liu Dong
*/
public class Maps {
/**
* If map is null, return immutable empty map; else return self
*/
public static Map nullToEmpty(@Nullable Map map) {
return map == null ? Maps.of() : map;
}
/**
* Create immutable view of this Set.
*/
public static Map wrapImmutable(Map map) {
return Collections.unmodifiableMap(map);
}
/**
* Create a immutable map with content
*/
public static Map copyImmutable(Map map) {
return Collections.unmodifiableMap(new HashMap<>(map));
}
/**
* Convert map value, return new map
*/
public static Map convert(Map map, Function super V, ? extends U> function) {
Map newMap = Maps.create();
map.forEach((k, v) -> newMap.put(k, function.apply(v)));
return newMap;
}
/**
* Create immutable empty map
*/
public static Map of() {
return Collections.emptyMap();
}
/**
* Create immutable map
*/
public static Map of(Map.Entry extends K, ? extends V> value) {
return wrapImmutable(create(value));
}
/**
* Create immutable map
*/
public static Map of(Map.Entry extends K, ? extends V> value1,
Map.Entry extends K, ? extends V> value2) {
return wrapImmutable(create(value1, value2));
}
/**
* Create immutable map
*/
public static Map of(Map.Entry extends K, ? extends V> value1,
Map.Entry extends K, ? extends V> value2,
Map.Entry extends K, ? extends V> value3) {
return wrapImmutable(create(value1, value2, value3));
}
/**
* Create immutable map
*/
public static Map of(Map.Entry extends K, ? extends V> value1,
Map.Entry extends K, ? extends V> value2,
Map.Entry extends K, ? extends V> value3,
Map.Entry extends K, ? extends V> value4) {
return wrapImmutable(create(value1, value2, value3, value4));
}
/**
* Create immutable map
*/
public static Map of(Map.Entry extends K, ? extends V> value1,
Map.Entry extends K, ? extends V> value2,
Map.Entry extends K, ? extends V> value3,
Map.Entry extends K, ? extends V> value4,
Map.Entry extends K, ? extends V> value5) {
return wrapImmutable(create(value1, value2, value3, value4, value5));
}
@SafeVarargs
public static Map of(Map.Entry extends K, ? extends V>... values) {
if (values.length == 0) {
return of();
}
return wrapImmutable(create(values));
}
public static Map of(Collection extends Map.Entry> values) {
return wrapImmutable(create(values));
}
/**
* Create mutable empty map
*/
public static Map create() {
return new HashMap<>();
}
/**
* Create mutable map
*/
public static Map create(Map.Entry extends K, ? extends V> value) {
Map map = new HashMap<>();
map.put(value.getKey(), value.getValue());
return map;
}
/**
* Create mutable map
*/
public static Map create(Map.Entry extends K, ? extends V> value1,
Map.Entry extends K, ? extends V> value2) {
Map map = new HashMap<>();
map.put(value1.getKey(), value1.getValue());
map.put(value2.getKey(), value2.getValue());
return map;
}
/**
* Create mutable map
*/
public static Map create(Map.Entry extends K, ? extends V> value1,
Map.Entry extends K, ? extends V> value2,
Map.Entry extends K, ? extends V> value3) {
Map map = new HashMap<>();
map.put(value1.getKey(), value1.getValue());
map.put(value2.getKey(), value2.getValue());
map.put(value3.getKey(), value3.getValue());
return map;
}
/**
* Create mutable map
*/
public static Map create(Map.Entry extends K, ? extends V> value1,
Map.Entry extends K, ? extends V> value2,
Map.Entry extends K, ? extends V> value3,
Map.Entry extends K, ? extends V> value4) {
Map map = new HashMap<>();
map.put(value1.getKey(), value1.getValue());
map.put(value2.getKey(), value2.getValue());
map.put(value3.getKey(), value3.getValue());
map.put(value4.getKey(), value4.getValue());
return map;
}
/**
* Create mutable map
*/
public static Map create(Map.Entry extends K, ? extends V> value1,
Map.Entry extends K, ? extends V> value2,
Map.Entry extends K, ? extends V> value3,
Map.Entry extends K, ? extends V> value4,
Map.Entry extends K, ? extends V> value5) {
Map map = new HashMap<>();
map.put(value1.getKey(), value1.getValue());
map.put(value2.getKey(), value2.getValue());
map.put(value3.getKey(), value3.getValue());
map.put(value4.getKey(), value4.getValue());
map.put(value5.getKey(), value5.getValue());
return map;
}
@SafeVarargs
public static Map create(Map.Entry extends K, ? extends V>... values) {
Map map = new HashMap<>();
for (Map.Entry extends K, ? extends V> tuple : values) map.put(tuple.getKey(), tuple.getValue());
return map;
}
public static Map create(Collection extends Map.Entry> values) {
Map map = new HashMap<>();
for (Map.Entry tuple : values) map.put(tuple.getKey(), tuple.getValue());
return map;
}
/**
* Combine two list to map
*/
public static Map zip(List keys, List values) {
if (keys.size() != values.size()) {
throw new IllegalArgumentException("Key and value size not equals");
}
Map map = new HashMap<>();
Iterator keyIter = keys.iterator();
Iterator valueIter = values.iterator();
while (keyIter.hasNext() && valueIter.hasNext()) {
map.put(keyIter.next(), valueIter.next());
}
return map;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy