It's not a secret anymore!
The moose likes EJB and other Java EE Technologies and the fly likes MDB's General questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "MDB Watch "MDB New topic

MDB's General questions

Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Hi all,
sorry but I am going to ask some dumb newbie questions. Well a question.

I am working on a project, I have never done any EJB coding before. We have an MDB that is executing and doing a lot of work.
I am not overly concerend about this, however what I am worried about is that the methods of the objects that the MDB makes use of returns HashMaps.

To my mind if you wish to return more the one item from a method, a hashmap is the last object I would chose, the frist being a custome object that contains the multiply items to be returned, then perhaps an array, or a list, but never a hashmap.

I was just wondering what your opionions was?

Shailesh Kini
Ranch Hand

Joined: Oct 17, 2001
Posts: 153
Hello Gavin,

This is a general Java design question so I would not categorize this as "MDB's General Questions". This is a general Java question, could be applied to anything that's java.

In my opinion the use of HashMaps are justified if you are going to search for an object stored in the map using a key. You should check what is or should be done with the HashMap after it is returned. HashMap if used correctly, i.e. the object being used as a key should have properly overridden hashcode and equals method, offers very fast object retrieval using the key. If nothing of this sort is required in your case then a normal Array or ArrayList should suffice.

You could move this post to an "Advanced Java" forum here at Javaranch and find more interesting comments.

Shailesh Kini.
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
The reason I asked here, was I have never seen a hashmap returned just to get around the fact that you cant return multiple items from a method, arrays, lists, composite objects yes, hashmaps never.

Of course if you have a collection of objects to return you return those in the most suitable Collection type, but there seems to be no need for the hashmap.

Somebody muttered something about it being an Enterpise Design Pattern to pass objects between application layers *shrugs*

Jimmy Ho
Ranch Hand

Joined: Jul 31, 2007
Posts: 61
I think using a HashMap is fine, if the data returned tends to vary a lot or it's simply not worth coding a special data structure for just that class.

Having said that, don't forget about inner classes:

public class Foobar {

public class innerFoobarReplacesHashMap {
field1, field2, etc.

public innerFoobarReplaceHashMap doStuffHere () {

Another way is to put more 'controls' on the hashmap (and return a Map, not a HashMap). That is, define the exact map keys so that the caller isn't guessing or hardcoding string literals for the keys.

public class Foo {
final public static MAP_KEY_FOO = "foo";
final public static MAP_KEY_BAR = "bar";

/* Returns Map with keys defined above. MAP_KEY_FOO is a Date, etc. */
public Map doStuffHere () {
subject: MDB's General questions
jQuery in Action, 3rd edition