aspose file tools*
The moose likes Java in General and the fly likes Efficient Implmentaion of List and Map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Efficient Implmentaion of List and Map" Watch "Efficient Implmentaion of List and Map" New topic
Author

Efficient Implmentaion of List and Map

Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
Hello All,
I would like to know the efficient implmentaion of List and Map for jdk 1.3.1.
Thanks,
Santhosh.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well, usually I use ArrayList and HashMap. Sometimes LinkedList or TreeMap (depending on my needs). If you're curious you can find the source code for all these in src.jar. Is that what you meant?


"I'm not back." - Bill Harding, Twister
Snigdha Solanki
Ranch Hand

Joined: Sep 07, 2000
Posts: 128
The implementations of List in JDK1.3.1 are Vector,ArrayList,LinkedList and implementations of Map are HashMap, Hashtable, RenderingHints, WeakHashMap, Attributes. Except Vector and Hashtable rest all are unsynchronized and hence offer better performance.


Snigdha<br />Sun Certified Programmer for the Java™ 2 Platform
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
Thank you Jim and Solanki for the reply.
I have read somewhere in the net that, if you implement the list and map yourself, it will offer better performance than ArrlyList or HashMap... so I was looking any info regd this? any URL where I can get this or any guidelines?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Not sure what you mean by implement it your self...write your own? In most cases, the performance gains from writing your own, if any, will not be a major factor. The current implementations of HashMap, ArrayList, etc. Should suffice.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I think that the idea of writing your own implementations comes from two things. One is that the initial implementations of come Collections weren't as good as they could be. But they've improved in subsequent releases. The other is that for collections of primitive values, it may be desirable to eliminate the overhead of creating object wrappers for each primitive. The latter also can apply to collections of specific Object subclasses - if you know that all theo objects in your List are really Strings, you can eliminate some casting by creating a list that only works with Strings, and stores them in a String[] array internally rather than an Object[] array. (Note that technically you're no longer implementing a Collection if it doesn't accept Objects.) There are probably some other situations where you might want to get specific performance characteristics which are different from the generic Collection implementations.
However, it is almost certainly a bad idea to try to do this unless and until you determine that the current performance is unacceptable, and that the bottleneck is in fact in an area which you can fix with a different implementation. Optimizing a collection is not as easy as it may seem, and the current Collection implementations are really pretty good. It will be very easy to create something slower that you'd get with these implementations, and even when you can improve on it, the difference will often be negligible.
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
After doing some benchmarking, here is what I have decided.
Use ArrayList and ArrayMap. Our requirement does not have much of deletions. So I think these two would be suffice. But I have given a wrapper for each type of object I want to keep. for ex., if want to keep list of users, I have made UserList which extends from ArrayList. So that in future if I want, make it as array users (User[]) to give better performance.
Thanks you very much for the input. If you think of anything fishy with this approach, pl reply.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Originally posted by Santhosh Kumar:
...But I have given a wrapper for each type of object I want to keep. for ex., if want to keep list of users, I have made UserList which extends from ArrayList. So that in future if I want, make it as array users (User[]) to give better performance.
Thanks you very much for the input. If you think of anything fishy with this approach, pl reply.

Indeed. By extending the class you are still exposing the original classes interface. Which means people can still insert ANY Object or retrieve an Object from the superclass interface.
Might be a better choice to simply aggregate the Object. Of course in this case you cant implement the Collection interface and use any of the helpers in the Collections class.

in this case nobody can call get directory on the superclass and retrieve and object or class Object, nor can they put an object of class Object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Efficient Implmentaion of List and Map