Java Utililty Methods Map Reverse

List of utility methods to do Map Reverse

Description

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

Method

MapreverseMap(Map map)
'reverse' a map's key and values, beware this may cause losing some entries(values may have duplications)
Map<V, K> ret = new HashMap<V, K>();
for (Map.Entry<K, V> e : map.entrySet()) {
    ret.put(e.getValue(), e.getKey());
return ret;
MapreverseMap(Map origMap)
reverse Map
if (origMap == null)
    return null;
Map<Object, Object> reverseMap = new HashMap<Object, Object>();
for (Object key : origMap.keySet()) {
    putMultiple(reverseMap, origMap.get(key), key);
return reverseMap;
MapreverseMap(Map map)
Convert String->Integer map to Integer->String map
if (map == null) {
    return null;
Map<Integer, String> rmap = new HashMap<>();
for (Map.Entry<String, Integer> entry : map.entrySet()) {
    rmap.put(entry.getValue(), entry.getKey());
return rmap;
...
MapreverseMap(Map keyIdMap, Map keyValueMap)
reverse Map
Map<K, G> ret = new HashMap<K, G>();
for (Entry<String, G> entry : keyValueMap.entrySet()) {
    K id = keyIdMap.get(entry.getKey());
    ret.put(id, entry.getValue());
return ret;
KreverseMapLookup(Map map, V value)
reverse Map Lookup
for (Entry<K, V> entry : map.entrySet()) {
    if (entry.getValue() == value) {
        return entry.getKey();
return null;
voidreverseMapping(Map value2keys, Map map)
reverse Mapping
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry entry = (Map.Entry) it.next();
    Object value = entry.getValue();
    Object key = entry.getKey();
    List keys = (List) value2keys.get(value);
    if (keys == null)
        keys = new LinkedList();
...
Map>reverseMapping(Map map)
reverse a Map to give a mapping from values to a list of keys
Map<V, List<K>> rev = new HashMap<>();
for (Map.Entry<K, V> e : map.entrySet()) {
    V v = e.getValue();
    if (!rev.containsKey(v)) {
        rev.put(v, new ArrayList<>());
    rev.get(v).add(e.getKey());
return rev;
Map>sort(Map> dataNodes, boolean reverse)
sort
Map<Long, List<String>> result = getSortedMap(reverse);
for (String hostName : dataNodes.keySet()) {
    Map<Long, Long> usage = dataNodes.get(hostName);
    long space = usage.values().iterator().next();
    List<String> hosts = result.get(space);
    if (hosts == null) {
        hosts = new ArrayList<>();
    hosts.add(hostName);
    result.put(space, hosts);
return result;
Mapsort(Map source, boolean reverse)
sort
Map<String, Object> result = new LinkedHashMap<String, Object>();
List<String> keys = new ArrayList<String>(source.keySet());
Collections.sort(keys);
if (reverse) {
    Collections.reverse(keys);
for (String key : keys) {
    result.put(key, source.get(key));
...
MapsortByUsedSpace(Map> dataNodes, boolean reverse)
sort By Used Space
Map<Long, List<String>> sorted = sort(dataNodes, reverse);
Map<String, Long> result = new LinkedHashMap<>();
for (long space : sorted.keySet()) {
    List<String> hosts = sorted.get(space);
    for (String host : hosts) {
        result.put(host, space);
return result;