• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Is a Map data type that holds three objects possible?

 
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm talking about something like this:



Or would it be better just to make a ProjectHolder object to hold these three things? I could then bind the object to a list of some type, the end goal is to bind this to a tree so I can build a tree that shows ownership between project, device and data. Right now I just have a loose list of project names, a list of device names and a list of channel names.

What does anyone think, any tips on how I might handle this with a better data type or making an object?
 
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,

it is possible

Please check org.apache.commons.collections.map.MultiKeyMap from apache collections
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matt Kohanek wrote:


No (atleast with standard Map interface & implementation from J2SE).


Or would it be better just to make a ProjectHolder object to hold these three things?



Yes. You can have an object holds name and two Lists for other objects (for devices & channel). There can be more ways to do this depending on your requirement.
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

see whether it will suit your requirement


 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about creating a Map of Maps:

The key into the top-level map is a String (the project name). The value is another Map, with device name as the key, and channel name as the value.
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jesper ,

Your code seems to OK for creating Maps of Maps with top level Map's Key as String .
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matt --

Please don't try to make maps of maps of maps, or 3-way maps, or anything as strange as that. Further, don't try to use Ganesh's code, which isn't going to compile. Try to use the simplest solution. The problem you're solving may seem strange to you, but it's really not strange at all; it's a very typical object modeling problem, and you want to solve it the same way it's been solved a million times before. Don't try to invent anything new and confusing, but rather try to study how other people have successfully approached it.

The traditional Java solution would be to create classes named Project, Device, and Channel, give each class instance variables to refer to the other types as needed, and then create the interconnected objects. You could then (for example) store all your Projects in a Map with their name as the key, and the Projects as the values, and easily look up Projects. You could then ask each Project for its Device and/or Channel.

Finally -- and I hope I'm not too out of line here, but I'm starting to feel bad watching you suffer this way -- when you ask for help, try stepping back a little and asking more about the problem, rather than how to implement a solution. To the best of my recollection, you've never asked "I have a bunch of data regarding interconnected objects stored in X format, and I want to display them as Y. What would be a good approach?" I suspect the answers might surprise you.
 
Matt Kohanek
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, and I have actually done the larger portion of that. I have the Projects, Devices, and Channels classes, and then I also have ProjectDevices, ProjectChannels, and ProjectPermisssions.

This data is stored on a web service in xml form, so it is just this next part that is where I have lost my way. I was thinking of creating a ProjectHolder class that has the variables projectName, deviceName, and channelName, and just making each project with all the devices and channels an object. I think that is similar to what you are saying, except your saying to hold them in a Map rather than an object right?

I know I am close with this, but I need it done in like 3 days lol, and I just don't have much time to spend on it unfortunately.
 
Matt Kohanek
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah I am still having problems with this, I can't seem to load the Map with what I need it to. Here is the code I came up with:



but when I call the load() followed by the print() methods it returns

Exception in thread "main" java.lang.NullPointerException
at nodesContainer.Nodes.load(Nodes.java:52)
at nodesContainer.AppNodes.main(AppNodes.java:20)

Which is an error I never get tired of seeing...

Still working on it, but if anyone has any tips that would help.

By the way, this does work - I can add either the projectid or the projectname to this list and when I print it out it shows just what you'd expect, a long list of names or Ids:



But it leaves me with a list that is just a bunch of projectNames.
 
Matt Kohanek
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it in the way i declare the Map? Instead of private Map <String, String> projectHolder;
do i need something more like
Map<String, String> projectHolder = new HashTable();

or something like that?

And thinking about the end goal, even if I do get this to work it may not be what is needed. The end goal is to bind this to a rich:tree that will have a layout like this:

projectName
--deviceName
----channelname

Will this solution work in the first place even if I do get it going?
 
Matt Kohanek
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so that was what i was doing wrong, I declared the Map incorrectly. Once I fixed that it does give me a Map with the projectId as the key and the projectname as the value.

Now I need help iterating through that I guess? I'm not too sure what the next step should be really. Something like I did with the projectNameList, where I go through and for every project n the collection i get the name. But now I need it to go through every project in this Map collection, and get it's device. The way the web service is setup, the deviceId is stored with the project Xml, so I would need to get the deviceId and then look up the deviceName by its ID.

But what do I do with this? Do I bind it to another list? This still seems like it is getting me a collection of projects and a collection of devices, but not shwoing the ownership between the two. Where I am now is essentially what i have already done, except this time I used a map rather than a list...

And still Im not sure how this is going to work binding this data to a rich:tree...
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matt Kohanek wrote:Now I need help iterating through that I guess?



I didn't go through your code, but what do you need to iterate through? Map or any other Collection ? Map has a keySet() you can use to iterate.
 
Matt Kohanek
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, that isnt the problem anymore actually. I can load a Map or List up with the objects like I need it to, but then I need to bind this Map or List to a rich:tree and have it display roots and nodes determined by the variables within that object.

So my big problem here is, what data type do I use to store the java Objects and then how do I bind that data type to a jsf page through tags and the tag attributes? I have been trying with rich:tree but havent had much luck unless I just use a List loaded with String variables. However when I try to do the same thing, the only difference is my List is loaded with java Objects, no tree will display.
 
Ganesh Gowtham
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all ,

Code which i had given is part of class MultiKeyMap

as such small snippet which i had pasted will give compilation erros i agree

Please check org.apache.commons.collections.map.MultiKeyMap from apache collections . to use entire functionality

as such in

Ernest Friedman-Hill wrote:Matt --

Please don't try to make maps of maps of maps, or 3-way maps, or anything as strange as that. Further, don't try to use Ganesh's code, which isn't going to compile. Try to use the simplest solution. The problem you're solving may seem strange to you, but it's really not strange at all; it's a very typical object modeling problem, and you want to solve it the same way it's been solved a million times before. Don't try to invent anything new and confusing, but rather try to study how other people have successfully approached it.

The traditional Java solution would be to create classes named Project, Device, and Channel, give each class instance variables to refer to the other types as needed, and then create the interconnected objects. You could then (for example) store all your Projects in a Map with their name as the key, and the Projects as the values, and easily look up Projects. You could then ask each Project for its Device and/or Channel.

Finally -- and I hope I'm not too out of line here, but I'm starting to feel bad watching you suffer this way -- when you ask for help, try stepping back a little and asking more about the problem, rather than how to implement a solution. To the best of my recollection, you've never asked "I have a bunch of data regarding interconnected objects stored in X format, and I want to display them as Y. What would be a good approach?" I suspect the answers might surprise you.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic