org.codefilarete.tool.collection.Maps Maven / Gradle / Ivy
package org.codefilarete.tool.collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
/**
* @author Guillaume Mary
*/
public final class Maps {
/**
* Allows chaining of the {@link ChainingMap#add(Object, Object)} method and then easily create a Map
* @return a new Map instance
*/
public static ChainingMap asMap(K key, V value) {
return new ChainingMap().add(key, value);
}
/**
* Allows chaining of the {@link ChainingHashMap#add(Object, Object)} method and then easily create a Map
* @return a new HashMap instance
*/
public static ChainingHashMap asHashMap(K key, V value) {
return new ChainingHashMap().add(key, value);
}
/**
* A simple factory method that takes only expected key and value types, made to avoid casting when using method factory with key and value
* (that next call to add(..) requires casting of key and/or values)
*
* @param keyType key type
* @param valueType value type
* @param key type
* @param value type
* @return a new {@link HashMap} that allows chaining of additional key and value
*/
public static ChainingHashMap forHashMap(Class keyType, Class valueType) {
return new ChainingHashMap<>();
}
/**
* Returns a {@link Map} of given {@link Map}s values that are mapped on the same keys in both {@link Map}s.
*
* @param map1 a {@link Map}
* @param map2 a {@link Map}
* @param {@link Map}s key type
* @param first {@link Map} values type
* @param second {@link Map} values type
* @return a {@link Map} of values that are in both {@link Map}s and mapped under the same keys
*/
public static Map innerJoin(Map map1, Map map2) {
Map result = new HashMap<>();
map1.forEach((k, v) -> {
if (map2.containsKey(k)) {
result.put(v, map2.get(k));
}
});
return result;
}
/**
* Returns a {@link Map} of given first {@link Map} keys and second {@link Map} values
* by joining them on first {@link Map} values and second {@link Map} keys.
*
* @param map1 a {@link Map}
* @param map2 a {@link Map}
* @param {@link Map}s key type
* @param first {@link Map} values and second {@link Map} keys type
* @param second {@link Map} values type
* @return a {@link Map} of first {@link Map} keys and second {@link Map} values joined on first {@link Map} values and second {@link Map} keys.
*/
public static Map innerJoinOnValuesAndKeys(Map map1, Map map2) {
Map result = new HashMap<>();
map1.forEach((k, v1) -> {
V2 v2 = map2.get(v1);
result.put(k, v2);
});
return result;
}
/**
* Puts all elements of map1 and map2 into a new {@link HashMap}.
* Made to have a putAll(..) method inlined.
*
* @param map1 any non null {@link Map}
* @param map2 any non null {@link Map}
* @param {@link Map}s key type
* @param {@link Map}s value type
* @return a new {@link HashMap} containing elements of map1 and
*/
public static Map putAll(Map extends K, ? extends V> map1, Map extends K, ? extends V> map2) {
Map result = new HashMap<>(map1);
result.putAll(map2);
return result;
}
/**
* Simple {@link LinkedHashMap} that allows to chain calls to {@link #add(Object, Object)} (same as put) and so quickly create a Map.
*
* @param
* @param
*/
public static class ChainingMap extends LinkedHashMap {
public ChainingMap() {
super();
}
public ChainingMap add(K key, V value) {
put(key, value);
return this;
}
}
/**
* Simple {@link HashMap} that allows to chain calls to {@link #add(Object, Object)} (same as put) and so quickly create a Map.
*
* @param
* @param
*/
public static class ChainingHashMap extends HashMap {
public ChainingHashMap() {
super();
}
public ChainingHashMap add(K key, V value) {
put(key, value);
return this;
}
}
/**
* Simple {@link TreeMap} that allows to chain calls to {@link #add(Object, Object)} (same as put) and so quickly create a Map.
*
* @param
* @param
*/
public static class ChainingComparingMap extends TreeMap {
public ChainingComparingMap(Comparator comparator) {
super(comparator);
}
public ChainingComparingMap add(K key, V value) {
put(key, value);
return this;
}
}
}