Thursday, 18 March 2010

MultiKeyMap , MultiValueMap - Apache commons

Very interesting implementation for a map. Something which I have been trying to implement or use for a while.

This has proved to be very useful, when we are iterating over collection of entities whose primary keys are composite then in such cases multi key maps can be used.

Simply use it like this
For example if an employee is uniquely identified by his id and department id then we can add it like this.
MultiKeyMap map = new MultiKeyMap();
map.put(employeeID, deptID, employee);

when getting the value back simply say :

map.get(employeeID, deptID);

On the other hand if we wish to store a collection of objects for a given key we can use MultiValueMap.