views:

131

answers:

9

Hello
Does anyone know of any resources or books I can read to help me understand the various Java collection classes?
For example:
When would one use a Collection<T> instead of a List<T> and when would you use a Map<T, V> instead of aList<V>, where V has a member getId as shown below, allowing you to search the list for the element matching a given key:

class V {
   T getId();
}


thanks

+4  A: 

You use a Map if you want a map-like behaviour. You use a List if you want a list-like behaviour. You use a Collection if you don't care.

Generics have nothing to do with this.

See the Collections tutorial.

It's not correct to say that you use Collection if you don't bother whether to use List or Map because Map IS-NOT-A Collection.
denis.zhdanov
I don't think the poster meant Generics (note the capital G), but instead generic, as in "not specific."
R. Bemrose
A: 

A good place to start would be the Java API. Each collection has a good description associated with it. After that, you can search for any variety of articles and books on Java Collections on Google.

Jeff Storey
+1  A: 

You can check the documentation of the java collection API.

Anyway a basic rule is : be as generic as possible for the type of your parameters. Be as generic as possible for the return type of your interfaces. Be as specific as possible for the return type of your final class.

Nicolas
+2  A: 

This book is very good and covers both the collection framework and generics.

Massimiliano Fliri
+3  A: 

You can take a look at sun tutorial. It explains everything in detail.

http://java.sun.com/docs/books/tutorial/collections/index.html (Implementation section explain the difference between them)

Mike
A: 

The decision depends on your data and your needs to use the data.

You should use a map if you have data where you can identify each element with a specific key and want to access or find it by with this key.

You take a List if you don't have a key but you're interested in the order of the elements. like a bunch of Strings you want to store in the order the user entered it.

You take a Set if you don't want to store the same element twice.

Also interesting for your decision is if you're working in am multithreaded environment. So if many threads are accessing the same list at the same tame you would rather take a Vector instead of an ArrayList.

Btw. for some collections it is usefull if your data class implements an interface like comparable or at least overrides the equals function.

here you will find more information.

raffael
You may also want a Set if you need to determine frequently whether an object is a member.
uckelman
uckelman : Set by itself is not more efficient than other interfaces to determine whether an element is a member of a set. It depends of the chosen implementation.
Nicolas
A: 

Most Java books will have a good expanation of the Collections Framework. I find that Object-Oriented-Software-Development-Using has a good chapter that expains the reasons why one Collection is selected over another.

The Head first Java also has a good intropduction but may not tackle the problem of which to select.

Vincent Ramdhanie
A: 

The answer to your question is how are you going to be using the data structure? And to get a better idea of the possibilities, it is good to look at the whole collections interfaces hierarchy. For simplicity sake, I am restricting this discussion only to the classic interfaces, and am ignoring all of the concurrent interfaces.

Collection
+- List
+- Set
   +- SortedSet

Map
+- SortedMap

So, we can see from the above, a Map and a Collection are different things.

A Collection is somewhat analogous to a bag, it contains a number of values, but makes no further guarantees about them. A list is simply an ordered set of values, where the order is defined externally, not implicitly from the values themselves. A Set on the other hand is a group of values, no two of which are the same, however they are not ordered, neither explicitly, nor implicitly. A SortedSet is a set of unique values that are implicitly sorted, that is, if you iterate over the values, then they will always be returned in the same order.

A Map is mapping from a Set of keys to values. A SortedMap is a mapping from a SortedSet of keys to values.

As to why you would want to use a Map instead of a List? This depends largely on how you need to lookup your data. If you need to do (effectively) random lookups using a key, then you should be using a set, since the implementations of that give you either O(1) or O(lgn) lookups. Searching through the list is O(n). If however, you are performing some kind of "batch" process, that is you are processing each, and every, item in the list then a list, or Set if you need the uniqueness constraint, is more appropriate.

Paul Wagland
A: 

The other answers already covered an overview of what the collections are, so I'd add one rule of thumb that applies to how you might use collections in your programming:

Be strict in what you send, but generous in what you receive

This is a little controversial (some engineers believe that you should always be as strict as possible) but it's a rule of thumb that, when applied to collections, teaches us to pick the collection that limits your users the least when taking arguments but gives as much information as possible when returning results.

In other words a method signature like:

LinkedList< A > doSomething(Collection< A > col);

Might be preferred to:

Collection< A > doSomething(LinkedList< A > list);

In version 1, your user doesn't have to massage their data to use your method. They can pass you an ArrayList< A >, LinkedHashSet< A > or a Collection< A > and you will deal with. On receiving the data from the method, they have a lot more information in what they can do with it (list specific iterators for example) than they would in option 2.

GSP
I still disagree with this rule in the case of interfaces, where you should be "generous" in sending and receiving.
Nicolas