Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data-structure of Pairs where each value (in pair) Maps to other value?

I'm back again with a similar question. Is there a DataType that can return its specific partner? For example:

ExampleType<String,String> test = new ExampleType<String,String>();
test.put("hello","hi");

If I were to type test.get("hi"), it would return "hello", and if I were to type test.get("hello"), it would return "hi".

My only guess for this would maybe be a two dimensional array, but I'm not sure how I would implement this. As of right now, the only way I can understand of doing this is creating two different hashmaps and swapping the keys in each. (obviously this is not very efficient/effective).

Thanks for all the help!

like image 323
SgtStud Avatar asked Mar 27 '12 01:03

SgtStud


People also ask

Which data structure is used for map?

The map data type is referred to as an associative array because, like an array, it is a collection of values rather than a single value, as an Int or a String is. Furthermore, each distinct key is associated with a value, resulting in an associative array.

What is mapping in data structure?

• A Map is an abstract data structure (ADT) • it stores key-value (k,v) pairs. • there cannot be duplicate keys. • Maps are useful in situations where a key can be viewed as a unique identifier for the object. • the key is used to decide where to store the object in the structure.

What is sequential mapping in data structure?

'Sequential' would mean that the data structure has an order which may be used to order operations on the elements of the data structure. There is a sense that this should be a natural order, but the term could also mean that there is an order imposed on the data structure which enables iterative operations.

What is the data structure used for store key-value pair?

A key-value database is a type of nonrelational database that uses a simple key-value method to store data. A key-value database stores data as a collection of key-value pairs in which a key serves as a unique identifier. Both keys and values can be anything, ranging from simple objects to complex compound objects.


2 Answers

You can use the Guava's BiMap for this. It supports inverse lookup as well:

A bimap (or "bidirectional map") is a map that preserves the uniqueness of its values as well as that of its keys. This constraint enables bimaps to support an "inverse view", which is another bimap containing the same entries as this bimap but with reversed keys and values.

If you already have a dependency on commons-collections then you can also use BidiMap.

like image 129
Aravind Yarram Avatar answered Sep 24 '22 19:09

Aravind Yarram


Nothing built in, so you either use a 3rd party package like Guava's BiMap mentioned by Pangea, or, if you want to roll your own, if you need two distinct data types, the 2-maps idea is not bad, if your keys and values are the same type you can use a single map with doubled entries:

public class BiMap<T>{

    private Map<T,T> theMap = new HashMap<T,T>();

    public void put( T key, T value ){ put( key, value, false ); }
    public void forcePut( T key, T value ){ put( key, value, true ); }

    private void put( T key, T value, boolean force ){
        if( force || !theMap.containsKey(value) ){
            theMap.remove( theMap.remove( key ) );
            theMap.put( key, value );
            theMap.put( value, key );
        }else if( !theMap.get( value ).equals( key ) ){
            // If you allow null values&keys this will get more complicated.

            throw new IllegalArgumentException();
            // can make this more informative.
        }
        // else the pair is already in, there's nothing to do.
    }

    public T get( T key ){ return theMap.get( key ); }

    public T remove( T key ){
        T value = theMap.remove( key );
        if( value != null ) theMap.remove( value );
        return value;
    }
}

Notice that because everything is an object, there isn't much waste as far as efficiency/space is concerned: the only things you're storing twice are object addresses. The same goes for your 2-map idea.

Also it wouldn't be too hard to add the necessary methods to make this implement the Map<T,T> interface for compliance's sake.

like image 30
trutheality Avatar answered Sep 26 '22 19:09

trutheality