There are many types of collections in java and I have trouble knowing the following issues below:
- What are the differences between them?
- What situations and / or cases should I use in each of the different collections?
There are many types of collections in java and I have trouble knowing the following issues below:
What are the differences between them?
First of all it is important to understand that Collection is a term that may be ambiguous, as there are:
java.util.Collection
): That is an interface that has the subinterfaces Set
, List
and Queue
; and java.util.Collections
): That is a class with some static methods to be used with subclasses of Collection
. While Set
, List
, and Queue
are collections (or collection , with lowercase "c") too, Collection
's, %. The same does not happen with java.util.Collection
, which although it can be considered a collection, it does not implement the Map
interface.
See below:
Themaindifferencesamongtheabovementionedinterfacesare:
java.util.Collection
:Listofthings;List
:Listofthingswithoutrepetition;Set
:Listwithkey-value,wherethekeymustbeunique;andMap
:Row.Eachinterfacehasclassesthatimplementthem.AstheyaremanyIwilljustputthecomparativetableofeachofthem,whichIthinksumsupverywellthefunctionalityofeachone:
Meaning of:
Source: SCJP Sun Certified Programmer for Java 6
What situations or cases should I use in each of the different collections?
The situation depends on your need, looking at the differences listed above is very clear when one should choose one or another collection.