Package dsa.lab05.solutions
Class SortedArrayMap<Key extends Comparable<Key>,Value>
java.lang.Object
dsa.lab05.solutions.SortedArrayMap<Key,Value>
- Type Parameters:
Key- the key typeValue- the value type
- All Implemented Interfaces:
Container<MapItem<Key,,Value>> Map<Key,,Value> OrderedMap<Key,,Value> Iterable<MapItem<Key,Value>>
public class SortedArrayMap<Key extends Comparable<Key>,Value>
extends Object
implements OrderedMap<Key,Value>
A sorted array map.
-
Constructor Summary
ConstructorsConstructorDescriptionConstruct an empty sorted array map.SortedArrayMap(MapItem<Key, Value>... items) Construct a sorted array map containing the given items.SortedArrayMap(Iterable<MapItem<Key, Value>> items) Construct a sorted array map containing the given items.SortedArrayMap(Iterable<MapItem<Key, Value>> items, int size) Construct a sorted array map containing the given items more efficiently thanSortedArrayMap(Iterable). -
Method Summary
Modifier and TypeMethodDescriptionFind the item with the given key.voidInsert the given item.items()Get a forward iterable that yields each item once.max()Get the item with the greatest key.min()Get the item with the least key.Get the item that would be the successor of one with the given key.Get the item that would be the predecessor of one with the given key.Remove and return the item with the given key.reversed()Get a reverse iterable that yields each item once.intsize()Get the number of contained items.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface dsa.lab04.base.Map
containsKey, containsValue, get, insert, valuesMethods inherited from interface dsa.lab05.base.OrderedMap
keys, reversedKeys
-
Constructor Details
-
SortedArrayMap
public SortedArrayMap()Construct an empty sorted array map. -
SortedArrayMap
Construct a sorted array map containing the given items.- Parameters:
items- the items
-
SortedArrayMap
Construct a sorted array map containing the given items more efficiently thanSortedArrayMap(Iterable).- Parameters:
items- the itemssize- the number of items- Throws:
IllegalArgumentException- ifsize!=n(wherenisitems's size)
-
SortedArrayMap
Construct a sorted array map containing the given items.- Parameters:
items- the items
-
-
Method Details
-
size
public int size()Description copied from interface:ContainerGet the number of contained items.- Specified by:
sizein interfaceContainer<Key extends Comparable<Key>>- Returns:
- the size
-
find
Description copied from interface:MapFind the item with the given key.- Specified by:
findin interfaceMap<Key extends Comparable<Key>,Value> - Parameters:
key- the item's key- Returns:
- the item
- Throws:
NoSuchElementException- if no item has keykey
-
insert
Description copied from interface:MapInsert the given item.If there's already an item with the same key, that item is replaced with this one.
This means that if the key was not already contained, the size is incremented, otherwise it isn't.
-
remove
Description copied from interface:MapRemove and return the item with the given key.Decrements the size (assuming the item was actually in the map).
- Specified by:
removein interfaceMap<Key extends Comparable<Key>,Value> - Parameters:
key- the item's key- Returns:
- the item
- Throws:
NoSuchElementException- ifkeyis not contained
-
previous
Description copied from interface:OrderedMapGet the item that would be the predecessor of one with the given key.The map may or may not contain an item with the given key.
- Specified by:
previousin interfaceOrderedMap<Key extends Comparable<Key>,Value> - Parameters:
key- a key- Returns:
- the previous item (by key), or
nullif there is none
-
next
Description copied from interface:OrderedMapGet the item that would be the successor of one with the given key.The map may or may not contain an item with the given key.
- Specified by:
nextin interfaceOrderedMap<Key extends Comparable<Key>,Value> - Parameters:
key- a key- Returns:
- the next item (by key), or
nullif there is none
-
min
Description copied from interface:OrderedMapGet the item with the least key.- Specified by:
minin interfaceOrderedMap<Key extends Comparable<Key>,Value> - Returns:
- the minimum item (by key), or
nullif there is none
-
max
Description copied from interface:OrderedMapGet the item with the greatest key.- Specified by:
maxin interfaceOrderedMap<Key extends Comparable<Key>,Value> - Returns:
- the maximum item (by key), or
nullif there is none
-
items
Description copied from interface:OrderedMapGet a forward iterable that yields each item once.The items are iterated over in least-to-greatest order (by key).
- Specified by:
itemsin interfaceContainer<Key extends Comparable<Key>>- Specified by:
itemsin interfaceOrderedMap<Key extends Comparable<Key>,Value> - Returns:
- an iterable over the items
-
reversed
Description copied from interface:OrderedMapGet a reverse iterable that yields each item once.The items are iterated over in greatest-to-least order (by key).
- Specified by:
reversedin interfaceOrderedMap<Key extends Comparable<Key>,Value> - Returns:
- an iterable over the items
-