• Post Reply Bookmark Topic Watch Topic
  • New Topic

help me please to choose the best

 
Nada Mohammed
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
we now that we have many types of collections like vector ,linked list,stack ,...etc and choose any one of them depend on the problem we want to solve so please help me to choose the best one of those collections depend on my problem and depend on what i need :
* size of the collection decide at run time so no fixed size for this collection.
* i can remove|return elemnt at postion x
* operations of this collection are synchronized because i have threads in the program.
***** provide good performance for the program so i want to care about memory and speed .

Thank you
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vector is reportedly the slowest in the bunch.
Personally, I like ArrayLists and HashTables, but that's just me. Use a representation that fits naturally with your data, it makes your logic clearer (a tree maybe?)
 
Jack Shirazi
Author
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of the available collections from the SDK, Vector is the fastest collection that is synchronized, indexable, and provides random-access, which are the criteria listed (all SDK collections are non-fixed size collections). Note that Vector is significantly faster than a synchronized-wrapped ArrayList.
You can make Vector slightly faster by subclassing it and overriding accessors to be quicker by missing some tests, if that is appropriate for your data. But I often find that people who want faster access to collections really want to <em>query</em> the collection faster, and that is best done by implementing the query inside the collection class - see my article on Optimizing a query on a collection.
There is also some more discussion in the 'Vector vs. the new collections' thread in this forum.
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
* i can remove|return elemnt at postion x

In Bruce Eckel�s book: Thinking in Java, in Chapter 9, (you can get an html version from: www.bruceeckel.com) Bruce made a comparison between Collections, group by their implementation interfaces.
He made code to compare the List implementations and took the result that LinkedList is faster to remove data than Vector but slower to get data than it.
You may have to analize the code and the results to choose what action you want to prioritize
 
Derek True
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marcela,
You might want to edit that link and take of the ")". It dosen't work with the extrr symbol.
Thank you for pointing out the free HTML version.

Derek
 
Jack Shirazi
Author
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a little more complicated. LinkedList performance versus ArrayList performance for element insertion & removal is very dependent on the size of the collection and the location of the element being inserted/removed.
LinkedList has the node traversal overhead for random access, which means that the further away the element is from either end of the list, the longer the traversal takes. The insertion/removal is fast <em>once</em> the required node has been reached.
ArrayList has no node traversal overhead, but has an array copying overhead which depends on how far away the element is from the end of the list.
In addition, LinkedList must be used as a synchronized wrapped array for synchronization purposes, which imposes significant overheads, whereas the Vector implementation avoids having to use a wrapper, while still provding synchronization.
I find that for the typical small array, Vector is faster. Though you can almost certainly implement a different data structure which is more appropriate to the particular data and task, which will be faster than the SDK provided collections.
I also find that most of the time, the speed differences do not matter at all. I frequently get into this kind of discussion, only to find that no speed problem related to the collection objects has been identfied. It is important to profile the application to find exactly where your problems are before you start 'fixing' performance problems.
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jack!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!