You are on page 1of 128

Java Collections Framework

Marcus Biel,, Software Craftsman

www.marcus-biel.com
Collection(s)
2. Java Collections Framework

3. a data structure

4. java.util.Collection interface

5. java.util.Collections

Unfortunately,
there are several overloaded uses of the word collection.
Let me clarify the various meanings up-front.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection(s)
1. a compilation or group of things

2. Java Collections Framework

3. a data structure

4. java.util.Collection interface

5. java.util.Collections
The different use cases are:
A collection without any IT relevance,
as a compilation or group of things
2016, Marcus Biel, http://www.marcus-biel.com/
Collection(s)
1. a compilation or group of things

2. Java Collections Framework

3. a data structure

4. java.util.Collection interface

5. java.util.Collections
Second: The Java Collections Framework
a library of different interfaces and classes

2016, Marcus Biel, http://www.marcus-biel.com/


Collection(s)
1. a compilation or group of things

2. Java Collections Framework

3. a data structure

4. java.util.Collection interface

5. java.util.Collections
Third: A collection as a data structure
Think of a box or container,
that can hold a group of objects like an array for example.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection(s)
1. a compilation or group of things

2. Java Collections Framework

3. a data structure

4. java.util.Collection interface

5. java.util.Collections
Fourth: The java.util.Collection interface
one of the two main interfaces of the Java Collections Framework

2016, Marcus Biel, http://www.marcus-biel.com/


Collection(s)
1. a compilation or group of things

2. Java Collections Framework

3. a data structure

4. java.util.Collection interface

5. java.util.Collections
And fifth: java.util.Collections
a utility class that will help you to modify or
operate on Java collections
2016, Marcus Biel, http://www.marcus-biel.com/
What is the Java Collections Framework?

So what is the Java Collections framework,


from a high level perspective?

2016, Marcus Biel, http://www.marcus-biel.com/


What is the Java Collections Framework?
A toolbox of generic interfaces and classes

First of all, it is more like a library,


a toolbox of generic interfaces and classes.
This toolbox contains:
2016, Marcus Biel, http://www.marcus-biel.com/
What is the Java Collections Framework?
A toolbox of generic interfaces and classes
collection interfaces and classes

various collection interfaces and classes that serve


as a more powerful,
object oriented alternative to arrays.
2016, Marcus Biel, http://www.marcus-biel.com/
What is the Java Collections Framework?
A toolbox of generic interfaces and classes
collection interfaces and classes
collection related utility interfaces classes

Collection related utility interfaces and classes


that assist you in using the collections.
I am going to describe both parts in detail now.
2016, Marcus Biel, http://www.marcus-biel.com/
Collections interface and class hierarchy

<<interface>> <<interface>>
Collection Map

On the next slides you will see the interface and


class hierarchy for collections.
Unlike arrays, all collections can dynamically grow or shrink in size.
2016, Marcus Biel, http://www.marcus-biel.com/
Collections interface and class hierarchy

<<interface>> <<interface>>
Collection Map

As said before a collection can hold a group of objects.


A Map can store pairs of objects that have some kind of relation
which ties them together, named key and value.
2016, Marcus Biel, http://www.marcus-biel.com/
Collections interface and class hierarchy

<<interface>> <<interface>>
Collection Map

A value does not have a specific position in this map,


but can be retrieved with the key it is related to.
Relax if you dont get it now, we will look at it in more detail later on.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

So here you see the hierarchy of classes and interfaces extending


or implementing the collection interface.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

Just try to remember some of the names listed here.


This is just an overview so far.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

As you can see, the collection interface sits on top of


a number of sub interfaces and implementing classes.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

A collection can hold a group of objects.


The Collection interface is extended by the interfaces Set, List and Queue.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

A Set is defined as a group of unique objects.


2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

What is considered as unique is defined by the equals


method of the Object type the Set holds.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

So in other words, a Set can not hold two equal objects.


2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

A List is defined as a sequence of objects.


So unlike a Set, a List can contain duplicate entries.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

Besides,
a List keeps its elements in the order they were inserted into the list.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

A queue has two sides.


Entries are added to the end and removed from the top of the queue.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

This is often described as first in first out,


which is pretty much like a waiting line in real life works.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

The first person queuing up will also be


the first person leaving the queue.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

Now lets have a closer look at the interfaces and


classes that extend or implement the Set interface.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

HashSet, LinkedHashSet and TreeSet


are all implementing the Set interface.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

HashSet is the default implementation that is used


in the majority of cases.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

LinkedHashSet is like a mix of a HashSet and a List,


as it does not allow duplicate entries like a Set,
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

but it returns its elements in the order


in which they were inserted, like a List would do.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

TreeSet will constantly keep all its elements in sorted order.


2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

But keep in mind there is no free lunch,


every added feature comes at a certain cost.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

After looking at the classes implementing the Set interface,


lets also have a look at the two extending interfaces
we havent talked about yet.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

As the name implies,


SortedSet is a Set that is constantly sorted.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

The NavigableSet interface was added with Java 6.


It allows to navigate through the sorted list
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

For example it provides methods to retrieve the next element


greater or smaller then a given element of the Set.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

Next, lets have a closer look at the


classes that implement the List interface.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

ArrayList is the default implementation of the List interface.


Like any list implementation, it does allow duplicate elements,
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

and it does allow to iterate the list in the order of insertion.


2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

As it is based on arrays,
it is very fast to iterate and read from an ArrayList,
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

but adding or removing an element at a random position is very slow,


as this will require to rebuild the underlying array structure.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

Vector is a class that exists since JDK 1,


which is even before the Collections Framework was added with Java 2.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList Vector LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

In short, its performance is suboptimal, so please never use it,


use ArrayList or LinkedList instead. Lets directly remove it
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

and forget about it.


2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

The next List implementation is LinkedList.


As the name implies, its implementation is based on a LinkedList.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

Which makes it easy to add or


remove elements at any position in the list.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

Last but not least, lets have a look at the classes


implementing the Queue interface.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

We already talked about LinkedList,


as it also implements the List interface.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

However, the fact that it is based on a DoubleLinkedList


makes it quite easy to also implement the queue interface.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

LinkedList is the default Queue implementation.


2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

PriorityQueue is a Queue implementation that


keeps its elements automatically ordered.
2016, Marcus Biel, http://www.marcus-biel.com/
Collection Interface <<interface>>
Collection

<<interface>> <<interface>> <<interface>>


Set List Queue

<<interface>>
HashSet ArrayList LinkedList PriorityQueue
SortedSet

<<interface>>
LinkedHashSet
NavigableSet implements
extends
TreeSet

It has similar functionality like a TreeSet,


but it does allow duplicate entries.
2016, Marcus Biel, http://www.marcus-biel.com/
Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashtable Hashmap
NavigableMap

LinkedHashMap TreeMap

Now lets look at the map interface.


This interface has no relation to the Collection interface.
2016, Marcus Biel, http://www.marcus-biel.com/
Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashtable Hashmap
NavigableMap

LinkedHashMap TreeMap

A Collection operates on one entity,


while a map operates on two entities -
2016, Marcus Biel, http://www.marcus-biel.com/
Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashtable Hashmap
NavigableMap

LinkedHashMap TreeMap

- A unique key, for example a Vehicle Identification Number,


- and an object that is related to this key, for example a car object.
2016, Marcus Biel, http://www.marcus-biel.com/
Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashtable Hashmap
NavigableMap

LinkedHashMap TreeMap

With the help of the key you can retrieve the object it relates to.

2016, Marcus Biel, http://www.marcus-biel.com/


Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashtable Hashmap
NavigableMap

LinkedHashMap TreeMap

The interface map is the root of a lot of interfaces and classes,


which we will look at now.
2016, Marcus Biel, http://www.marcus-biel.com/
Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashtable Hashmap
NavigableMap

LinkedHashMap TreeMap

The class Hastable was the first collection in Java JDK1


that was based on the data structure hashtable,
2016, Marcus Biel, http://www.marcus-biel.com/
Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashtable Hashmap
NavigableMap

LinkedHashMap TreeMap

so the Java creators called it Hashtable.

2016, Marcus Biel, http://www.marcus-biel.com/


Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashtable Hashmap
NavigableMap

LinkedHashMap TreeMap

Unfortunately, this makes it hard to differentiate between the two.


2016, Marcus Biel, http://www.marcus-biel.com/
Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashtable Hashmap
NavigableMap

LinkedHashMap TreeMap

Like Vector, the class is deprecated because


of its suboptimal performance
2016, Marcus Biel, http://www.marcus-biel.com/
Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashmap
NavigableMap

LinkedHashMap TreeMap

so lets remove and forget about it, too.


Instead, use one of the other classes that implement
the map interface.
2016, Marcus Biel, http://www.marcus-biel.com/
Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashmap
NavigableMap

LinkedHashMap TreeMap

HashMap is the default implementation that


you should use in the majority of cases.
2016, Marcus Biel, http://www.marcus-biel.com/
Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashmap
NavigableMap

LinkedHashMap TreeMap

A Map usually does not make any guarantees on


how it internally stores its elements.
2016, Marcus Biel, http://www.marcus-biel.com/
Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashmap
NavigableMap

LinkedHashMap TreeMap

An exception to this rule is LinkedHashMap,


which allows to iterate the map in the order of insertion.
2016, Marcus Biel, http://www.marcus-biel.com/
Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashmap
NavigableMap

LinkedHashMap TreeMap

Last but not least, TreeMap is a constantly sorted map.

2016, Marcus Biel, http://www.marcus-biel.com/


Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashmap
NavigableMap

LinkedHashMap TreeMap

Now lets look at the interfaces that extend the map interface.

2016, Marcus Biel, http://www.marcus-biel.com/


Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashmap
NavigableMap

LinkedHashMap TreeMap

As the name implies,


the interface SortedMap extends the map interface and
defines the contract of a constantly sorted map.
2016, Marcus Biel, http://www.marcus-biel.com/
Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashmap
NavigableMap

LinkedHashMap TreeMap

NavigableMap again extends the SortedMap interface and


adds methods to navigate through the map.
2016, Marcus Biel, http://www.marcus-biel.com/
Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashmap
NavigableMap

LinkedHashMap TreeMap

This allows you to retrieve all entries smaller or


bigger then a given entry, for example.
2016, Marcus Biel, http://www.marcus-biel.com/
Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashmap
NavigableMap

LinkedHashMap TreeMap

Actually, there are many similarities between


the Map and the Set hierarchy.
2016, Marcus Biel, http://www.marcus-biel.com/
Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashmap
NavigableMap

LinkedHashMap TreeMap

The reason is that the Set implementations are


actually internally backed by a Map implementation.
2016, Marcus Biel, http://www.marcus-biel.com/
Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashmap
NavigableMap

LinkedHashMap TreeMap

Last but not least, you might have noticed,


the Java Collection classes often contain the data
structure they are based on in their name.
2016, Marcus Biel, http://www.marcus-biel.com/
Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashmap
NavigableMap

LinkedHashMap TreeMap

To choose the best collection for a given situation you have to


compare the specific characteristics of data structures like
Array, LinkedList, Hashtable or Tree first.
2016, Marcus Biel, http://www.marcus-biel.com/
Map Interface <<interface>>
Map
implements
extends <<interface>>
SortedMap

<<interface>>
Hashmap
NavigableMap

LinkedHashMap TreeMap

In short, there is no single best option,


each one has its very own advantages and disadvantages.
2016, Marcus Biel, http://www.marcus-biel.com/
Generics
List myList = new ArrayList (100);

Map myMap = new HashMap(100);

Generics is a topic at least as big as the Java Collections Framework.


In the context of this episode I will therefore only explain you the
bare minimum you need to understand the Collections Framework.
2016, Marcus Biel, http://www.marcus-biel.com/
Generics
List<String> myList = new ArrayList<String>(100);

In the first line, you see I have defined a List variable myList,
and with the String parameter in angle brackets
I tell the compiler that my myList reference variable
is supposed to be used only with Strings.
2016, Marcus Biel, http://www.marcus-biel.com/
Generics
List<String> myList = new ArrayList<String>(100);

Then I create an Object of type ArrayList, and again I tell the


compiler that this object is only supposed to be used with Strings,
and the compiler will ensure that no one ever tries to put anything
else then a String into my List.
2016, Marcus Biel, http://www.marcus-biel.com/
Generics
List<String> myList = new ArrayList<String>(100);

Map<VIN, Car> myMap = new HashMap<VIN, Car>(100);

In other words, this is what makes the containers type save.

2016, Marcus Biel, http://www.marcus-biel.com/


Generics
List<String> myList = new ArrayList<String>(100);

Map<VIN, Car> myMap = new HashMap<VIN, Car>(100);

Also note that I use the interface List


for the variable and not ArrayList. This makes your code more flexible.
Only at one place you create the object,
but at various places in your code you will use it.

2016, Marcus Biel, http://www.marcus-biel.com/


Generics
List<String> myList = new ArrayList<String>(100);

Map<VIN, Car> myMap = new HashMap<VIN, Car>(100);

When you use List instead of ArrayList for the reference variable,
you could later replace the ArrayList by a LinkedList,
and all you had to adjust was this one line of code.
2016, Marcus Biel, http://www.marcus-biel.com/
Generics
Collection<String> myList = new ArrayList<String>(100);

Map<VIN, Car> myMap = new HashMap<VIN, Car>(100);

In case you dont really need the specific methods of a list,


you could also use the Collection interface instead.
Always use the least specific interface for the reference variable.
2016, Marcus Biel, http://www.marcus-biel.com/
Generics
Collection<String> myList = new ArrayList<String>(100);

Map<VIN, Car> myMap = new HashMap<VIN, Car>(100);

By the way - I am sure you noticed the integer one hundred


that I used as a constructor argument.
This I have added for performance optimization.
2016, Marcus Biel, http://www.marcus-biel.com/
Generics
Collection<String> myList = new ArrayList<String>(100);

Map<VIN, Car> myMap = new HashMap<VIN, Car>(100);

As said before, the collection classes can


dynamically grow and shrink in size.
However ArrayList and all Hashtable based
collections are internally operating on arrays.
2016, Marcus Biel, http://www.marcus-biel.com/
Generics
Collection<String> myList = new ArrayList<String>(100);

Map<VIN, Car> myMap = new HashMap<VIN, Car>(100);

When an array based collection grows in size, it will internally,


on the fly, create a larger array,
and transfer all contents from the old to the new array.
2016, Marcus Biel, http://www.marcus-biel.com/
Generics
Collection<String> myList = new ArrayList<String>(100);

Map<VIN, Car> myMap = new HashMap<VIN, Car>(100);

This of course takes some extra time.


Modern hardware is so fast that this usually should not be a problem.

2016, Marcus Biel, http://www.marcus-biel.com/


Generics
Collection<String> myList = new ArrayList<String>(100);

Map<VIN, Car> myMap = new HashMap<VIN, Car>(100);

But on the other side, if you already know the


exact or approximate size of your array based collection,
this is usually better then trusting on the collections default sizes.
2016, Marcus Biel, http://www.marcus-biel.com/
Generics
Collection<String> myList = new ArrayList<String>(100);

Map<VIN, Car> myMap = new HashMap<VIN, Car>(100);

Now in the second line you see


how a HashMap is instantiated accordingly.

2016, Marcus Biel, http://www.marcus-biel.com/


Generics
Collection<String> myList = new ArrayList<String>(100);

Map<VIN, Car> myMap = new HashMap<VIN, Car>(100);

As said before, a map is basically a relation of


one identifying Key element to one Value element,
both these elements can be of different types.
2016, Marcus Biel, http://www.marcus-biel.com/
Generics
Collection<String> myList = new ArrayList<String>(100);

Map<VIN, Car> myMap = new HashMap<VIN, Car>(100);

Like here in my example I use


VIN - the vehicle identification number as the Key,
and a Car object as the Value.
2016, Marcus Biel, http://www.marcus-biel.com/
Generics
Collection<String> myList = new ArrayList<String>(100);

Map<VIN, Car> myMap = new HashMap<VIN, Car>(100);

This has to be added as a comma separated list in angle brackets,


and the compiler again will check that this holds true.
2016, Marcus Biel, http://www.marcus-biel.com/
Generics
Collection<String> myList = new ArrayList<>(100);

Map<VIN, Car> myMap = new HashMap<>(100);

If you create the instance reference variable


and the Object both in one line, you can also leave the
second pair of angle brackets empty,
as it can be inferred from the generic type of the reference variable.
2016, Marcus Biel, http://www.marcus-biel.com/
Generics
Collection<String> myList = new ArrayList<>(100);

Map<VIN, Car> myMap = new HashMap<>(100);

This was introduced with Java 7 and


is called the Diamond operator,
because the empty angle brackets in a way look like a diamond.
2016, Marcus Biel, http://www.marcus-biel.com/
Generics
Collection<String> myList = new ArrayList<>(100);

Map<VIN, Car> myMap = new HashMap<>(100);

Actually, I am sorry, but thats not the whole story.


There are two parts. What I just showed you, was actually part two,
the usage, or invocation of a generic class,
where you lock in the concrete Parameter to be used.
2016, Marcus Biel, http://www.marcus-biel.com/
Generics
Collection<String> myList = new ArrayList<>(100);

Map<VIN, Car> myMap = new HashMap<>(100);

But this is only possible, if the method, interface or class


was defined to be used in a generic way before hand.

2016, Marcus Biel, http://www.marcus-biel.com/


Generics
public interface MyInterface<E,T> {

E read()

void process(T o1, T o2)

Here you see a generically defined interface.


In the first line, the interface is defined as
an interface operating on two
separate generic types that have to be specified at a later time.
2016, Marcus Biel, http://www.marcus-biel.com/
Generics
public interface MyInterface<E,T> {

E read()

void process(T o1, T o2)

However, when these types are locked in, this will automatically
also specify the types interface methods will use.

2016, Marcus Biel, http://www.marcus-biel.com/


Generics
public interface MyInterface<E,T> {

E read()

void process(T o1, T o2)

So if you see some weird one letter types in one of the next slides,
just remember it means this is a
method that can be used in a generic way.
2016, Marcus Biel, http://www.marcus-biel.com/
Utility Interfaces
java.util.Iterator

java.lang.Iterable

java.lang.Comparable

java.util.Comparator

Okay now we are ready to look at some additional


Utility interfaces of the Java Collections Framework.

2016, Marcus Biel, http://www.marcus-biel.com/


Utility Interfaces
java.util.Iterator

java.lang.Iterable

java.lang.Comparable

java.util.Comparator

They are implemented by classes of the


Collections Framework or the JDK in general,
but they can also be implemented by your own classes,
making use of the power of the Collections Framework.
2016, Marcus Biel, http://www.marcus-biel.com/
Utility Interfaces
java.util.Iterator

java.lang.Iterable

java.lang.Comparable

java.util.Comparator

Well - strictly speaking, the interface java.lang.Iterable


is not part of the framework, but more precisely on top of it.

2016, Marcus Biel, http://www.marcus-biel.com/


Utility Interfaces
java.util.Iterator

java.lang.Iterable

java.lang.Comparable

java.util.Comparator

It is the super interface of java.util.Collection,


so every class that implements java.util.Collection
will also implement the java.lang.Iterable interface.
Okay, anyway, lets look at each interface in detail.
2016, Marcus Biel, http://www.marcus-biel.com/
java.util.Iterator
boolean hasNext();

E next();

void remove();

An Iterator is an object that acts like


a remote control to iterate through a collection.
Lets look at its methods:
2016, Marcus Biel, http://www.marcus-biel.com/
java.util.Iterator
boolean hasNext();

E next();

void remove();

boolean hasNext(); -
Returns true if the collection has more elements.

2016, Marcus Biel, http://www.marcus-biel.com/


java.util.Iterator
boolean hasNext();

E next();

void remove();

E next(); -
Returns the next element in the iteration.

2016, Marcus Biel, http://www.marcus-biel.com/


java.util.Iterator
boolean hasNext();

E next();

void remove();

void remove(); -
Removes the last element returned by
this iterator from the underlying collection.
2016, Marcus Biel, http://www.marcus-biel.com/
java.lang.Iterable
Iterator<T> iterator()

This interfaces provides only one method


which will return an Iterator.
Every collection that implements this interface
can be used in the for each loop,
2016, Marcus Biel, http://www.marcus-biel.com/
java.lang.Iterable
Iterator<T> iterator()

which greatly simplifies the usage of your home made collection.

2016, Marcus Biel, http://www.marcus-biel.com/


java.lang.Iterable
Iterator<T> iterator()

In order to plug in your collection into the for each loop,


you will have to execute two simple steps:

2016, Marcus Biel, http://www.marcus-biel.com/


java.lang.Iterable
Iterator<T> iterator()

First create an Iterator that is able to iterate over your collection,


with methods like hasNext and next(), as we saw on the last slide.
2016, Marcus Biel, http://www.marcus-biel.com/
java.lang.Iterable
Iterator<T> iterator()

Second, you need to implement the Iterable interface by adding


an iterator() method that will return an instance of this Iterator.
2016, Marcus Biel, http://www.marcus-biel.com/
java.lang.Comparable

int compareTo(T o)

Implementing the interface java.lang.Comparable


defines a sort order for your entities.

2016, Marcus Biel, http://www.marcus-biel.com/


java.lang.Comparable

int compareTo(T o)

The interface contains only one method you need to implement,


which is int compareTo.

2016, Marcus Biel, http://www.marcus-biel.com/


java.lang.Comparable

int compareTo(T o)

If you want to define a natural sort order for an entity class,


make it implement this interface.

2016, Marcus Biel, http://www.marcus-biel.com/


java.lang.Comparable

int compareTo(T o)

Return a negative integer if the object is less


than the given method argument,
zero if the object is equal to the given method argument and
a positive integer if the object is greater than
the given method argument.
2016, Marcus Biel, http://www.marcus-biel.com/
java.lang.Comparable
Return a negative integer if the object is less than the given method
argument, zero if the object is equal to the given method argument and
a positive integer if the object is greater than the given method argument.

2016, Marcus Biel, http://www.marcus-biel.com/


java.lang.Comparable
Return a negative integer if the object is less than the given method
argument, zero if the object is equal to the given method argument and
a positive integer if the object is greater than the given method argument.

What means smaller or greater is for you to define.


For numbers that would probably mean that
1 is smaller then 5 for example.
But for colors?
This all depends on how you want to sort your entities.
2016, Marcus Biel, http://www.marcus-biel.com/
java.lang.Comparable
Return a negative integer if the object is less than the given method
argument, zero if the object is equal to the given method argument and
a positive integer if the object is greater than the given method argument.

When you put objects of an entity that implements the


Comparable interface into a TreeSet or TreeMap,
it will use your compareTo method to
automatically sort all elements you put into the collection.
2016, Marcus Biel, http://www.marcus-biel.com/
java.lang.Comparable
Return a negative integer if the object is less than the given method
argument, zero if the object is equal to the given method argument and
a positive integer if the object is greater than the given method argument.

As you can see, the Java Collections framework has been


greatly designed for extension,
it offers a lot of possibilities to plug in your own classes.
2016, Marcus Biel, http://www.marcus-biel.com/
java.util.Comparator

int compare(T o1, T o2)

This interface is very similar to the Comparable interface.


It allows you to define additional sorting orders,
like a Reverse Ordering.
2016, Marcus Biel, http://www.marcus-biel.com/
java.util.Comparator

int compare(T o1, T o2)

So the sorting logic is not directly implemented in your entity,


but in an external sorting strategy class that can optionally be added
to a Collection or a sorting method to define an alternative
sorting order for your collection of entities.
2016, Marcus Biel, http://www.marcus-biel.com/
java.util.Comparator

int compare(T o1, T o2)

The rules of the interface contract are pretty much


the same as for Comparable:

2016, Marcus Biel, http://www.marcus-biel.com/


java.util.Comparator
Return a negative integer if the first argument is less than the
second argument, zero if both arguments are equal and a
positive integer if the first argument is greater than the second

2016, Marcus Biel, http://www.marcus-biel.com/


Utility classes

Last but not least, lets look at the two utility classes Collections and Arrays.
Like a Swiss army knife they provide static helper methods that
greatly enhance the general usefulness of the collection classes.

2016, Marcus Biel, http://www.marcus-biel.com/


Utility classes

java.util.Collections

java.util.Collections
Offers methods like sort, shuffle, reverse, search, min or max.

2016, Marcus Biel, http://www.marcus-biel.com/


Utility classes

java.util.Collections

java.util.Arrays

java.util.Arrays -
Operates on Arrays and not on collections actually.
Similar to the Collections class, it allows to sort arrays or
to search through arrays, for example.
2016, Marcus Biel, http://www.marcus-biel.com/
Copyright 2016
Marcus Biel
All rights reserved

2016, Marcus Biel, http://www.marcus-biel.com/

You might also like