0

Are there any potential issues that could arise from using a Key on different maps as follows.

Map<Key, Value> employeeBasicInfo= new HashMap<>(); 
Map<Key, Value> employeeDetailInfo = new HashMap<>(); 

....
//EDIT: Lets assume this is a unique key where every department has unique employee names. 
 String firstName = "John";
String lastName = "Smith";
String department = "IT";
Key employeeKey = new Key(firstName, lastName, department );

Value employeeBasicInfo = new Value(salary,grade,dateHired....);

Value employeeDetail = new Value(performanceEvaluation,benefit,familyInfo,...);

employeeBasicInfo.put(employeeKey, employeeBasicInfo);
employeeDetailInfo.put(employeeKey, employeeDetail);

NOTE:The key is used twice with employeeBasicInfo and employeeDetailInfo

Assuming the key above will be unique(no two people will have the name John Smith in the same department), is this considered as a bad programming practice, if so what are the reasons?

Thank you!

6
  • I think that if there are 2 John Smith from IT you would get a problem. Maybe if you have a unique id for each employee would be safer. But this is a functional problem, not technical Jul 10, 2014 at 19:27
  • I agree, this is just a simple example I could come-up with. The Key is unique in the actual case.
    – user624558
    Jul 10, 2014 at 19:28
  • Beside, we are not planning to hire another John Smith in our IT department any time soon :)
    – user624558
    Jul 10, 2014 at 19:42
  • You just reject people who happens to have the same name?
    – Jimmy T.
    Jul 10, 2014 at 19:44
  • @JimmyT. Not really,I was just making fun of my own poor example :)
    – user624558
    Jul 10, 2014 at 20:04

3 Answers 3

2

It's okay to have one key point to two pieces of data, but the data structure for it feels wrong - you have to maintain two separate maps, and if the data in one is mutated, then you've lost the key forever.

You shouldn't mutate the key, but what you can do is store them in one data structure: a Multimap. It's part of Guava's suite.

final Multimap<Key, Value> employeeInfo = ArrayListMultimap.create();
employeeInfo.put(employeeKey, employeeBasicInfo);
employeeInfo.put(employeeKey, employeeDetail);

What this gives you is equivalent (in a sense) to a Map<Key, Collection<Value>>, and since you know the order in which you put these elements in, retrieval is straightforward.

Value basicInfo = ((List<Value) employeeInfo.get(employeeKey)).get(0);
Value detail = ((List<Value>) employeeInfo.get(employeeKey)).get(1);

You have to perform the cast, since Collection has no notion of a get(int index).

3
  • Interesting solution. I wonder if the get and put methods have a constant-time complexity similar to a HashMap
    – user624558
    Jul 10, 2014 at 20:02
  • Seems like it would; retrieval from a map is constant, and retrieval from a list with index is constant.
    – Makoto
    Jul 10, 2014 at 20:03
  • This would work better than keeping two maps, the only problem is I am planning to keep one of the maps as a cache and reset the other one after processing the data. The API doesn't show a way to remove only one of the values from the MultiMap.
    – user624558
    Jul 10, 2014 at 20:11
1

It's perfectly valid to use the same Key on different maps (as long as you don't mutate the key value). Using the same value is fine too (as long as you don't mutate it). For example, I might use a "name" to index a person's address and their date of birth in two maps.

0

In general it is very common to have the same key in diffrent maps. There is no problem with that.

In your specific example you have name and department as key but at least the department could change. In that case the map would not work. I can't see why you even need the map in the first place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.