Download presentation
Presentation is loading. Please wait.
1
Java Collections Framework
In this episode, I will give you a high level introduction to the Java Collections Framework. Marcus Biel, Software Craftsman
2
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.
3
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
4
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
5
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.
6
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
7
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
8
What is the Java Collections Framework?
So what is the Java Collections framework, from a high level perspective?
9
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:
10
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.
11
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.
12
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
13
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
14
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
15
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
16
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
17
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
18
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
19
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
20
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
21
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
22
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
23
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
24
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
25
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
26
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
27
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
28
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
29
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
30
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
31
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
32
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
33
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
34
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
35
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
36
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
37
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
38
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
39
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
40
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
41
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
42
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
43
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
44
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
45
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
46
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
47
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
48
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
49
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
50
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
51
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
52
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
53
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
54
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.
55
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.
56
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.
57
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”.
58
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.
59
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.
60
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.
61
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.
62
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.
63
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.
64
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.
65
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.
66
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.
67
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.
68
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”.
69
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.
70
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.
71
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.
72
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.
73
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.
74
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.
75
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.
76
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.
77
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.
78
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.
79
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.
80
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.
81
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:
82
java.util.Iterator boolean hasNext(); E next(); void remove();
boolean hasNext(); - Returns true if the collection has more elements.
83
java.util.Iterator boolean hasNext(); E next(); void remove();
E next(); - Returns the next element in the iteration.
84
java.util.Iterator boolean hasNext(); E next(); void remove();
void remove(); - Removes the last element returned by this iterator from the underlying collection.
85
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.
86
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:
87
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.
88
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.
89
java.lang.Comparable int compareTo(T o)
Implementing the interface java.lang.Comparable defines a sort order for your entities.
90
java.lang.Comparable int compareTo(T o)
The interface contains only one method you need to implement, which is “int compareTo”.
91
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.
92
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.
93
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.
94
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.
95
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.
96
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.
97
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.
98
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.
99
java.util.Comparator int compare(T o1, T o2)
The rules of the interface contract are pretty much the same as for Comparable:
100
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
101
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.
102
Utility classes java.util.Collections
java.util.Collections - Offers methods like sort, shuffle, reverse, search, min or max.
103
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.
104
Copyright © 2015 Marcus Biel All rights reserved
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.