Java Utililty Methods Map Create

List of utility methods to do Map Create

Description

The list of methods to do Map Create are organized into topic(s).

Method

MapcreateMap(Object... args)
Helper function to create maps easier.
assert args.length % 2 == 0;
Map<Object, Object> result = new HashMap<Object, Object>();
for (int i = 0; i < args.length; i += 2) {
    result.put(args[i], args[i + 1]);
return result;
MapcreateMap(Object... objects)
Create Map filled with values.
Map<T, X> map = new HashMap<>();
for (int i = 0; i < objects.length; i++) {
    map.put((T) objects[i], (X) objects[++i]);
return map;
MapcreateMap(Object... objects)
create Map
if (isEmpty(objects)) {
    return Collections.emptyMap();
if (objects.length % 2 != 0) {
    throw new IllegalArgumentException("objects should be even");
Map<K, V> map = new HashMap<>();
for (int i = 0; i < objects.length; i++) {
...
MapcreateMap(Object[] keysValues)
create Map
Map ret = new HashMap();
for (int i = 0; i < keysValues.length; i = i + 2) {
    ret.put(keysValues[i], keysValues[i + 1]);
return ret;
MapcreateMap(String entries)
create Map
return fillMap(new HashMap<String, String>(), entries);
MapcreateMap(String key, Object value)
create Map
return createMap(new String[] { key }, new Object[] { value });
MapcreateMap(String[] list1, String[] list2)
Create a map from the two list of strings.
final Map<String, String> ret = new HashMap<String, String>(list1.length);
for (int i = 0; i < list1.length; i++) {
    ret.put(list1[i].trim(), list2[i].trim());
return ret;
MapcreateMap(String[] mappings, String sep)
Create map from string description.
Map<String, String> dict = new HashMap<>();
for (String m : mappings) {
    String[] parts = m.split(sep, 2);
    if (parts.length == 1) {
        dict.put(m, m);
    } else {
        dict.put(parts[0], parts[1]);
return dict;
MapcreateMap(T... _args)
Creates a map from the even-sized parameter array.
Map<T, T> map = new HashMap<>();
if (_args != null) {
    if (_args.length % 2 != 0) {
        throw new IllegalArgumentException(
                "Even number of parameters required to create map: " + Arrays.toString(_args));
    for (int i = 0; i < _args.length;) {
        map.put(_args[i], _args[i + 1]);
...
MapcreateMap(U... items)
create Map
Map<U, U> result = new HashMap<U, U>();
for (U item : items) {
    result.put(item, item);
return result;