Before the introduction to generics to the Java language I would have written classes encapsulating collections-of-collections-of-collections. For example:
class Account { private Map tradesByRegion; //KEY=Region, VALUE=TradeCollection } class TradeCollection { private Map tradesByInstrument; //KEY=Instrument, Value=Trade }
Of course, with generics, I can just do:
class Account { private Map<Region, Map<Instrument, Trade>> trades; }
I tend to now opt for option #2 (over a generified version of option #1) because this means I don’t end up with a proliferation of classes that exist solely for the purpose of wrapping a collection. But I have a nagging feeling that this is bad design (e.g. how many nested collections should I use before declaring new classes). Opinions?
2 is better because:
What is there to recommend 1? admittedly the Map< Integer , < Map < String, < Map< … generics are a bit hard to get used to, but to my eye it’s much easier to understand than code with maps, and lists of maps, and maps of lists of maps, and custom objects full of lists of maps.