File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes java.util Decisions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "java.util Decisions" Watch "java.util Decisions" New topic

java.util Decisions

Matt DeLacey
Ranch Hand

Joined: Oct 12, 2000
Posts: 318
Hey All:
Okay, I'm going over everything and I'm having a tough time justifying all of my decisions as far as which type of collection I used under different circumstances. There are some few times when I used a certain one for a certain purpose, but more often than not there was not a compelling reason to use one over the other, so I just used Vector generally (because I'm most familiar with it). There are several places where it seems you could use
most any collection. For instance, my lockHolder. I made it a Vector, but there's not a particularly compelling reason why. I mean, it works! It seems like it would have worked equally well, with most any other collection. Any profound insights on this one?
With Respect,
Carol Enderlin
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Java in a Nutshell: "Vector is quite similar to the ArrayList class, except that the methods of Vector are synchronized, which makes them thread-safe but increases the overhead of calling them. If you need thread safety or need to be compatible with Java 1.0 or Java 1.1 use Vector, otherwise use ArrayList."
It says the same for Hashtable vs. HashMap.
Doug Melzer
Ranch Hand

Joined: Mar 23, 2000
Posts: 51
Another option would be to use one of the Collections.synchronizedXxx() methods to synchronize your collection.
Doug Melzer
Ranch Hand

Joined: Mar 23, 2000
Posts: 51
If you do use the Collections framework, then deciding upon required behaviors will help you to decide whether to use a Set, List, Map, etc.
For example, are duplicate entries allowed, do you want to look up an object based upon some sort of key (perhaps a record #), etc.
R Bischof
Ranch Hand

Joined: Feb 13, 2001
Posts: 48
Regarding the synchronization: Keep in mind that you only need synchronized collections if concurrent modification can occur to the collection. If you do your synchronization within your methods and no concurrent modifications can happen, use the unsynch'ed ones.

Rainer<br />SCJP, SCJD, SCWCD
I agree. Here's the link:
subject: java.util Decisions
It's not a secret anymore!