This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Regarding Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regarding Collections" Watch "Regarding Collections" New topic

Regarding Collections

Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 186

Dear friends,

What is the exact difference between Collection<E> and Collection<T>?
What is the exact difference between java.util.Collection and java.util.Collections?

Couldn't understand API explanation at my level.

Your kindly help needed.

Thanks in advance!!
krishnamoorthy kitcha
Ranch Hand

Joined: Feb 04, 2006
Posts: 96


The root interface in the collection hierarchy. A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered. The SDK does not provide any direct implementations of this interface: it provides implementations of more specific subinterfaces like Set and List. This interface is typically used to pass collections around and manipulate them where maximum generality is desired.

Bags or multisets (unordered collections that may contain duplicate elements) should implement this interface directly.

All general-purpose Collection implementation classes (which typically implement Collection indirectly through one of its subinterfaces) should provide two "standard" constructors: a void (no arguments) constructor, which creates an empty collection, and a constructor with a single argument of type Collection, which creates a new collection with the same elements as its argument. In effect, the latter constructor allows the user to copy any collection, producing an equivalent collection of the desired implementation type. There is no way to enforce this convention (as interfaces cannot contain constructors) but all of the general-purpose Collection implementations in the SDK comply.


public class Collections
extends Object

This class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection, and a few other odds and ends.

The documentation for the polymorphic algorithms contained in this class generally includes a brief description of the implementation. Such descriptions should be regarded as implementation notes, rather than parts of the specification. Implementors should feel free to substitute other algorithms, so long as the specification itself is adhered to. (For example, the algorithm used by sort does not have to be a mergesort, but it does have to be stable.)

See Also
Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 186

Could anyone tell me
What is the exact difference between Collection<E> and Collection<T>?

Thank you,
Rob Spoor

Joined: Oct 27, 2005
Posts: 20132

There is no difference. The <E> or <T> is the generic type of the collection, which is limiting the objects that can be added.

However, when declaring a class, it doesn't matter how you call it. There is no difference in the following pieces of code:

It's just a name (mostly a single letter) that the programmer thought would be readable. Mostly it is "T" for "type", "E" for "element", "K" for "key" and "V" for "value" (the last two mostly for maps).

As for using, this is when you "replace" the E/T with any class you want.
Collection<Integer> will be a collection that only accepts integers; Collection<String> will only accept strings, etc.

For instance, using the above example, you use SomeInterface<Integer>, the compiler treats the class as the following:

How To Ask Questions How To Answer Questions
Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 186

Dear Rob,

Thank you very much for the reply.

I agree. Here's the link:
subject: Regarding Collections
It's not a secret anymore!