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!
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.
• 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.
'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.
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.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With