Java Utililty Methods Map Invert

List of utility methods to do Map Invert

Description

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

Method

MapinvertMap(final Map map)
Inverts the supplied map returning a new HashMap such that the keys of the input are swapped with the values.
final Map<V, K> out = new HashMap<V, K>(map.size());
for (final Entry<K, V> entry : map.entrySet()) {
    out.put(entry.getValue(), entry.getKey());
return out;
MapinvertMap(final Map map)
invert Map
final Map<V, K> inverted = new IdentityHashMap<>(map.size());
for (final Map.Entry<K, V> entry : map.entrySet()) {
    inverted.put(entry.getValue(), entry.getKey());
return Collections.unmodifiableMap(inverted);
MapinvertMap(Map map)
Inverts the supplied map returning a new HashMap such that the keys of the input are swapped with the values.
Map out = new HashMap(map.size());
for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
    Map.Entry entry = (Map.Entry) it.next();
    out.put(entry.getValue(), entry.getKey());
return out;
MapinvertMap(Map map)
Inverts the supplied map returning a new HashMap such that the keys of the input are swapped with the values.
Map out = new HashMap(map.size());
for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
    Map.Entry entry = (Map.Entry) it.next();
    out.put(entry.getValue(), entry.getKey());
return out;
MapinvertMap(Map map)
invert Map
Map<V, K> inverted = new HashMap<V, K>();
for (Map.Entry<K, V> entry : map.entrySet()) {
    inverted.put(entry.getValue(), entry.getKey());
return inverted;
MapinvertMap(Map map)
Invert map.
Map<V, K> inv = new HashMap<V, K>();
for (Entry<K, V> entry : map.entrySet())
    inv.put(entry.getValue(), entry.getKey());
return inv;
MapinvertMap(Map map)
Inverts the given map from key-value mappings to value-key mappings

Note: this may have unintended results if a certain value is included in the map more than once

Map<V, K> inverted = new HashMap<V, K>();
for (Map.Entry<K, V> entry : map.entrySet()) {
    inverted.put(entry.getValue(), entry.getKey());
return inverted;
HashMapinvertStringMap(Map input)
Inverts Map of Strings (values become keys, keys become values).
HashMap<String, String> output = new HashMap<String, String>(input.size());
Set<Map.Entry<String, String>> entries = input.entrySet();
for (Map.Entry<String, String> entry : entries) {
    output.put(entry.getValue(), entry.getKey());
return output;