MultiKey Map an Amazing Data Structure
Apache Commons API is just Amazing; it’s worth exploring and
if you are not familiar with it try exploring “Google Guava” a much new API.
It’s much better than Apache Commons Collections API.
It handles Generics and is thread safety.
Till now you will only know map as a key, value pair. You can
get the value by map.get(key).
Have you ever owned a Locker in a Bank, or have seen big
Lockers in Jewellery shops to store diamonds. Those will have multiple keys,
here I don’t mean a duplicate key but I want to emphasize that you can unlock
them only via a sequence of Key. In a
typical bank locker I key remains with the Bank and the other is given to the
Owner. Whenever you visit the bank to take out/keep your valuables the bank
official will go along with you and use his key to unlock it. Here the locker
is not open yet, after that you have to use your key to unlock the locker. Here
this is just done to ensure security. Note that the sequence is very important
here.
Multikey Locker |
The multi key map also work on
the same lines. You can have a set of keys to map a value. Let’s us understand
this with below Figure. People who are already aware of Hashed data structures
will be able to enjoy this example in a better way.
Syntax of a Normal
Java Map
Map map = new HashMap ()//
Generics supported
Syntax of a MultikeyMap
MultikeyMap multikeyMap
= new MultiKeyMap()// It does not support generics
To understand it you
can assume
MultikeyMap multikeyMap ;
We will be learning more
about multyikey map with the below example.
Let us discuss the
Example:
We have to fetch the
name of the person based on the city and company;
Lets consider the
below data
MultiKeyMap multiKeyMap = new MultiKeyMap();
multiKeyMap.put("New York","IBM","Sam");
multiKeyMap.put("Sydney","Infosys","Honey");
multiKeyMap.put("Prague","JP
Morgan","Peter");
multiKeyMap.put("Scotland","RBS","Deny");
multiKeyMap.put("Paris","Nomura","Lily");
multiKeyMap.put("Melbourne","Citi
Bank","Sandy");
multiKeyMap.put("Aukland","Bank
of America","Tommy");
Values stored in the memory:
Hascode
|
Multi
Keys
|
Value
|
11111111
|
New
York,IBM
|
Sam
|
22222222
|
Sydney,Infosys
|
Honey
|
33333333
|
Prague,JP
Morgan
|
Peter
|
44444444
|
Scotland,RBS
|
Deny
|
55555555
|
Paris,Nomura
|
Lily
|
66666666
|
Melbourne,Citi
Bank
|
Sandy
|
77777777
|
Aukland,Bank
of America
|
Tommy
|
Now if we say
1) System.out.println(multiKeyMap.get("Sydney","Infosys"));
2) System.out.println(multiKeyMap.get("Paris","Nomura"));
3) System.out.println(multiKeyMap.get("Nomura","Paris"));
4) System.out.println(multiKeyMap.get("Sydney","IBM"));
We get the below outputs:
1)
Honey // match was found
2)
Lily // match was found
3)
null // Because here we reversed the Order of the keys and no
hashcode was found for that multikey
mapped
4) null
// here we gave two different keys and it was not mapped.
Note that MultiKeyMap is not synchronized and is
not thread-safe. If you wish to use this map from multiple threads
concurrently, you must use appropriate synchronization.
Refer the Apache Commons API for more information
Is it possible to define Keys which may be different?
ReplyDeleteexample: MultiKeyMap multiKeyMap = new MultiKeyMap();
multiKeyMap.put("NewYork","Engineering","Frank");
multiKeyMap.put("SanFrancisco","California","Sales","David");
so first put has 2 Objects as keys and second put has 3 Objects.
Hi David, that is very much possible. You can have any combination of keys but while fetching values from the map you have to maintain the same sequence of the keys. Better we can execute this and see the result.
ReplyDelete