2

Once have a look at the part of my GUI alt text

Once a user selects a particular branch from the branch JComboBox, i have to query the database and get all the years applicable to that branch and add those years to the next JComboBox year and so forth. There is quite a bit of chance for the user to swap between his selection of branch, and i would find myself querying the database the same query again each time he changes his option, and it is highly unlikely that the data in the database is going to change in between these swapping.... So i decided that i store these in some data structure, what is the best choice i have for one such datastructure? there may be 2 to 3 different branches, 4 to 6 different years and so on.....
What is my best choice?

sasidhar
  • 7,523
  • 15
  • 49
  • 75
  • 4
    unrelated: it should be "Semester" instead of "Semister" – Nishant Jan 09 '11 at 16:47
  • @Nishant yeah right...... :) – sasidhar Jan 09 '11 at 18:01
  • Is this a JavaScript GUI like extJS or GWT? Is leaking your DB to the client an issue? Is the DB slow? Are you using an ORM? Do you plan to use one? Is there any complicated business logic that needs to be done at the Java level? What about validation? Me I'd be tempted to answer *"none"* as the best data structure. It seems highly unlikely that DB calls for such infos would be slow and if the data *"is highly unlikely to change"* then an ORM like Hibernate will very gently take care of caching all this for you. Either you have a DB or not, but duplicating it in Java isn't smart. – SyntaxT3rr0r Jan 09 '11 at 18:37
  • @SpoonBender yeah you are right, but my situation is i will have my DB in a remote server and the network is pathetically slow... and besides i am supposed to build this application for exceedingly slow machines that run pentium 3 i guess... the old college computers completely outdated, in this light, i wanted to minimize the network overhead to the maximum amount feasible..... – sasidhar Jan 09 '11 at 19:00

3 Answers3

3

How about you create a Map where branch is the key and years, semesters and sections are in a VO object? Let's assume the VO is named BranchDetails then you could use something like Map<String,BranchDetails>().

BranchDetails could be something as simple as below:

class BranchDetails{
   List<Integer> years;
   List<Integer> semesters;
   List<Integer> sections;
   //getters and setters omitted for brevity 
}
CoolBeans
  • 20,654
  • 10
  • 86
  • 101
  • Is this what OP wants? The way I understood it, the semesters are dependant on the year and the sections dependant on the semester. Who's right? – moinudin Jan 09 '11 at 16:47
  • @marcog- In that case he would need nested Maps each key leading to its next set of possible values. I am not exactly sure which one he is looking for. – CoolBeans Jan 09 '11 at 16:49
  • The nested maps is what I answered. We'll see what OP says. – moinudin Jan 09 '11 at 16:50
  • & @marcog The semesters are dependent on the year and the sections are in turn dependent on the semester. It looks like nested HashMap is all that i need, but i am not sure how i am going to use the nested HashMap. Please give me some link that explains how i use nested HashMaps.. I am actually a newbie to the Collection Frameworks..... – sasidhar Jan 09 '11 at 17:26
2

If the application is single-threaded, use a HashMap. You can "nest" the HashMaps, e.g. HashMap<String, HashMap<Integer, HashMap<Integer, HashMap<String, Item>>>>. See this question to see how to iterate over the HashMap.

Here's some sample code using just two nestings to give you the idea:

HashMap<String, HashMap<Integer, HashMap<Integer, HashMap<String, String>>>> map1 = new HashMap<String, HashMap<Integer, HashMap<Integer, HashMap<String, String>>>>();
map1.put("Computers", new HashMap<Integer, HashMap<Integer, HashMap<String, String>>>());
map1.get("Computers").put(2011, new HashMap<Integer, HashMap<String, String>>());
map1.get("Computers").get(2011).put(2, new HashMap<String, String>());
Community
  • 1
  • 1
moinudin
  • 134,091
  • 45
  • 190
  • 216
  • the application is single threaded, but how do i map the same branch name to different years and in turn map these years to different semisters of the same branch and year? I quite didn't understand how i would use HashMap to this situation? Please help.. – sasidhar Jan 09 '11 at 16:38
  • @Sasidhar Did you see my edit where I nested the HashMaps? You can do it like that. – moinudin Jan 09 '11 at 16:39
  • @marcog thats what i wanted exactly, can you give me some example so that i can understand it better, i am not used to using Collection Frameworks and generic types.... – sasidhar Jan 09 '11 at 17:28
  • @sasidhar Sure I can. Read [this tutorial](http://www.javamex.com/tutorials/collections/using_4.shtml) first and tell me if it helps. – moinudin Jan 09 '11 at 17:40
  • @marcog in the code you have given, we are mapping "Cape Town branch" to 2011 and that in turn to "Item", but in my case, i want "Cape town branch" to map to multiple elements, say to 2011,2012,2013... while each year maps to multiple number of semesters... say to 1 and 2 or only to 1... how do i do this..? Its more like a tree i think..... – sasidhar Jan 09 '11 at 18:00
  • @Sasidhar I tried to keep the example short. Here's a more complete example: http://pastebin.com/Ru32CjRC – moinudin Jan 09 '11 at 18:05
  • @marcog MyEclipse is showing the following error in the above given code at line 3 :Multiple markers at this line -Type mismatch: cannot convert from String to int - The type of the expression must be an array type but it resolved to HashMap>>> [Note]: I have replaced the Item from your code and replaced it with String... I can't figure out what is the problem..? – sasidhar Jan 09 '11 at 19:28
  • @sasidhar Sorry, I hadn't tested the code I gave you and I was using C++ ideas :P You have to use `get`/`put` instead of `[]`: http://ideone.com/tvFE3 – moinudin Jan 09 '11 at 19:43
  • @marcog excellent, thank you very much... that exactly suits my requirement.. one last question though.. what happens when there is a hash collision? I mean what if two keys are same? Like this: http://ideone.com/unrgi how do we get the other value with the same key? – sasidhar Jan 09 '11 at 20:12
  • @sasidhar A collision will result in the last key overriding all those before it. That's probably what you want here though - if a user selects a branch, how will you know which one it is if there are duplicates? If you do need to have multiple keys, look at the multimap section in [this article](http://download.oracle.com/javase/tutorial/collections/interfaces/map.html). – moinudin Jan 09 '11 at 20:23
  • @marcog of course i want multiple keys, for the last section, there are two sections "A" and "B" for the same semester 2 as i indicated in the above code. Thanks a lot that really helped... – sasidhar Jan 09 '11 at 20:29
1

I would just keep them in separate lists with the complete data. (they are a small).

List<Branch> branches;
List<Year> years;
List<Semester> semesters;
List<Section> sections;

where each object Branch, Year, Semester, Section has the neccessary data to link them to another. For example a Branch could contains a list of yearId, etc mapping as closely as possible the actual database structure (to make the initial loading quick and easy).

Having that once you have a state change in the UI you just replicate the actual database query on this data structure and return what you need.

Mihai Toader
  • 12,041
  • 1
  • 29
  • 33
  • i didn't quite really get how we implement your idea practically, but any how HashMap seems promising enough. – sasidhar Jan 09 '11 at 20:32
  • The Map approach will work sometimes and you should do it to see how it goes. I didn't have time to explain this in more detail unfortunately. – Mihai Toader Jan 09 '11 at 23:03