31.java Collections Framework
31.java Collections Framework
24 April 2013!
OSU CSE!
1!
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
! There are some important differences, too, however, that deserve mention (at the end)
24 April 2013! OSU CSE! 2!
Overview of Interfaces
Iterable Collection Map
Set
List
Queue
SortedMap NavigableMap
SortedSet NavigableSet
24 April 2013!
Deque
OSU CSE!
3!
Overview of Interfaces
Iterable Collection Map
Set
List
Queue
SortedMap NavigableMap
SortedSet NavigableSet
24 April 2013!
4!
Overview of Interfaces
Iterable Collection Map
Set
List
Queue
SortedMap
SortedSet NavigableSet
24 April 2013!
Iterable is in java.lang NavigableDeque Map (because of its intimate connection to for-each loops), but Iterator is in java.util.
OSU CSE! 5!
interfaces.
Map
Collection
Set
List
Queue
SortedMap NavigableMap
SortedSet NavigableSet
24 April 2013!
Deque
OSU CSE!
6!
24 April 2013!
OSU CSE!
7!
State
m = {("PB", 99), ("BK", 42), ("SA", 42)}
24 April 2013!
OSU CSE!
19!
Example: List<Integer> s
Code State
s = <10, 7, 4, 2> s.subList(1,3).clear(); s = <10, 2>
24 April 2013!
OSU CSE!
20!
24 April 2013!
OSU CSE!
21!
State
m = {("PB", 99), ("BK", 42), ("SA", 42)}
24 April 2013!
OSU CSE!
22!
! The remove being called is optional if the object type of m.values() is a List implementation, but not if it is a Queue
! How can the client know what interface it implements?
24 April 2013! OSU CSE! 23!
! The remove being called is optional if the object type of m.values() is a List implementation, but not if it is a Queue
! How can the client know what interface it implements?
24 April 2013! OSU CSE! 24!
! The remove being called is optional if the object type of m.values() is a List implementation, but not if it is a Queue
! How can the client know what interface it implements?
24 April 2013! OSU CSE! 25!
24 April 2013!
OSU CSE!
26!
24 April 2013!
OSU CSE!
27!
AbstractCollection
! Has code for many methods (shared, and possibly overridden, by all later implementations of Collection) :
! add ! remove ! clear ! ...
24 April 2013!
OSU CSE!
28!
AbstractCollection
! Has code for many methods (shared, and possibly overridden, by all later implementations of Collection) :
! add ! remove ! clear ! ...
This methods implementation here, for example, always throws an UnsupportedOperationException.
24 April 2013!
OSU CSE!
29!
Set
HashSet
TreeSet
24 April 2013!
OSU CSE!
30!
AbstractSet
! Has code for these methods (shared, and possibly overridden, by all later implementations of Set):
! equals ! hashCode ! removeAll
24 April 2013!
OSU CSE!
31!
HashSet
! Uses hashing in the Set representation ! Has code for these methods (overriding those in AbstractSet):
! add ! remove ! clear ! clone
24 April 2013!
OSU CSE!
32!
HashSet
! Uses hashing in the Set representation ! Has code for these methods (overriding those in AbstractSet):
! add ! remove ! clear ! clone
The first three methods, though optional, are implemented here and do what you should expect.
24 April 2013!
OSU CSE!
33!
HashSet
! Uses hashing in the Set representation ! Has code for these methods (overriding those in AbstractSet):
! add ! remove ! clear ! clone
The clone method makes a shallow copy, i.e., the elements are not cloned; which raises many questions. Best practice: do not use it!
24 April 2013!
OSU CSE!
34!
TreeSet
! Uses a balanced binary search tree as the Set representation ! Has code for several methods (overriding those in AbstractSet)
24 April 2013!
OSU CSE!
35!
List
ArrayList
LinkedList
24 April 2013!
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)
24 April 2013!
OSU CSE!
37!
ArrayList
! Uses arrays in the List representation ! Has code for many methods (overriding those in AbstractList)
24 April 2013!
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
24 April 2013! OSU CSE! 39!
AbstractMap
HashMap
TreeMap
24 April 2013!
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)
24 April 2013!
OSU CSE!
41!
HashMap
! Uses hashing in the Map representation ! Has code for many methods (overriding those in AbstractMap)
24 April 2013!
OSU CSE!
42!
TreeMap
! Uses a balanced binary search tree as the Map representation ! Has code for several methods (overriding those in AbstractMap)
24 April 2013!
OSU CSE!
43!
Notice that the class Collections is different from the interface Collection, and in particular it does not implement that interface!
OSU CSE! 45!
24 April 2013!
OSU CSE!
47!
24 April 2013!
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
! JCF descriptions and contracts use similar terms, though; e.g.,collections may:
! be ordered or unordered ! have duplicates or not have duplicates
24 April 2013!
OSU CSE!
49!
24 April 2013!
OSU CSE!
50!
24 April 2013!
OSU CSE!
51!
24 April 2013!
24 April 2013!
OSU CSE!
53!
24 April 2013!
OSU CSE!
54!
24 April 2013!
OSU CSE!
55!
24 April 2013!
OSU CSE!
57!
24 April 2013!
OSU CSE!
58!
24 April 2013!
OSU CSE!
59!
Resources
! The Collections Framework (from Oracle)
! http://docs.oracle.com/javase/7/docs/technotes/guides/collections/ index.html
24 April 2013!
OSU CSE!
62!