What types of collections and their differences in java?

16

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?
asked by anonymous 02.10.2014 / 00:50

1 answer

20
  

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:

  • collection : That would be any data structure in which objects are stored and can be iterated;
  • Collection ( java.util.Collection ): That is an interface that has the subinterfaces Set , List and Queue ; and
  • Collections ( 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;and
  • Map:Row.

Eachinterfacehasclassesthatimplementthem.AstheyaremanyIwilljustputthecomparativetableofeachofthem,whichIthinksumsupverywellthefunctionalityofeachone:

Meaning of:

  • Ordered : When a collection is ordered, it means that it is possible to iterate the collection elements in a specific, non-random order.
  • Sorted : When a collection is sorted, it means that the order of the collection elements is determined according to some rule, known as sorting order.

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.

    
02.10.2014 / 03:22