tags:

views:

59

answers:

1
HashMap<Pair<Class<T>,Boolean>,String> abc = new HashMap<Pair<Class<T>,Boolean>,String>();

I have two approaches here... create a key{class, Boolean} -> {string} or i could also do this.

{class} -> {Boolean, string}

the first approach has 1 level of indirection which the second approach has 2... what are the pros and cons here? Is the second approach bad?

+2  A: 

Ideally, the choice may best be driven by what's natural for the problem/solution domain. Does the Key<class, Boolean> represent anything in the domain? Does Map<Boolean, String> represent anything in the domain? Using 1-level or 2-levels of indirection may be an implementation detail you end up wanting to hide.

However, if it is purely a performance decision, all other things being equal (e.g. access pattern doesn't favor one way or the other, good hash function, sparse map, etc.), I would think HashMap<Key<X, Y>, Z> would be faster than HashMap<X, Map<Y, Z>> - I want to think that 1 lookup in a larger HashMap is faster than 2 lookups in smaller maps.

Since you have a boolean key, also consider 2 HashMap tables (one for true and one for false) and some ternary operator (?:) magic instead:

final Map<Class, String> falseMap = new HashMap<Class, String>();
final Map<Class, String> trueMap = new HashMap<Class, String>();

final String s = ((booleanKey ? trueMap: falseMap).get(classKey));
Bert F