Collections/GenericsHomepage « Learn Java5 « Collections/Generics
In our seventh section on learning Java5 we look at collections and investigate generics. We start by defining what a collection is and look at some diagrams of the collection types to get a feel for the various
structures of the classes and interfaces involved. After this we make an in-depth investigation of generics and unravel the strange syntax we get with it. Following on from this we examine the various hierarchies
within The Collections Framework by taking a look at some concrete implementations of Sets, Lists, Maps and Queues. We than look at the java.util.Arrays
and java.util.Collections
classes that contain a lot of static
utility methods we can use with our collections. We finish the section by finding out how to sort our collections using the Comparable
and Comparator
interfaces.
Collection Overview
In our first lesson we give an overview of collections which can be thought of as generic types that allows us to store and iterate over data. The suite of programs we use for collections is commonly known as The Collections Framework and we will break the framework up into into various hierarchies using diagrams for ease of understanding. We also explain the terminology used when dealing with The Collections Framework.
Generics
Now we have looked at an overview of the The Collections Framework and before we delve into the various collections within the framework we need to talk about generics. In this lesson we unravel the terminology behind generics and unravel the strange syntax we get with it. Generics were added to Java in 1.5 and are the biggest of all the changes made to the Java language in the 1.5 release. Although generics are not just for collection types, the majority of usage is within collections and so here is a good place to talk about generics.
Sets
In the first of three lessons on the different collection types within the Collection hierarchy we look at Sets which are probably the easiest collection type to understand, and as such, a good starting point. A Set doesn't allow duplicates so every entry within a Set must be unique and as the name implies the interface models the mathematical set abstraction.
Lists
In the second of three lessons on the different collection types within the Collection hierarchy we look at three of the concrete implementations of the List<E>
interface. Lists are ordered
collections that use an index for the ordering of the elements within the collection.
Queues
In our final lesson on the different collection types within the Collection hierarchy we look at the PriorityQueue<E>
concrete implementation of the Queue<E> interface.A Queue is an
ordered list of actions that are to be processed and as such does not allow null
elements. Typically, a Queue would be in FIFO (first-in, first-out) order although other orders such as
LIFO (last-in, first-out), natural-ordering and custom comparator order are also possible.
Maps
In this lesson we look at the Map hierarchy and examine four of the concrete implementations of the Map<K,V>
interface. Maps have unique identifiers as their keys which are mapped to values,
similar to key/value pairs in other languages.
Utilities
In this lesson we look at the Utilities hierarchy and examine the java.util.Arrays
and java.util.Collections
classes that contains a lot of static
utility methods we can use with our arrays and collections.
Sorting Collections
In our final look at collection we look at sorting our collections using the Comparable
and Comparator
interfaces. We already looked at examples of sorted collections when we looked
at the TreeSet and TreeMap classes earlier in the section. In this lesson we take a much closer look using custom classes and comparators.