Java Collections Framework

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 The Collections API.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 21 Generics.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Chapter 19 Java Data Structures
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
Collections. Why collections? Collections are used to hold a collection of objects. List holds objects based on order of insertion and can hold non unique.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
(c) University of Washington14-1 CSC 143 Java Collections.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
Big Java Chapter 16.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Object Oriented Programming Ders 10: Data Structures Mustafa Emre İlal
Chapter 18 Java Collections Framework
Collections –data structures and Algorithms L. Grewe.
Data structures and algorithms in the collection framework 1.
Arrays, ArrayLists, and Collections. Rationale Suppose we have a program to compute the average of three numbers. We could write a simple little method.
16-August-2002cse Libraries © 2002 University of Washington1 Class Libraries CSE 142, Summer 2002 Computer Programming 1
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
13 Collections Framework. 2 Contents What is Collection? Collections Framework Collections Hierarchy Collections Implementations Set List Map.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Slides by Donald W. Smith
Using the Java Collection Libraries COMP 103 # T2
Sixth Lecture ArrayList Abstract Class and Interface
Marcus Biel, Software Craftsman
Chapter 19 Java Data Structures
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Software Development Java Collections
Chapter 20 Lists, Stacks, Queues, and Priority Queues
JAVA COLLECTIONS LIBRARY
JAVA COLLECTIONS LIBRARY
University of Central Florida COP 3330 Object Oriented Programming
Java Collections Overview
Generics 27-Nov-18.
Collections Not in our text.
Collections Framework
Introduction to Data Structure
Review: libraries and packages
Generics 2-May-19.
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Introduction to Java Collection
Presentation transcript:

Java Collections Framework In this episode, I will give you a high level introduction to the Java Collections Framework. Marcus Biel, Software Craftsman 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.

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

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

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.

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

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

What is the Java Collections Framework? So what is the Java Collections framework, from a high level perspective?

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:

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.

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.

Collections interface and class hierarchy 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. <<interface>> Collection <<interface>> Map

Collections interface and class hierarchy 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. <<interface>> Collection <<interface>> Map

Collections interface and class hierarchy 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. <<interface>> Collection <<interface>> Map

Collection Interface implements extends <<interface>> Collection So here you see the hierarchy of classes and interfaces extending or implementing the collection interface. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList Vector LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends <<interface>> Collection Just try to remember some of the names listed here. This is just an overview so far. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList Vector LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends <<interface>> Collection As you can see, the collection interface sits on top of a number of sub interfaces and implementing classes. A collection can hold a group of objects. The Collection interface is extended by the interfaces Set, List and Queue. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList Vector LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends <<interface>> Collection A Set is defined as a group of unique objects. What is considered as unique is defined by the equals method of the Object type the Set holds. So in other words, a Set can not hold two equal objects. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList Vector LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends <<interface>> Collection A List is defined as a sequence of objects. So unlike a Set, a List can contain duplicate entries. Besides, a List keeps its elements in the order they were inserted into the list. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList Vector LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends <<interface>> Collection A queue has two sides. Entries are added to the end and removed from the top of the queue. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList Vector LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends <<interface>> Collection This is often described as “first in first out”, which is pretty much like a waiting line in real life works. The first person queuing up will also be the first person leaving the queue. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList Vector LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends <<interface>> Collection Now let’s have a closer look at the interfaces and classes that extend or implement the Set interface. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList Vector LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends <<interface>> Collection HashSet, LinkedHashSet and TreeSet are all implementing the Set interface. HashSet is the default implementation that is used in the majority of cases. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList Vector LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends <<interface>> Collection LinkedHashSet is like a mix of a HashSet and a List, as it does not allow duplicate entries like a Set, but it returns its elements in the order in which they were inserted, like a List would do. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList Vector LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends <<interface>> Collection TreeSet will constantly keep all its elements in sorted order. But keep in mind “there is no free lunch”, every added feature comes at a certain cost. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList Vector LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends <<interface>> Collection 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. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList Vector LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends <<interface>> Collection As the name implies, SortedSet is a Set that is constantly sorted. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList Vector LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends <<interface>> Collection The NavigableSet interface was added with Java 6. It allows to navigate through the sorted list, for example it provides methods to retrieve the next element greater or smaller then a given element of the Set. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList Vector LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends <<interface>> Collection Next, let’s have a closer look at the classes that implement the List interface. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList Vector LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends <<interface>> Collection ArrayList is the default implementation of the List interface. Like any list implementation, it does allow duplicate elements, and it does allow to iterate the list in the order of insertion. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList Vector LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends <<interface>> Collection As it is based on arrays, it is very fast to iterate and read from an ArrayList, but adding or removing an element at a random position is very slow, as this will require to rebuild the underlying array structure. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList Vector LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends <<interface>> Collection Vector is a class that exists since JDK 1, which is even before the Collections Framework was added with Java 2. In short, it’s performance is suboptimal, so please never use it, use ArrayList or LinkedList instead. Let’s directly remove it <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList Vector LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends and forget about it. <<interface>> Collection and forget about it. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends <<interface>> Collection The next List implementation is LinkedList. As the name implies, its implementation is based on a LinkedList. Which makes it easy to add or remove elements at any position in the list. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends <<interface>> Collection Last but not least, let’s have a look at the classes implementing the Queue interface. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends <<interface>> Collection We already talked about LinkedList, as it also implements the List interface. However, the fact that it is based on a DoubleLinkedList makes it quite easy to also implement the queue interface. LinkedList is the default Queue implementation. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Collection Interface implements extends <<interface>> Collection PriorityQueue is a Queue implementation that keeps its elements automatically ordered. It has similar functionality like a TreeSet, but it does allow duplicate entries. <<interface>> Set <<interface>> List <<interface>> Queue HashSet <<interface>> SortedSet ArrayList LinkedList PriorityQueue LinkedHashSet <<interface>> NavigableSet implements extends TreeSet

Map Interface implements extends <<interface>> Map Now let’s look at the map interface. This interface has no relation to the Collection interface. A Collection operates on one entity, while a map operates on two entities - <<interface>> SortedMap Hashtable Hashmap <<interface>> NavigableMap implements extends LinkedHashMap TreeMap

Map Interface implements extends <<interface>> Map - A unique key, for example a Vehicle Identification Number, and an object that is related to this key, for example a car object. With the help of the key you can retrieve the object it relates to. <<interface>> SortedMap Hashtable Hashmap <<interface>> NavigableMap implements extends LinkedHashMap TreeMap

Map Interface implements extends <<interface>> Map The interface map is the root of a lot of interfaces and classes, which we will look at now. <<interface>> SortedMap Hashtable Hashmap <<interface>> NavigableMap implements extends LinkedHashMap TreeMap

Map Interface implements extends <<interface>> Map The class Hastable was the first collection in Java JDK1 that was based on the data structure hashtable, so the Java creators called it Hashtable. <<interface>> SortedMap Hashtable Hashmap <<interface>> NavigableMap implements extends LinkedHashMap TreeMap

Map Interface implements extends <<interface>> Map Unfortunately, this makes it hard to differentiate between the two. Like Vector, the class is deprecated because of its suboptimal performance <<interface>> SortedMap Hashtable Hashmap <<interface>> NavigableMap implements extends LinkedHashMap TreeMap

Map Interface implements extends <<interface>> Map so let’s remove and forget about it, too. Instead, use one of the other classes that implement the map interface. <<interface>> SortedMap Hashmap <<interface>> NavigableMap implements extends LinkedHashMap TreeMap

Map Interface implements extends <<interface>> Map HashMap is the default implementation that you should use in the majority of cases. <<interface>> SortedMap Hashmap <<interface>> NavigableMap implements extends LinkedHashMap TreeMap

Map Interface implements extends <<interface>> Map A Map usually does not make any guarantees on how it internally stores its elements. An exception to this rule is LinkedHashMap, which allows to iterate the map in the order of insertion. <<interface>> SortedMap Hashmap <<interface>> NavigableMap implements extends LinkedHashMap TreeMap

Map Interface implements extends <<interface>> Map Last but not least, TreeMap is a constantly sorted map. <<interface>> SortedMap Hashmap <<interface>> NavigableMap implements extends LinkedHashMap TreeMap

Map Interface implements extends <<interface>> Map Now let’s look at the interfaces that extend the map interface. <<interface>> SortedMap Hashmap <<interface>> NavigableMap implements extends LinkedHashMap TreeMap

Map Interface implements extends <<interface>> Map As the name implies, the interface SortedMap extends the map interface and defines the contract of a constantly sorted map. <<interface>> SortedMap Hashmap <<interface>> NavigableMap implements extends LinkedHashMap TreeMap

Map Interface implements extends <<interface>> Map NavigableMap again extends the SortedMap interface and adds methods to navigate through the map. This allows you to retrieve all entries smaller or bigger then a given entry, for example. <<interface>> SortedMap Hashmap <<interface>> NavigableMap implements extends LinkedHashMap TreeMap

Map Interface implements extends <<interface>> Map Actually, there are many similarities between the Map and the Set hierarchy. The reason is that the Set implementations are actually internally backed by a Map implementation. <<interface>> SortedMap Hashmap <<interface>> NavigableMap implements extends LinkedHashMap TreeMap

Map Interface implements extends <<interface>> Map Last but not least, you might have noticed, the Java Collection classes often contain the data structure they are based on in their name. <<interface>> SortedMap Hashmap <<interface>> NavigableMap implements extends LinkedHashMap TreeMap

Map Interface implements extends <<interface>> Map 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. <<interface>> SortedMap Hashmap <<interface>> NavigableMap implements extends LinkedHashMap TreeMap

Map Interface implements extends <<interface>> Map In short, there is no single best option, each one has its very own advantages and disadvantages. <<interface>> SortedMap Hashmap <<interface>> NavigableMap implements extends LinkedHashMap TreeMap

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.

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.

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.

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

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.

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.

Generics Collection<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.

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.

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.

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.

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.

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.

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.

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.

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

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.

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.

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.

Generics Collection<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.

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.

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.

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.

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.

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.

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.

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.

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.

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:

java.util.Iterator boolean hasNext(); E next(); void remove(); boolean hasNext(); - Returns true if the collection has more elements.

java.util.Iterator boolean hasNext(); E next(); void remove(); E next(); - Returns the next element in the iteration.

java.util.Iterator boolean hasNext(); E next(); void remove(); void remove(); - Removes the last element returned by this iterator from the underlying collection.

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, which greatly simplifies the usage of your home made collection.

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:

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.

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.

java.lang.Comparable int compareTo(T o) Implementing the interface java.lang.Comparable defines a sort order for your entities.

java.lang.Comparable int compareTo(T o) The interface contains only one method you need to implement, which is “int compareTo”.

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.

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.

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

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.

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.

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.

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.

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.

java.util.Comparator int compare(T o1, T o2) The rules of the interface contract are pretty much the same as for Comparable:

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

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.

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

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.

Copyright © 2015 Marcus Biel All rights reserved