• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calling one map from another

 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but it would have been quicker to just write a short test program to see what happened.
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rakesh sugirtharaj:
I m still not sure but hope this helps. . .



Well done giving useful answers.
 
reply
    Bookmark Topic Watch Topic
  • New Topic