Lesson: Collections

Overview: 
Explore basic variable collections in Java.
Objectives: 

Understand basic collections, what they are, how they work and how to use them.

Content: 

A Collection is an object that stores lists of other objects allowing the group of stored objects to be manipulated in many powerful ways. A Collection may sound like an array or ArrayList and while a Collection is quite different than an array, ArrayList is in fact an implementation of the Collection concept. Java has a large number of specific implementations of the Collection concept you can use. Here are the most commonly used types of Collection:

  • set - A list of objects with no duplicates.
  • list - A list of objects duplicates allowed.
  • map - A list of objects with key values (no duplicates).
  • queue - A list of objects with features that support sequential processing of the elements.

Within each type, there are a number of actual implementations you can use. Each implementation has specific features or performance aspects that you consider when choosing an implementation to use for your programs. Here is an example:

This prints out:
second string
third string
first string
The example creates a Set of String objects stored in a HashSet implementation. A HashSet is a high performance Set but does not guarantee any particular order when retrieving elements from the Set. Note that since  this is a Set, the second attempt to add "first string" to the collection is ignored since there is already a duplicate element in the Set.
 
Collections have methods for adding, deleting and retrieving elements and much more. One useful feature of Collections is the Iterator. An Iterator is an object you can retrieve from the Collection that provides methods for navigating and modifying the list. Using an Iterator you can move forward and backward on a Collection using next and previous. Note the for keyword in the example. The Java for keyword understands Iterators and supports using them to access Collection elements.  Here we we are saying do a for loop for all of the elements in mySet, type the elements as String and give me access to each element through the variable s.
 
Here is an example of a List type Collection:

This prints out:
new first string                                                                                                                                       
first string                                                                                                                                           
second string                                                                                                                                          
third string
second string
--------------------
second string
third string
second string
first string
new first string

Here we create a List Collection using the ArrayList implementation and add some elements. Note we added an element using an index (position) and it inserted the element at that location, moving all subsequent elements up. The ArrayList allows us to add a duplicate element. Finally we use the Iterator type ListIterator (a specialized Iterator for List collections) to manually list the elements in forward order and then reverse order.

Due to the many types of Collections and the many implementations of the types of Collections, Collections can seem daunting and overly complex. But Collections are very powerful tools for manipulating data sets and most cases can be handled by the Set or List Collection type.

Here is a detailed discussion of Collections starting with an introduction and moving through the specific implementations of the various Collection types.

Here is the example code in CodingGround.

 

Lesson navigation: