Java Design – How to Handle Nested Collections More Elegantly

data structuresdesignjava

My question is rather a design question. In my program I got to a data structure that looks something like this:

private ConcurrentHashMap<A, ConcurrentHashMap<B, ConcurrentHashMap<Integer, C>>> services  = new ConcurrentHashMap<A, ConcurrentHashMap<B, ConcurrentHashMap<Integer, C>>>();

Is there a way to handle such a data structure more elegantly?
Thanks!

edit: A, B and C are business classes. An A instance "can have" (as association) many Bs and a B "can have" many mappings Integer-C.

Best Answer

Create a class Triple with fields for A,B,Integer, override hashCode() and equals(), and use Map<Triple,C> instead of Map<A,Map<B,Map<Integer,C>>>

In this approach - you put all elements in one map, with a larger possible range of keys.

Related Topic