wood burning stoves 2.0*
The moose likes Java in General and the fly likes What better: return Hashtable[] or List of Hashtables? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "What better: return Hashtable[] or List of Hashtables?" Watch "What better: return Hashtable[] or List of Hashtables?" New topic
Author

What better: return Hashtable[] or List of Hashtables?

Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
Which is better performance-wise, memory-wise and programming-wise: to return an array of Hashtables or a List/Vector of Hashtables?
Gopi Balaji
Ranch Hand

Joined: Jan 23, 2003
Posts: 84
Originally posted by Robert Paris:
Which is better performance-wise, memory-wise and programming-wise: to return an array of Hashtables or a List/Vector of Hashtables?

My humble opinion -
performance-wise - array
memory-wise - array
programming-wise - List
-GB.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I agree with Gopi, with a few more comments. For the person writing the method, ArrayList will usually be easier to program with. For a person who wants to use the method, it may be slightly clearer if the method returns an array. E.g.

tells another programmer instantly that the things in the array are Maps, and if they try to use them as some other incompatible object, the compiler will yell at them. But

does not do this. I need to include a javadoc comment here saying that the things in the List are all Maps. And there's no guarantee that other programmers will actually read the javadoc. (Of course if they don't, it's their own fault when ClassCastException is thrown.) So it may be useful to return an array here - even if most of the work inside the method was done with an ArrayList. You can just convert at the end of the method:

Of course there's a slight performance hit to copy the List contents into the array, but in most cases it's not a big deal, and the extra ease-of-use may be worth it. May have other benefits too, like if you don't want other classes to be able to access your private List - you can just give them a copy. Changes to the array don't affect the List. (Though changes to the objects in the array certainly affect the objects in the List; they're the same objects after all.)
I was more likely to return an array than a List back when collections were new to the language, and I couldn't be sure that other programmers would know how to use them effectively. Nowadays I'm more likely to say to heck with them if they don't understand it; they need to learn collections. But it depends on my mood.
[ January 31, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What better: return Hashtable[] or List of Hashtables?