Docsity

Data Structures: Understanding SortedMap and TreeMap in Java - Prof. Douglas J. Wokoun, Study notes of Computer Science

Partial preview of the text

Download Data Structures: Understanding SortedMap and TreeMap in Java - Prof. Douglas J. Wokoun and more Study notes Computer Science in PDF only on Docsity!

CS251 Intermediate Programming

Lecture 23

Data Structures

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

  1. Don’t share collections. If another class needs one of yourshared collections:There are several approaches for maintaining modularity with and data.has the data. Object orientation is about grouping functionssome way to incorporate that function into the class thatclass' collections to perform a function, see if there isn’t
  2. If you receive a collection from another class, and you need private copy of itto store it in a member variable for later use, make a

(^) fi rst.

  1. If you need to provide a collection to another class, put a read-only wrapper around it

(^) 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 copy = new TreeSet(Collection);HashSet copy = new HashSet(Collection);LinkedList copy = new LinkedList(Collection);ArrayList copy = new ArrayList(Collection); another collection: All collections classes have "copy constructors" that duplicate the contents of

(^) TreeSet

(^) or (^) TreeMap

, you must pass

a SortedSet

(^) or (^) SortedMap

, respectively, instead of a generic

(^) Collection

.

Collection readOnly = Collections.unmodifiableCollection(Collection);

SortedMap<K, V> readOnly =Set readOnly = Collections.unmodifiableSet(Set);Map<K, V> readOnly = Collections.unmodifiableMap(Map<K, V>);List readOnly = Collections.unmodifiableList(List); Collections.unmodifiableSortedMap(SortedMap<K, V>);

SortedSet readOnly = Collections.unmodifiableSortedSet(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.