Reverse Map Lookup

Reverse map lookup

There isn't much you can do about it. Your have options to work with two maps, use multi-key map like one from Boost Multi-Index library, or do linear search.

UPDATE: The most lightweight out of the box solution seems to be Boost.Bimap, which stands for bi-directional map.

Does Java have a HashMap with reverse lookup?

There is no such class in the Java API. The Apache Commons class you want is going to be one of the implementations of BidiMap.

As a mathematician, I would call this kind of structure a bijection.

reverse lookup in a map

Try

(some #(if (= (val %) your-val) (key %)) your-map) 

Reverse a map in value, key format in Golang

You may write a for loop to iterate over the key-value pair of original map, and put them in a new map (see function reverseMap)

Code@https://play.golang.org/p/5y1gABTpdb8

package main

import (
"fmt"
)

func main() {
fruit_map := map[string]string{
"apple": "likey",
"orange": "no likey",
}

reversedMap := reverseMap(fruit_map)
fmt.Println(reversedMap)
}

func reverseMap(m map[string]string) map[string]string {
n := make(map[string]string, len(m))
for k, v := range m {
n[v] = k
}
return n
}

Output:

map[likey:apple no likey:orange]

BTW, it is not idiomatic to name go variable like fruit_map, you really should use camel-case, like fruitMap.

reverse map value c++

You can iterate over your map forward and backwards at the same time:

std::map<int, int>::const_iterator forwardIt = mapOriginal.begin();
std::map<int, int>::const_reverse_iterator reverseIt = mapOriginal.rbegin();
for ( ;
forwardIt != mapOriginal.end() ;
++forwardIt, ++reverseIt)
{
mapReverse[forwardIt->first] = reverseIt->second;
}

However, that seems like an unusual use of maps. Are you sure a vector would not fulfill your needs?

std::vector<int> vec { 3, 2, 1 };

std::vector<int> reverseVec;
std::reverse_copy(vec.begin(), vec.end(), std::back_inserter(reverseVec));
// or, if you want to reverse in-place:
std::reverse(vec.begin(), vec.end());

Java HashMap reverse way

You can use Guava BiMap :

BiMap<String, Long> map = HashBiMap.create();
map.put("a", 1L);
map.put("b", 2L);
map.put("c", 3L);

System.out.println(map.get("b")); // 2L
System.out.println(map.inverse().get(2L)); // "b"

An other alternative is Apache commons BidiMap :

BidiMap<String, Long> map = new DualHashBidiMap<>();
map.put("a", 1L);
map.put("b", 2L);
map.put("c", 3L);

System.out.println(map.get("b")); // 2L
System.out.println(map.inverseBidiMap().get(2L)); // "b"

Dafny reverse lookup map

You can use a "let such-that" statement to do that. For example:

method Test(m: map<char,int>, val: int)
requires exists i :: i in m && m[i] == val;
{
var i :| i in m && m[i] == val;
// now use i...
}

You can also invert the map as follows (but you don't need to just to do a single reverse lookup)

function method InvertMap(m: map<char,int>): map<int,char>
{
map b | b in m.Values :: var a :| a in m && m[a] == b; a
}

Reverse / invert a dictionary mapping

Python 3+:

inv_map = {v: k for k, v in my_map.items()}

Python 2:

inv_map = {v: k for k, v in my_map.iteritems()}

How can I reverse map using reflection

The key and value are of type reflect.Value, so passing them to reflect.TypeOf() will not return the type descriptors of the key and value types of the map (string and int), but instead the type descriptor of the reflect.Value type itself.

Instead simply call their Value.Type() method:

mapType := reflect.MapOf(value.Type(), key.Type())

With this it'll (almost) work and print (try it on the Go Playground):

map[1:one 2:two]

I wrote "almost" because you're returning a reflect.Value, not a map. But if a reflect.Value is passed to the fmt package, it prints the value wrapped inside it:

If the operand is a reflect.Value, the operand is replaced by the concrete value that it holds, and printing continues with the next rule.

So you should call Value.Interface() on out before returning it.

It's easier to return early if the kind is not map, so you can create the map right after that:

func ReverseMap(in interface{}) interface{} {
v := reflect.ValueOf(in)
if v.Kind() != reflect.Map {
return nil
}

mapType := reflect.MapOf(v.Type().Elem(), v.Type().Key())
out := reflect.MakeMap(mapType)

for _, key := range v.MapKeys() {
out.SetMapIndex(v.MapIndex(key), key)
}

return out.Interface()
}

Try this variant on the Go Playground.

Another approach may be using Value.MapRange():

for iter := v.MapRange(); iter.Next(); {
out.SetMapIndex(iter.Value(), iter.Key())
}

Try this variant on the Go Playground.

Javascript - map value to keys (reverse object mapping)

You could use Object.assign, while respecting the given array of the inserted values.

const city2country = { Amsterdam: 'Netherlands', Rotterdam: 'Netherlands', Paris: 'France' };const reverseMapping = o => Object.keys(o).reduce((r, k) =>        Object.assign(r, { [o[k]]: (r[o[k]] || []).concat(k) }), {})
console.log(reverseMapping(city2country));


Related Topics



Leave a reply



Submit