permaculture playing cards*
The moose likes Beginning Java and the fly likes Calling one map from another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling one map from another" Watch "Calling one map from another" New topic
Author

Calling one map from another

ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear all,
I'm a novice to java programming.
I've a slight programming doubt in map interface.
Suppose I've two Treemaps T1 and T2 .
The value for the first TreeMap should be another Treemap .

T1<key,T2>
T2<key,object>

Is it possible in Java?
Regards.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
Yes, but it would have been quicker to just write a short test program to see what happened.


Joanne
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
Any 'Object' can be put in a Map. And a Map is also an 'Object'.


Cheers!
RSR
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Any 'Object' can be put in a Map. And a Map is also an 'Object'.

No I want a simple program explanation or any links pertaining to this?
Regards.
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
Is it possible in java?
Sure it is possible and i recommend that you try to write the code. It is very easy unless there is some hard question buried inside this simple question which i have failed to understand.

Steps:
1. Create a Map T2<key,Value>
2. Create a Map T1<key,T2<key,Value>>
3. Put T2 in T1.
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Is it like this?
TreeMap treemap1=new TreeMap(new Comp());
TreeMap treemap2=new TreeMap(new Comp());
treemap2.put(qd.getQuotaState(),qd);
treemap1.put(qd.getAllocation(),treemap2.get(qd.getQuotaState()));


Here qd is an object instance which has some states which can be got by
calling
getAllocation() and getQuotaState();

My concerns are
1) Does the structure of building map of maps is correct?
2) How to lookup these map of maps to locate a particular qd instance, whose states(allocation and quotastate) matches those of my method parameters(allocation and quotastate) in which I use these map of maps.
3) Why they employ map of maps here by saving one of the qd's state in one TreeMap & other state with the qd instance in the other TreeMap?

The complete method is :
ISCCMQuotaDetail getQuotaDetail(ISCCMAllocation allocation,
ISCCMDirectPromotion directPromotion, ISCCMQuotaState quotaState,
ISCCollection details)
{
trace.println("!!getQuotaDetail method");
ISCCMQuotaDetail quotaDetail = null;
Iterator detailsItr=details.elements();
while(detailsItr.hasNext())
{
ISCCMQuotaDetail qd = (ISCCMQuotaDetail) detailsItr.next();
TreeMap treemap1=new TreeMap(new Comp());
TreeMap treemap2=new TreeMap(new Comp());
treemap2.put(qd.getQuotaState(),qd);
treemap1.put(qd.getAllocation(),treemap2.get(qd.getQuotaState()));
}
}
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
Ok Lets go through this one by one

The value here is not a map at all. If it had been then it is Map of map.

Next,
Does the structure of building map of maps is correct?
Sure, it is correct, if your program needs it. For example, a telephone directory is an example of map<Name and Number>. In case you are tourist agency and you have a number of telphone directories(of different cities) and you put them in shelves by alphabetic order, then you are using a Map pf maps.

Next,
How to lookup these map of maps to locate a particular qd instance, whose states(allocation and quotastate) matches those of my method parameters(allocation and quotastate) in which I use these map of maps.
I really dont understand the question, sorry!

Next,
Why they employ map of maps here by saving one of the qd's state in one TreeMap & other state with the qd instance in the other TreeMap?
That depends upon some requirement in the project and is a matter to be discussed with designer/developer who put that code/logic. I guess there should be some other part of the project that might require such a construct.
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
How to lookup these map of maps to locate a particular qd instance, whose states(allocation and quotastate) matches those of my method parameters(allocation and quotastate) in which I use these map of maps.

What i want to know is that suppose I want to identify a particular instance i.e. qd in those treemaps Treemap1 & Treemap2 how to lookup the treemaps & find that instance gd?
Hope you are clear now?
Regards.
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
I m still not sure but hope this helps.

If you know the key for the value(object) you can use get
If you are not sure if the object exists then you can use containsValue.
Else you can get a collection of values and iterate over them.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
Originally posted by rakesh sugirtharaj:
I m still not sure but hope this helps. . .


Well done giving useful answers.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Calling one map from another
 
Similar Threads
Hashtable vs Hashmap
Creating a compount HashMap key
List, ArrayList and Generics as UML
Question about inserting elements into a treemap as compared to arraylist?
sql syntax question