Thursday, August 16, 2012

Generics - Part 2


One of the hardest sections is Generics that was introduced by Sun in Java 5.0, this section will try to summarize the most relevant topics as well as real examples to help you memorize when/where and why use a specific collection for a given scenario.

Let's start with the key interfaces and classes.

Intefaces



Classes



Noticed that almost all the interfaces and classes directly or not extend java.util.Collection, only Map and interfaces and classes under it don't extend java.util.Collection. Take a look at the structure of Collection below:



Watch out - Collections is a utility class which provides static methods and Collection is an interface.

Overview of each member of collections.

¦ Lists - Lists of things (classes that implement List).
¦ Sets  - Unique things (classes that implement Set).
¦ Maps  - Things with a unique ID (classes that implement Map).
¦Queues - Things arranged by the order in which they are to be processed.

Sorted,Unsorted,Ordered and Unordered that's a tricky part of collections

Ordered --> When a collection is ordered, it means you can iterate through the
collection in a specific (not-random) order, find below some examples of ordered and unordered collection


As you can see the class LinkedHashSet keeps the order based on insertion where the last inserted is the last element in a collection, ArrayList keeps the order by using index position and finally Hashtable does not keep the order of its elements.

Sorted --> A sorted collection means that the order in the collection is determined
according to some rule or rules, to determine this rule we need to implement Comparator or Comparable, more about it is comming up.

Let's see an overview of the interfaces and those concrete implementations.

List Interface:
A List cares about the index, it's a ordered collection.

ArrayList:
It's a ordered collection by index but not sorted, it's a good idea choose this collection when you need fast iteration, however is not suitable if your intention is insertion and deletion.

Vector: 
Basically the same as an ArrayList, but its methods are synchronized.

LinkedList: 
It's a ordered collection by index, also it has methods that allow insertion and deletion from the beginning or end.
You'll be in a good shape using this collection to massive insertion and deletion but be careful when you need iteration.
Finally since Java 5.0 it implements java.util.Queue so it has the basic methods peek(), poll(), and offer().

Set Interface:
A Set cares about uniqueness, it doesn't allow duplicates, to do this the method equals() ensures it.

HashSet:
A HashSet is an unsorted, unordered Set, ensure that you did a good implementation for hashCode() method because it's determines a performance of this collection, use it when you don't want duplicates and you don't care about order when you iterate through it.

LinkedHashSet: 
Different from HashSet this collection is ordered, so use it when you care about order when you iterate through it, an order is determined by insertion of elements.

TreeSet:
It's a sorted collection, the elements will be in ascending order based on natural order also you can construct a TreeSet using a Comparator/Comparable.

Map Interface:
A Map cares about unique identifiers. It's structure is based on key/value(Objects of course).
Equals() method determines if the keys are the same or different.

HashMap:
A HashMap is an unsorted, unordered Set, ensure that you did a good implementation for hashCode() method because it's determines a performance of this collection
HashMap allows one null key and multiple null values in a collection.

HashTable:
It's like a Vector where its methods are synchronized, different from HashMap it does not allow null values as well as null key.

LinkedHashMap:
Similar to LinkedHashSet this collection is ordered, so use it when you care about order when you iterate through it, an order is determined by insertion of elements, you can get faster interation but don't expect good performance for insertion and deletion.

TreeMap:
It's a sorted collection, the elements will be in ascending order based on natural order also you can construct a TreeMap using a Comparator/Comparable.

Queue Interface:
Basically used as a FIFO(first in - first out), it supports all the methods related to collections, in addition there are methods for adding, removing and review the queue.

PriorityQueue:
A PriorityQueue's elements are ordered either by natural ordering

See below the table with useful information.


No comments: