Java Utililty Methods Array to Map

List of utility methods to do Array to Map

Description

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

Method

HashMaparrayToMap(int[] array)
array To Map
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < array.length; i++) {
    map.put(i + 1, array[i]);
return map;
MaptoMap(K[] keys, V[] values)
to Map
HashMap<K, V> map = new HashMap<K, V>();
if (keys != null) {
    for (int i = 0; i < keys.length; i++) {
        map.put(keys[i], values != null ? (i < values.length ? values[i] : null) : null);
return map;
MaptoMap(K[] keys, V[] values)
Creates a mutable map out of two arrays with keys and values.
int keysSize = (keys != null) ? keys.length : 0;
int valuesSize = (values != null) ? values.length : 0;
if (keysSize == 0 && valuesSize == 0) {
    return new HashMap<>();
if (keysSize != valuesSize) {
    throw new IllegalArgumentException("The number of keys doesn't match the number of values.");
Map<K, V> map = new HashMap<>();
for (int i = 0; i < keysSize; i++) {
    map.put(keys[i], values[i]);
return map;
MaptoMap(String... args)
Transforms the given array into a key-value map using the following rules
  • keys start with a dash (-) e.g.
    Map<String, Object> rv = new HashMap<>();
    for (int i = 0; i < args.length; i++) {
        if (args[i].startsWith("-")) { 
            List<String> l = new ArrayList<>();
            String key = args[i].substring(1);
            if (i + 1 < args.length && !args[i + 1].startsWith("-")) { 
                do {
                    l.add(args[++i]);
    ...
    
MaptoMap(String... keysAndValues)
to Map
Map<String, String> ret = new HashMap<String, String>();
if (keysAndValues != null) {
    for (int i = 0; i < keysAndValues.length; i += 2) {
        ret.put(keysAndValues[i], keysAndValues[i + 1]);
return ret;
MaptoMap(String[] keyNames, T[] values)
to Map
if (values != null && keyNames != null && values.length >= keyNames.length) {
    Map<String, T> map = new HashMap<String, T>(keyNames.length);
    for (int i = 0; i < keyNames.length; i++) {
        map.put(keyNames[i], values[i]);
    return map;
} else {
    return new HashMap<String, T>(0);
...
MaptoMap(String[] keys)
Convert array of strings to string→index map.
Map<String, Integer> m = new HashMap<String, Integer>();
for (int i = 0; i < keys.length; i++) {
    m.put(keys[i], i);
return m;
MaptoMap(String[]... wordMappings)
to Map
Map<String, String> mappings = new HashMap<String, String>();
for (int i = 0; i < wordMappings.length; i++) {
    String singular = wordMappings[i][0];
    String plural = wordMappings[i][1];
    mappings.put(singular, plural);
return mappings;
MaptoMap(String[][] checksumsArray)
to Map
Map<String, String> checksums = new HashMap<String, String>();
for (String[] checksumPair : checksumsArray) {
    checksums.put(checksumPair[0], checksumPair[1]);
return Collections.unmodifiableMap(checksums);
MaptoMap(String[][] strings)
Create a map from an array of string arrays as follows:

For {{key1, value1}, {key2, value2}, ...}, create a map of the keys to values.

final Map<String, String> result = new HashMap<String, String>();
for (String[] array : strings) {
    for (int i = 0; i < array.length - 1; i += 2) {
        result.put(array[i], array[i + 1]);
return result;