java collections framework inroduction with video tutorial

128
Java Collections Framework Marcus Biel,, Software Craftsman www.marcus-biel.com

Upload: marcus-biel

Post on 15-Jan-2017

327 views

Category:

Education


3 download

TRANSCRIPT

Page 1: Java Collections Framework Inroduction with Video Tutorial

Java Collections FrameworkMarcus Biel,,Software Craftsman

www.marcus-biel.com

Page 2: Java Collections Framework Inroduction with Video Tutorial

2016, Marcus Biel, http://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.

Page 3: Java Collections Framework Inroduction with Video Tutorial

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.CollectionsThe different use cases are:

A collection without any IT relevance, as a compilation or group of things

Page 4: Java Collections Framework Inroduction with Video Tutorial

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.CollectionsSecond: The “Java Collections Framework” – a library of different interfaces and classes

Page 5: Java Collections Framework Inroduction with Video Tutorial

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.CollectionsThird: A collection as a data structure –

Think of a box or container, that can hold a group of objects like an array for example.

Page 6: Java Collections Framework Inroduction with Video Tutorial

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.CollectionsFourth: The java.util.Collection interface –

one of the two main interfaces of the Java Collections Framework

Page 7: Java Collections Framework Inroduction with Video Tutorial

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.CollectionsAnd fifth: java.util.Collections –

a utility class that will help you to modify or operate on Java collections

Page 8: Java Collections Framework Inroduction with Video Tutorial

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?

Page 9: Java Collections Framework Inroduction with Video Tutorial

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:

Page 10: Java Collections Framework Inroduction with Video Tutorial

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.

Page 11: Java Collections Framework Inroduction with Video Tutorial

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.

Page 12: Java Collections Framework Inroduction with Video Tutorial

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

Collections interface and class hierarchy

<<interface>>

Collection

<<interface>>

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.

Page 13: Java Collections Framework Inroduction with Video Tutorial

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

Collections interface and class hierarchy

<<interface>>

Collection

<<interface>>

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.

Page 14: Java Collections Framework Inroduction with Video Tutorial

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

Collections interface and class hierarchy

<<interface>>

Collection

<<interface>>

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 don’t get it now, we will look at it in more detail later on.

Page 15: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

So here you see the hierarchy of classes and interfaces extending or implementing the collection interface.

Page 16: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

Just try to remember some of the names listed here. This is just an overview so far.

Page 17: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

As you can see, the collection interface sits on top of a number of sub interfaces and implementing classes.

Page 18: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

A collection can hold a group of objects. The Collection interface is extended by the interfaces Set, List and Queue.

Page 19: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

A Set is defined as a group of unique objects.

Page 20: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

What is considered as unique is defined by the equals method of the Object type the Set holds.

Page 21: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

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

Page 22: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

A List is defined as a sequence of objects. So unlike a Set, a List can contain duplicate entries.

Page 23: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

Besides, a List keeps its elements in the order they were inserted into the list.

Page 24: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

A queue has two sides. Entries are added to the end and removed from the top of the queue.

Page 25: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

This is often described as “first in first out”, which is pretty much like a waiting line in real life works.

Page 26: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

The first person queuing up will also be the first person leaving the queue.

Page 27: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

Now let’s have a closer look at the interfaces and classes that extend or implement the Set interface.

Page 28: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

HashSet, LinkedHashSet and TreeSet are all implementing the Set interface.

Page 29: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

HashSet is the default implementation that is used in the majority of cases.

Page 30: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

LinkedHashSet is like a mix of a HashSet and a List, as it does not allow duplicate entries like a Set,

Page 31: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

but it returns its elements in the order in which they were inserted, like a List would do.

Page 32: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

TreeSet will constantly keep all its elements in sorted order.

Page 33: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

But keep in mind “there is no free lunch”, every added feature comes at a certain cost.

Page 34: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

After looking at the classes implementing the Set interface, let’s also have a look at the two extending interfaces

we haven’t talked about yet.

Page 35: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

As the name implies, SortedSet is a Set that is constantly sorted.

Page 36: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

The NavigableSet interface was added with Java 6. It allows to navigate through the sorted list

Page 37: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

For example it provides methods to retrieve the next element greater or smaller then a given element of the Set.

Page 38: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

Next, let’s have a closer look at the classes that implement the List interface.

Page 39: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

ArrayList is the default implementation of the List interface. Like any list implementation, it does allow duplicate elements,

Page 40: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

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

Page 41: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

As it is based on arrays, it is very fast to iterate and read from an ArrayList,

Page 42: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

but adding or removing an element at a random position is very slow, as this will require to rebuild the underlying array structure.

Page 43: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

Vector is a class that exists since JDK 1, which is even before the Collections Framework was added with Java 2.

Page 44: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList Vector LinkedList PriorityQueue

LinkedHashSet implementsextends

In short, it’s performance is suboptimal, so please never use it, use ArrayList or LinkedList instead. Let’s directly remove it

Page 45: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList LinkedList PriorityQueue

LinkedHashSet implementsextends

and forget about it.

Page 46: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList LinkedList PriorityQueue

LinkedHashSet implementsextends

The next List implementation is LinkedList. As the name implies, its implementation is based on a LinkedList.

Page 47: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList LinkedList PriorityQueue

LinkedHashSet implementsextends

Which makes it easy to add or remove elements at any position in the list.

Page 48: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList LinkedList PriorityQueue

LinkedHashSet implementsextends

Last but not least, let’s have a look at the classes implementing the Queue interface.

Page 49: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList LinkedList PriorityQueue

LinkedHashSet implementsextends

We already talked about LinkedList, as it also implements the List interface.

Page 50: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList LinkedList PriorityQueue

LinkedHashSet implementsextends

However, the fact that it is based on a DoubleLinkedList makes it quite easy to also implement the queue interface.

Page 51: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList LinkedList PriorityQueue

LinkedHashSet implementsextends

LinkedList is the default Queue implementation.

Page 52: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList LinkedList PriorityQueue

LinkedHashSet implementsextends

PriorityQueue is a Queue implementation that keeps its elements automatically ordered.

Page 53: Java Collections Framework Inroduction with Video Tutorial

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

Collection Interface<<interface>>

Collection

<<interface>>

Set

<<interface>>

List

<<interface>>

Queue

HashSet<<interface>

>SortedSet

<<interface>>

NavigableSet

TreeSet

ArrayList LinkedList PriorityQueue

LinkedHashSet implementsextends

It has similar functionality like a TreeSet, but it does allow duplicate entries.

Page 54: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashtable Hashmap

LinkedHashMap

implementsextends

Now let’s look at the map interface. This interface has no relation to the Collection interface.

Page 55: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashtable Hashmap

LinkedHashMap

implementsextends

A Collection operates on one entity, while a map operates on two entities -

Page 56: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashtable Hashmap

LinkedHashMap

implementsextends

- A unique key, for example a Vehicle Identification Number,- and an object that is related to this key, for example a car object.

Page 57: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashtable Hashmap

LinkedHashMap

implementsextends

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

Page 58: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashtable Hashmap

LinkedHashMap

implementsextends

The interface map is the root of a lot of interfaces and classes, which we will look at now.

Page 59: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashtable Hashmap

LinkedHashMap

implementsextends

The class Hastable was the first collection in Java JDK1 that was based on the data structure hashtable,

Page 60: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashtable Hashmap

LinkedHashMap

implementsextends

so the Java creators called it Hashtable.

Page 61: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashtable Hashmap

LinkedHashMap

implementsextends

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

Page 62: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashtable Hashmap

LinkedHashMap

implementsextends

Like Vector, the class is deprecated because of its suboptimal performance

Page 63: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashmap

LinkedHashMap

implementsextends

so let’s remove and forget about it, too. Instead, use one of the other classes that implement

the map interface.

Page 64: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashmap

LinkedHashMap

implementsextends

HashMap is the default implementation that you should use in the majority of cases.

Page 65: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashmap

LinkedHashMap

implementsextends

A Map usually does not make any guarantees on how it internally stores its elements.

Page 66: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashmap

LinkedHashMap

implementsextends

An exception to this rule is LinkedHashMap, which allows to iterate the map in the order of insertion.

Page 67: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashmap

LinkedHashMap

implementsextends

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

Page 68: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashmap

LinkedHashMap

implementsextends

Now let’s look at the interfaces that extend the map interface.

Page 69: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashmap

LinkedHashMap

implementsextends

As the name implies, the interface SortedMap extends the map interface and

defines the contract of a constantly sorted map.

Page 70: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashmap

LinkedHashMap

implementsextends

NavigableMap again extends the SortedMap interface and adds methods to navigate through the map.

Page 71: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashmap

LinkedHashMap

implementsextends

This allows you to retrieve all entries smaller or bigger then a given entry, for example.

Page 72: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashmap

LinkedHashMap

implementsextends

Actually, there are many similarities between the Map and the Set hierarchy.

Page 73: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashmap

LinkedHashMap

implementsextends

The reason is that the Set implementations are actually internally backed by a Map implementation.

Page 74: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashmap

LinkedHashMap

implementsextends

Last but not least, you might have noticed, the Java Collection classes often contain the data

structure they are based on in their name.

Page 75: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashmap

LinkedHashMap

implementsextends

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.

Page 76: Java Collections Framework Inroduction with Video Tutorial

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

Map Interface<<interface>>

Map

TreeMap

<<interface>>

NavigableMap

<<interface>>

SortedMap

Hashmap

LinkedHashMap

implementsextends

In short, there is no single best option, each one has its very own advantages and disadvantages.

Page 77: Java Collections Framework Inroduction with Video Tutorial

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

GenericsList 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.

Page 78: Java Collections Framework Inroduction with Video Tutorial

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

GenericsList<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.

Page 79: Java Collections Framework Inroduction with Video Tutorial

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

GenericsList<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.

Page 80: Java Collections Framework Inroduction with Video Tutorial

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

GenericsList<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”.

Page 81: Java Collections Framework Inroduction with Video Tutorial

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

GenericsList<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.

Page 82: Java Collections Framework Inroduction with Video Tutorial

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

GenericsList<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.

Page 83: Java Collections Framework Inroduction with Video Tutorial

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

GenericsCollection<String> myList = new ArrayList<String>(100);

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

In case you don’t 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.

Page 84: Java Collections Framework Inroduction with Video Tutorial

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

GenericsCollection<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.

Page 85: Java Collections Framework Inroduction with Video Tutorial

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

GenericsCollection<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.

Page 86: Java Collections Framework Inroduction with Video Tutorial

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

GenericsCollection<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.

Page 87: Java Collections Framework Inroduction with Video Tutorial

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

GenericsCollection<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.

Page 88: Java Collections Framework Inroduction with Video Tutorial

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

GenericsCollection<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.

Page 89: Java Collections Framework Inroduction with Video Tutorial

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

GenericsCollection<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.

Page 90: Java Collections Framework Inroduction with Video Tutorial

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

GenericsCollection<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.

Page 91: Java Collections Framework Inroduction with Video Tutorial

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

GenericsCollection<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”.

Page 92: Java Collections Framework Inroduction with Video Tutorial

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

GenericsCollection<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.

Page 93: Java Collections Framework Inroduction with Video Tutorial

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

GenericsCollection<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.

Page 94: Java Collections Framework Inroduction with Video Tutorial

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

GenericsCollection<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.

Page 95: Java Collections Framework Inroduction with Video Tutorial

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

GenericsCollection<String> myList = new ArrayList<>(100);

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

Actually, I am sorry, but that’s 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.

Page 96: Java Collections Framework Inroduction with Video Tutorial

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

GenericsCollection<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.

Page 97: Java Collections Framework Inroduction with Video Tutorial

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

Genericspublic 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 twoseparate generic types that have to be specified at a later time.

Page 98: Java Collections Framework Inroduction with Video Tutorial

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

Genericspublic 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.

Page 99: Java Collections Framework Inroduction with Video Tutorial

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

Genericspublic 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.

Page 100: Java Collections Framework Inroduction with Video Tutorial

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.

Page 101: Java Collections Framework Inroduction with Video Tutorial

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.

Page 102: Java Collections Framework Inroduction with Video Tutorial

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.

Page 103: Java Collections Framework Inroduction with Video Tutorial

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, let’s look at each interface in detail.

Page 104: Java Collections Framework Inroduction with Video Tutorial

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.

Let’s look at its methods:

Page 105: Java Collections Framework Inroduction with Video Tutorial

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.

Page 106: Java Collections Framework Inroduction with Video Tutorial

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.

Page 107: Java Collections Framework Inroduction with Video Tutorial

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.

Page 108: Java Collections Framework Inroduction with Video Tutorial

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,

Page 109: Java Collections Framework Inroduction with Video Tutorial

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

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

which greatly simplifies the usage of your home made collection.

Page 110: Java Collections Framework Inroduction with Video Tutorial

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:

Page 111: Java Collections Framework Inroduction with Video Tutorial

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.

Page 112: Java Collections Framework Inroduction with Video Tutorial

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.

Page 113: Java Collections Framework Inroduction with Video Tutorial

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.

Page 114: Java Collections Framework Inroduction with Video Tutorial

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”.

Page 115: Java Collections Framework Inroduction with Video Tutorial

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.

Page 116: Java Collections Framework Inroduction with Video Tutorial

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.

Page 117: Java Collections Framework Inroduction with Video Tutorial

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

java.lang.ComparableReturn 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.

Page 118: Java Collections Framework Inroduction with Video Tutorial

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

java.lang.Comparable

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.

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.

Page 119: Java Collections Framework Inroduction with Video Tutorial

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

java.lang.ComparableReturn 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.

Page 120: Java Collections Framework Inroduction with Video Tutorial

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

java.lang.ComparableReturn 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.

Page 121: Java Collections Framework Inroduction with Video Tutorial

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.

Page 122: Java Collections Framework Inroduction with Video Tutorial

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.

Page 123: Java Collections Framework Inroduction with Video Tutorial

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:

Page 124: Java Collections Framework Inroduction with Video Tutorial

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

java.util.ComparatorReturn 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

Page 125: Java Collections Framework Inroduction with Video Tutorial

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

Utility classes

Last but not least, let’s 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.

Page 126: Java Collections Framework Inroduction with Video Tutorial

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.

Page 127: Java Collections Framework Inroduction with Video Tutorial

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.

Page 128: Java Collections Framework Inroduction with Video Tutorial

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

Copyright © 2016 Marcus Biel

All rights reserved