• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Collections

 
Vijay Chandran
Ranch Hand
Posts: 186
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
Regards,
Vijay
 
krishnamoorthy kitcha
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Collection

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.

Collections

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
Collection
Set
List
Map
 
Vijay Chandran
Ranch Hand
Posts: 186
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone tell me
What is the exact difference between Collection<E> and Collection<T>?


Thank you,
Vijay
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Vijay Chandran
Ranch Hand
Posts: 186
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Rob,

Thank you very much for the reply.


Regards,
Vijay
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic