• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling one map from another

 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but it would have been quicker to just write a short test program to see what happened.
 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any 'Object' can be put in a Map. And a Map is also an 'Object'.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rakesh sugirtharaj:
I m still not sure but hope this helps. . .


Well done giving useful answers.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic