Reputation: 635
I was working on a java program that takes a string and splits it into different levels.
Map<String, ArrayList<Map<String, ArrayList<Map<String,Map<String, String>>>>>> courseInfoMap = new HashMap<String, ArrayList<Map<String,ArrayList<Map<String,Map<String, String>>>>>>();
ArrayList<Map<String, ArrayList<Map<String,Map<String, String>>>>> courseNumList = new ArrayList<Map<String, ArrayList<Map<String,Map<String, String>>>>>();
Map<String, ArrayList<Map<String,Map<String, String>>>> courseNumTextbookMap = new HashMap<String, ArrayList<Map<String,Map<String, String>>>>();
ArrayList<Map<String,Map<String, String>>> listOfTextbooks = new ArrayList<Map<String,Map<String,String>>>();
Map<String, Map<String, String>> textbookMap = new HashMap<String, Map<String, String>>();
Map<String, String> isbnMap = new HashMap<String,String>();
Essentially, I'm trying to have a course code --> list of course num --> course num (key) --> list of textbooks --> textbook --> isbn number(str) --> price
So because some course codes have multiple course numbers it points to an arraylist.
I have been adding stuff backwards, so loops within loops. Assuming I did not screw up there. I've been trying to loop through the elements in the same sort of way assuming they were probably added.
for(Map.Entry<String, ArrayList<Map<String, ArrayList<Map<String,Map<String, String>>>>>> entry : courseInfoMap.entrySet()){
System.out.println(entry.getKey());// COURSE CODE
for(int i = 0; i < entry.getValue().size();i++){//loops through arraylist of course numbers
System.out.println("i("+i+" - " + entry.getValue().get(i));
for(Map.Entry<String, ArrayList<Map<String,Map<String,String>>>> entry2 : entry.getValue().get(i).entrySet()){
System.out.println(" " + entry2.getKey());
// . . . and so on
This hasnt worked it just loops through everything. On each level.
My question is how do I navigate through a large number of hashmaps and/or is there a better way to do this.
Example:
AAAA
100
Name
###
$$$
Name
### <-- different from past
$$$
200
Name
###
$$$
BBBB
101
Name
###
$$$
Upvotes: 3
Views: 1229
Reputation: 31710
This seems exceedingly complicated, and frankly if I had to maintain that I'd really not look forward to it. Off the top of my head there are several other approaches you could probably take...
Use a relational database
Either an in-memory database or something like mysql or postgresql. They are designed for this sort of thing - organizing data and allowing for ad-hoc queries.
Put your data in Objects
Instead of Maps and Lists, just create an object that defines most of it in one place. Store that object in the List or Map and search for what you need directly. If you don't have too much data, a brute-force uninindexed search probably won't be that bad in wall-clock time.
Upvotes: 3