Professional Documents
Culture Documents
9 January 2015
OSU CSE
Overview
The Java Collections Framework (JCF)
is a group of interfaces and classes similar
to the OSU CSE components
The similarities will become clearly evident
from examples
See Java libraries package java.util
OSU CSE
Overview of Interfaces
Iterable
Collection
Set
SortedSet
List
Map
Queue
SortedMap
Deque
NavigableMap
NavigableSet
9 January 2015
OSU CSE
Overview of Interfaces
Iterable
Collection
Set
SortedSet
NavigableSet
9 January 2015
List
Map
Queue
Deque
Map
does
Note:
not
extend Collection;
but it is a collection.
OSU CSE
SortedMap
NavigableMap
Overview of Interfaces
Iterable
Collection
Set
SortedSet
NavigableSet
9 January 2015
List
Map
Queue
SortedMap
Iterable is in java.lang
NavigableDeque
Map
(because of its intimate
connection to for-each loops),
but Iterator is in java.util.
OSU CSE
Subsequent slides
Overview of Interfaces
discuss only certain
interfaces.
Iterable
Collection
Set
SortedSet
List
Map
Queue
SortedMap
Deque
NavigableMap
NavigableSet
9 January 2015
OSU CSE
9 January 2015
OSU CSE
OSU CSE
OSU CSE
OSU CSE
10
OSU CSE
11
OSU CSE
12
9 January 2015
OSU CSE
13
9 January 2015
OSU CSE
14
State
m = {("PB", 99),
("BK", 42),
("SA", 42)}
Set<String> s =
m.keySet();
m = {("PB", 99),
("BK", 42),
("SA", 42)}
s = {"SA", "BK",
"PB"}
9 January 2015
OSU CSE
15
State
m = {("PB", 99),
("BK", 42),
("SA", 42)}
Set<String> s =
m.keySet();
m = {("PB", 99),
("BK", 42),
("SA", 42)}
s = {"SA", "BK",
"PB"}
9 January 2015
OSU CSE
16
State
m = {("PB", 99),
("BK", 42),
("SA", 42)}
Collection<Integer> c =
m.values();
m = {("PB", 99),
("BK", 42),
("SA", 42)}
c = {42, 99, 42}
9 January 2015
OSU CSE
17
State
m = {("PB", 99),
("BK", 42)}
Set<Map.Entry<String,
Integer>> s =
m.entrySet();
m = {("PB",
("BK",
s = {("BK",
("PB",
9 January 2015
OSU CSE
99),
42)}
42),
99)}
18
9 January 2015
OSU CSE
19
Example: List<Integer> s
Code
State
s = <10, 7, 4, 2>
s.subList(1,3).clear();
s = <10, 2>
9 January 2015
OSU CSE
20
State
m = {("PB", 99),
("BK", 42),
("SA", 42)}
m.values().remove(42);
m = {("PB", 99),
("SA", 42)}
9 January 2015
OSU CSE
21
State
m = {("PB", 99),
("BK", 42),
("SA", 42)}
m.values().remove(42);
m = {("PB", 99),
("SA", 42)}
9 January 2015
OSU CSE
22
OSU CSE
23
OSU CSE
24
OSU CSE
25
9 January 2015
OSU CSE
26
Collection
Object
AbstractCollection
9 January 2015
OSU CSE
27
AbstractCollection
Has code for many methods (shared, and
possibly overridden, by all later
implementations of Collection) :
add
remove
clear
...
9 January 2015
OSU CSE
28
AbstractCollection
Has code for many methods (shared, and
possibly overridden, by all later
implementations of Collection) :
add
remove
clear
...
9 January 2015
OSU CSE
29
Collection
Object
AbstractCollection
Set
AbstractSet
HashSet
9 January 2015
TreeSet
OSU CSE
30
AbstractSet
Has code for these methods (shared, and
possibly overridden, by all later
implementations of Set):
equals
hashCode
removeAll
9 January 2015
OSU CSE
31
HashSet
Uses hashing in the Set representation
Has code for these methods (overriding
those in AbstractSet):
add
remove
clear
clone
9 January 2015
OSU CSE
32
HashSet
Uses hashing in the Set representation
Has code for these methods (overriding
those in AbstractSet):
add
remove
clear
clone
9 January 2015
OSU CSE
33
HashSet
Uses hashing in the Set representation
Has code for these methods (overriding
those in AbstractSet):
add
remove
clear
clone
9 January 2015
OSU CSE
34
TreeSet
Uses a balanced binary search tree as
the Set representation
Has code for several methods (overriding
those in AbstractSet)
9 January 2015
OSU CSE
35
Collection
Object
AbstractCollection
List
AbstractList
ArrayList
9 January 2015
LinkedList
OSU CSE
36
AbstractList
Has code for many methods (shared, and
possibly overridden, by all later
implementations of List)
Similar to AbstractSet but with code for
many more methods (because List has
many more potentially layered methods
than Set)
9 January 2015
OSU CSE
37
ArrayList
Uses arrays in the List representation
Has code for many methods (overriding
those in AbstractList)
9 January 2015
OSU CSE
38
LinkedList
Uses a doubly-linked list as the List
representation
Has code for many methods (overriding
those in AbstractList)
There is even more detail to the interfaces
and abstract classes related to
LinkedList, which you can look up if
interested
9 January 2015
OSU CSE
39
Object
AbstractMap
HashMap
9 January 2015
TreeMap
OSU CSE
40
AbstractMap
Has code for many methods (shared, and
possibly overridden, by all later
implementations of Map)
Similar to AbstractSet but with code for
many more methods (because Map has
many more potentially layered methods
than Set)
9 January 2015
OSU CSE
41
HashMap
Uses hashing in the Map representation
Has code for many methods (overriding
those in AbstractMap)
9 January 2015
OSU CSE
42
TreeMap
Uses a balanced binary search tree as
the Map representation
Has code for several methods (overriding
those in AbstractMap)
9 January 2015
OSU CSE
43
OSU CSE
44
45
OSU CSE
46
9 January 2015
OSU CSE
47
9 January 2015
OSU CSE
48
Adds the specified element to this set if it is not already present (optional operation). More formally, adds the
specified element e to this set if the set contains no element e2 such that
(e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set
unchanged and returns false. In combination with the restriction on constructors, this ensures that sets never
contain duplicate elements.
The stipulation above does not imply that sets must accept all elements; sets may refuse to add any particular
element, including null, and throw an exception, as described in the specification for Collection.add.
Individual set implementations should clearly document any restrictions on the elements that they may contain.
Throws:
UnsupportedOperationException - if the add operation is not supported by this set
ClassCastException - if the class of the specified element prevents it from being added to this set
NullPointerException - if the specified element is null and this set does not permit null elements
IllegalArgumentException - if some property of the specified element prevents it from being added to this
set
be ordered or unordered
have duplicates or not have duplicates
9 January 2015
OSU CSE
49
9 January 2015
OSU CSE
50
9 January 2015
OSU CSE
51
9 January 2015
OSU CSE
52
9 January 2015
OSU CSE
53
9 January 2015
OSU CSE
54
9 January 2015
OSU CSE
55
OSU CSE
56
9 January 2015
OSU CSE
57
9 January 2015
OSU CSE
58
9 January 2015
OSU CSE
59
OSU CSE
60
OSU CSE
61
Resources
The Collections Framework (from Oracle)
http://docs.oracle.com/javase/7/docs/technotes/guides/collections/
9 January 2015
OSU CSE
62