










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











Lecture 23
Just as
(^) Set
s have an ordered specialization, the
(^) SortedSet
Map
s have an ordered specialization, the
(^) SortedMap
SortedMap
(^) is much like
(^) Map
, except that it guarantees that
You have the same options with a(the values still have no order).the collection will be in order according to the order of the keys
(^) SortedMap
(^) as with a
SortedSet
(^) of using either the keys' natural ordering
( Comparable
) or an external ordering supplied to the
SortedMap
(^) constructor (
Comparator
The primary concrete implementation of
(^) SortedMap
(^) is called
TreeMap
Just like
(^) HashSet
(^) is a thin wrapper around
(^) HashMap
TreeSet
(^) is a thin wrapper around
(^) TreeMap
. The "tree" in each
the elements in the collection.name refers to the tree structure used internally for ordering
Remember this
keySet()
Set
values()
Collection
iterator()
compareTo()
compare()
Remember this
that breaks the local class.invoke. The foreign class could modify the collection in a waycontrol which of the collection's methods that class will Once you pass a collection to another class, there's no way tocollections in order to work together.On the other hand, sometimes modules need to sharechanges can be made to it.want to control changes to a collection so that only validSometimes, to ensure the correct behavior of a module, you
(^) fi rst.
(^) fi rst, so the other class can’t
change it.
references to objects, not the objects themselves.)elements in the collection. (Collections always contain Note that this only duplicates the collection and not theis to duplicate it and store the private copy.The way to maintain encapsulation with a received collectionencapsulation after it has been broken.access to the collection, there is no way of restoringcollection after it receives it, but if another class already hasThe local class may be correctly designed not to share out theafter it has been incorporated into the state of the local class.the collection allows it to make changes to the collection evenThe fact that another class potentially still has a reference tovariable, there is a likely encapsulation violation.or t wo its constructor, and intends to store it in a memberIf your class receives a collection as an argument to a method
the same sorting behavior of the originalThe underlined terms are replaced with an instance of that type. To maintain TreeMap<K, V> copy = new TreeMap(Map<K, V>);HashMap<K, V> copy = new HashMap(Map<K, V>);TreeSet
(^) TreeSet
(^) or (^) TreeMap
, you must pass
a SortedSet
(^) or (^) SortedMap
, respectively, instead of a generic
(^) Collection
.
Collection
SortedMap<K, V> readOnly =Set
SortedSet
The underlined terms are replaced with an instance of that type.wrapper will not allow extraction of the reference to the wrapped collection. You can make a collection read-only by putting it inside a wrapper class. The
the collection
wrapper
interface {
add get size set
add get size set
interface {
methods fromDifferent wrappers have different individual methods. Shown below are some The wrapper classes interact with the collections as shown in the diagram.
(^) List
. The wrapper is con
figured only to pass through certain
methods.
add get size set
add get size set
UnsupportedOperationException
the collection
wrapper
interface {interface {
set()
If (^) set()
(^) is called, the wrapper blocks the call with an exception.