• Post Reply Bookmark Topic Watch Topic
  • New Topic

Create Standin Collection  RSS feed

 
Brian Smither
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am wanting to create an array of data to be used until the database people get their part of the project put together.

In PHP, this would be so easy.

So far, I have made a best guess that I want a HashMap, but getting the syntax of all the little parts correct is proving daunting.

I have an indexed set of Hubs. I have an indexed set of Spokes. I have an indexed set of Titles.

For each Hub, I want to associate a selected group of Spokes, and for each one of those Spokes, I want to associate a selected group of Titles.

Therefore, simplified:
root = array('H1'=>array('S1'=>array('T1', ...), ...), ...)

This temporary structure will be "static", but the code should try to match what recordsets from a query would look like.

Please know that I have looked at books, videos, other sources, and have been shown much except what I actually need to figure this out.
 
Giovanni Montano
Ranch Hand
Posts: 434
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
welcome to the ranch,

I am a junior guy in this saloon, the one that get always shooted as soon as the bad bandits slam the swinging doors

but can tell you two things
that you could maybe like hasmap with varargs, and a customized class with observers to pass into the hashmap
and
that maybe is a question that should be published on the general java forum, being this quite not really a beginner question.

Possibly I am wrong on both of them, but never know:)
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain more about what you want; you need to decide what you are going to do before you try to work out how to do it.
 
Brian Smither
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only thing I understood is the swinging doors.

I know what I am going to do with the array. I will have FreeMarker split it up and populate a template.

The array has to arrive as one complete package.

What I want? Many things. But basically some guidance.

We can start with: is a 'hashmap' the proper structure to have an array that contain arrays that contain arrays that contain arrays?
 
Paul Clapham
Sheriff
Posts: 22838
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Smither wrote:We can start with: is a 'hashmap' the proper structure to have an array that contain arrays that contain arrays that contain arrays?


No, it isn't. A map is the proper structure if you have a set of keys with values attached to them, and you're going to access it via the keys.

But then arrays containing arrays containing arrays isn't the proper structure for what you described in your original post. It described some maps, two of them in fact. You've got one map whose keys are Hub objects and whose values are sets of Spokes. In Java code that would be a Map<Hub, Set<Spoke>>. And you also have a Map<Spoke, Set<Title>>.

There are still questions to be asked about that design, because what you posted wasn't complete, but it's probably a good start.

I dunno, I consider that "so easy". It comes straight out of your description. But if you asked me to do it in PHP I would have a lot of trouble doing it.
 
Paul Clapham
Sheriff
Posts: 22838
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that you're using the term "array" to mean some generalized bunch of data. Whereas an array in Java is a fixed-length list of items which are numbered starting at zero.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your question is much like "How do I cut this tree down with a chisel?" My answer would be the same to that question as with yours: If it were me, I wouldn't.

Instead, I would use a DAO interface then use an in-memory database implementation while the real database is unavailable. When the real database is ready, I would unplug the in-memory DAO implementation and plug in the real database implementation. I might even keep the in-memory database implementation for unit testing purposes if I find it useful enough.
 
Giovanni Montano
Ranch Hand
Posts: 434
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Smither wrote:(...)
The array has to arrive as one complete package.

What I want? Many things. But basically some guidance.

We can start with: is a 'hashmap' the proper structure to have an array that contain arrays that contain arrays that contain arrays?


The cases are two:
you want array that contain a fixed number of arrays

you want array that contain a variable number of arrays

First case
, fixed


bring to a two dimensional array




Second case
, variable, you do not know how many arrays will have
you will pass a kind of HashMap Object with varargs object inside an ArrayList, so that you will have also a Key, a reference efficient to find them after for your peruse




 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giovanni Montano wrote:
you want array that contain a fixed number of arrays
...
you want array that contain a variable number of arrays
...

This illustrates the kind of maze you can easily get lost in when you try to solve a problem using the wrong tools. Instead of giving you a simple straightforward way to simulate the functionality of the real database, you can quickly get lost in the jungle of Kludge, trying to cut through the undergrowth of details using only a dinner knife. Again, don't let your initial "solution" idea lead you there. I would advise you to abandon that plan and use a DAO interface and an in-memory database implementation instead.
 
Giovanni Montano
Ranch Hand
Posts: 434
7
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@JUnilu
From an operative point of view it is clear that efficiency should be the polar star to address,
but in this educational context I still have personally some weak reservations especially because the OP asked clearly if an hashmap was the proper structure to have an array that contains arrays ad libitum ,

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Giovanni Montano wrote:@JUnilu
From an operative point of view it is clear that efficiency should be the polar star to address,
but in this educational context I still have personally some weak reservations especially because the OP asked clearly if an hashmap was the proper structure to have an array that contains arrays ad libitum ,

I get where you're coming from but since OP said something about "the database people get their part of the project put together", I would put the OP's question more in the category of "I want to free myself of these handcuffs, is it best to cut my hand off with a saw or with an ax?" My answer would be, "Don't cut off your hand!" I just see your previous response being like "Well, here's how you would do it with a saw, and here's how you would do it with an axe." I'd rather not give OP any ideas about going down either route.
 
Giovanni Montano
Ranch Hand
Posts: 434
7
Android Open BSD Slackware
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
I get where you're coming from but since OP said something about "the database people get their part of the project put together", I would put the OP's question more in the category of "I want to free myself of these handcuffs, is it best to cut my hand off with a saw or with an ax?" My answer would be, "Don't cut off your hand!" I just see your previous response being like "Well, here's how you would do it with a saw, and here's how you would do it with an axe." I'd rather not give OP any ideas about going down either route.

Sure, your point is absolutely understandable indeed, and Tarantino would love your metaphor
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Smither wrote:This temporary structure will be "static", but the code should try to match what recordsets from a query would look like.

And THAT is likely to be the tough part, because what an SQL query returns to Java is a ResultSet, which is a an interface with no fewer than 194 methods!

Now the chances are that you'll only have to implement half a dozen of them or so, but just working out which ones they are and creating "default" methods for the rest is going to take you a while.

Personally, I'd go the DAO route myself, or maybe just write a throwaway API to get the information you need, and maybe load it from flat files.

But, as others have already said, you really need to understand the requirements (the 'what') in detail before you write one line of Java code.

Winston
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Personally, I'd go the DAO route myself, or maybe just write a throwaway API to get the information you need, and maybe load it from flat files.

+1 for flat files if it's just a throwaway anyway. Probably easier than in memory db
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Smither wrote:This temporary structure will be "static"...

I hope you don't mean static - as in the Java keyword - because that would be a BIG mistake.

One of the things I notice (and I'm certainly guilty of) is that we sometimes talk about abstract notions like 'what' without explaining how you go about it.

Well, the first thing to do is StopCoding, because code is unlikely to provide a solution until you've designed it.

Second: Rather than looking at what type of collection you're going to use (HashSet, TreeMap etc), think about what this information is used for. You have Hubs, Spokes and Titles, but what do they represent? The first two sound like components of a bicycle wheel to me, but I have no idea where a Title would fit into that.

Better yet, come up with the names of business processes that use this data, and try to map those out: What do they need as input? Who runs them and when? What result or results do they produce? From that, you should be able to get a set of Java method stubs (or DAO/"business" objects) that fulfil those needs, and then you can set about working out how you're going to implement them while you have no database.

The idea is that if you get those methods/objects right, all you should need to do when your db team does get their act together is "rip out the plumbing" and replace it with database queries.
But you aren't going to be able to do it while you're thinking about HashMaps and static and arrays.

HIH

Winston
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, once you have your DAO interface (not concrete class) and a dummy class that can simply start life doing nothing bar returning empty arrays or empty model classes if needed, only add in things to this dummy as needed.

There is no point at all in spending a large amount of time trying to think about this if by the time you've implemented your stand in version the db team have completed their work.

You should be able to exercise your main code enough through JUnit mocking in tests and a very simple set of dummy DAOs for a temporary integration run.



And even that's probably overkill as it should take you enough time to put together your tests, so you'd be mocking anyway.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!