• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MDB's General questions

 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

thanks
G
 
Shailesh Kini
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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*

Thanks
G
 
Jimmy Ho
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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";
etc...

/* Returns Map with keys defined above. MAP_KEY_FOO is a Date, etc. */
public Map doStuffHere () {
...
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic