aspose file tools*
The moose likes Java in General and the fly likes Is a Map data type that holds three objects possible? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is a Map data type that holds three objects possible?" Watch "Is a Map data type that holds three objects possible?" New topic
Author

Is a Map data type that holds three objects possible?

Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

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?


True wisdom is in knowing you know nothing - Socrates
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

hello,

it is possible

Please check org.apache.commons.collections.map.MultiKeyMap from apache collections


Thanks, Ganesh Gowtham
http://ganesh.gowtham.googlepages.com
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3832

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

Hi

see whether it will suit your requirement


Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14164
    
  19

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

Hi Jesper ,

Your code seems to OK for creating Maps of Maps with top level Map's Key as String .
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

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
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

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
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

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
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

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

Joined: Mar 24, 2008
Posts: 3832

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
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

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

Joined: Mar 30, 2005
Posts: 225

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.
 
 
subject: Is a Map data type that holds three objects possible?