d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Multi-dimensional Maps Vs Inner Class > Question
12Next
Add Reply New Topic New Poll
Member
Posts: 1,849
Joined: May 31 2008
Gold: 2,571.50
Jun 24 2016 11:23pm
So the current project I'm working on I'm using a multi-dimensional map so something like: Map<Id, Map<Id, Map<Id,Object>>>

I'm wondering at what point or in what cases should I consider using an inner class or another object to store my information.

Is there a best practice?

Also does using inner class "hog" / slow down what I'm trying to accomplish?

This question seems "opinionated"... but still wondering.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jun 25 2016 12:30am
I think you should reevaluate your need for a Map of Map of Map of ... and instead look at a way to encapsulate your data within your own data type. Use object orientation to your advantage.
Member
Posts: 1,849
Joined: May 31 2008
Gold: 2,571.50
Jun 25 2016 01:32am
Quote (Minkomonster @ Jun 25 2016 01:30am)
I think you should reevaluate your need for a Map of Map of Map of ... and instead look at a way to encapsulate your data within your own data type. Use object orientation to your advantage.


Alrighty.

Currently my objects are:
Code
Event --> Location --> Points of Interest.


Basically I query for the event I'm on and all the children locations and all the locations points of interest records. On my page I just loop through my map that houses a list of Points of Interest. The reason I decided to choose this way is so that while I'm looping through the map on the page I have access to the Id of the current location / event of that point of interest. This is important because I could have a map of <Id, Events> // <Id, Location> and be able to access that map as I loop through my other one.

As I'm typing this... I think it would of been possible to query a List<Points of Interest> and just store the parent ids...


Is creating an inner class a form of encapsulation? I remember making 'Java Beans?' or something in school, but haven't outside of it. I will look more into encapsulation this weekend and might have some more questions.

as always thank you.

This post was edited by Noobtard on Jun 25 2016 01:34am
Member
Posts: 1,039
Joined: Jul 8 2008
Gold: 1,939.50
Jun 25 2016 08:55am
Quote (Noobtard @ Jun 25 2016 01:23am)
Map<Id, Map<Id, Map<Id,Object>>>


No.

It seems to me you should have a database and a few queries instead of some overly complex java program, but I don't know what the point of your project is. A travel app that shows events in locations? Needs a database.

Edit: You could have a few lists (one for each object type) then have a few methods that you use to relate them. This would take away the overly complex map. It may take a little while to make the code function in the new manner, but using a map like that isn't maintainable.

This post was edited by waraholic on Jun 25 2016 09:00am
Member
Posts: 23,444
Joined: Jun 30 2009
Gold: 167.53
Jun 25 2016 09:00am
So I try to avoid inner classes but they are okay to use as long as they follow RDD(responsibility driven development). You can use inner classes as long as it is within the scope of the responsibility of the main outter class.


Also, can you post the requirements of your assignment so we can give you alternative solutions rather than you using hash maps within hash maps (which I highly recommend you avoid).

And minko's last sentence is very important so take his advice.


This post was edited by umeshieee on Jun 25 2016 09:19am
Member
Posts: 23,444
Joined: Jun 30 2009
Gold: 167.53
Jun 25 2016 06:38pm
Quote (Noobtard @ Jun 25 2016 03:32am)
Alrighty.

Currently my objects are:
Code
Event --> Location --> Points of Interest.


Basically I query for the event I'm on and all the children locations and all the locations points of interest records. On my page I just loop through my map that houses a list of Points of Interest. The reason I decided to choose this way is so that while I'm looping through the map on the page I have access to the Id of the current location / event of that point of interest. This is important because I could have a map of <Id, Events> // <Id, Location> and be able to access that map as I loop through my other one.

As I'm typing this... I think it would of been possible to query a List<Points of Interest> and just store the parent ids...


Is creating an inner class a form of encapsulation? I remember making 'Java Beans?' or something in school, but haven't outside of it. I will look more into encapsulation this weekend and might have some more questions.

as always thank you.


Also, I don't know how your java object is structured but you can easily avoid the maps with in maps if you put the Points of Interest ArrayList within the Location object and put the Location object within the Event object. Just assign a unique Id key to the Event object and do getters and setters for both the Location and Event object. Now you can avoid the maps within maps. You will have you new generic for the hashmap as this:

private Map<String, Event> eventMap = new HashMap<String, Event>();

The string will be the event.getId() which will return your unique id key and you have now successfully stored all your information in the Event object.
Use proper OOP and everything will be made simple for you.

Last tip: don't instantiate the map in the class level. Instantiate it within a static block or something of that sort.

This post was edited by umeshieee on Jun 25 2016 06:39pm
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Jun 27 2016 06:41pm
it's java right?
what's wrong with class inheritance ?

e.
o ya last guy covered it nvm
i only see nested classes when they're used just to perform some task

This post was edited by Ideophobe on Jun 27 2016 06:54pm
Member
Posts: 20,253
Joined: Apr 30 2008
Gold: 5,267.97
Jun 28 2016 03:56am
Quote
Map<Id, Map<Id, Map<Id,Object>>>


My eyes are bleeding.

What happens if you suddenly decide that the second level of Maps could actually use another property additionally to the Id and the Map?

Please please please please do not do this. Just make classes; it takes no effort and makes your code 9001 times cleaner.
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Jun 28 2016 07:11am
You should be using join columns/tables in your database if you're using a database.

But even if you're just parsing xml or json, you should still be using classes. eg event has location, location has pointsOfInterest
Member
Posts: 7,324
Joined: Dec 22 2002
Gold: 1,261.00
Jun 30 2016 09:55am
Yeah, what the other guys are saying. It should be like this:

Code
class Event {

Map <Id, Location> locations; //or a list, if you only ever have a few. Also assumes Id is a valid type
//some other properties

}

class Location {

Map <Id, PointOfInterest> pointsOfInterest;
//some other properties. Probably geo coordinates or an address

}

class PointOfInterest {

//some properties

}


This will make your code MUCH easier to write, debug, and maintain. You'll thank yourself when you come back to it in a week because you decided to add or change something.
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll