• Post Reply Bookmark Topic Watch Topic
  • New Topic

ROT's for using collections  RSS feed

 
Mohit Sinha
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I would like to know if there are any Rule Of Thumbs when it comes to using the Collections API like

When it would be advantage to ArrayList over Vector

When HashMap scored over Hashtable

Say if i have only additions & views on my collection object then which 1 to go for

Say if I have continous updations & deletions from the collection object
then which 1 to use


I think we should have a separate forum for Collections (like Threads, IO etc have)

Rgrds
Mohit
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The examples you supply are easy:
  • When to use Vectors? Never
  • When to use Hashtables? Again, never.

  • Both are legacy collections.

    General guidelines for what to use when are given in the Collections Framework docs that come with the API. There is also a tutorial which covers this at Sun.
     
    Jessica Sant
    Sheriff
    Posts: 4313
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thomas Paul wrote a great series of articles about collections, check them out:

    Storing Objects in Java Part 4 - The Collections class
    Storing Objects in Java Part 3 - The Map Interface
    Collections in Java Part 2 - The Set Interface
    Collections in Java Part 1 - The List Interface

    Also, I'm moving this to the intermediate forum where I think its more
    appropriate. So please post your replies there.


    Thanks!
     
    Layne Lund
    Ranch Hand
    Posts: 3061
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Paul Sturrock:
    The examples you supply are easy:
  • When to use Vectors? Never
  • When to use Hashtables? Again, never.

  • Both are legacy collections.

    General guidelines for what to use when are given in the Collections Framework docs that come with the API. There is also a tutorial which covers this at Sun.
    I would disagree with using the word "never". In programming, there are rarely any absolutes. So imho (and I think other Java programmers would support this), you should prefer ArrayList over Vector. However, there are places where you should use Vector instead. The primary difference between Vector and ArrayList is that Vector is thread-safe, so if you are writing software where this is an issue, you should consider using Vector. The thread-safety carries an overhead, though, so that is why you should prefer ArrayList, especially when you are writing a program that will not be threaded.

    I'm not sure about Hashtable. I am almost willing to agree that you should never use them. However, I'm reluctant to be so absolute about it. I'm fairly certain that HashMap will do the same thing. Of course, the Properties class extends Hashtable and is very useful in loading configuration information from a property file. This is probably the only use I see of the Hashtable class.

    As always, it takes experience and reading some good tutorials to learn about these details. I'm sure the links given above will be a good jumping off point for you.

    Keep Coding!

    Layne
    [ December 01, 2004: Message edited by: Layne Lund ]
     
    Paul Sturrock
    Bartender
    Posts: 10336
    Eclipse IDE Hibernate Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    The primary difference between Vector and ArrayList is that Vector is thread-safe, so if you are writing software where this is an issue, you should consider using Vector


    Well, this:

    Sorts that out. I'd argue (and I suppose by doing that I'm proving your point about no absolutes) the need for a thread safe collection is a much less common requirement than inappropriate use of such a collection by using a Vector where an ArrayList is a better fit. So that's why I make my somewhat glib generalization to what is I'll admit a little more complex.

    Another reason I'd avoid Hashtables is that a null key == a Exception. Its not the case in HashMaps, so they are less open to Exceptions/require less code to use.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!