Package it.unimi.di.prog2.s12
Class StringToIntMap
java.lang.Object
it.unimi.di.prog2.s12.StringToIntMap
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Removes all of the mappings from this map.boolean
containsKey
(String key) Returns if this map contains the specified key.boolean
containsValue
(int value) Returns if this map contains the specified value.private static int
dichotomicSearch
(List<String> haystack, String needle) Finds the index (or insertion point) of a string in a list of strings kept in increasing lexicographyc order.boolean
int
Returns the value to which the specified key is mapped.int
hashCode()
boolean
isEmpty()
Returns if this map is empty.void
Associates in this map the new key with the specified value.boolean
Removes the mapping for a key from this map if it is present.int
size()
Returns the size of this map.toString()
-
Field Details
-
keys
A list containing the keys -
values
A list containing the values
-
-
Constructor Details
-
StringToIntMap
public StringToIntMap()Creates a new empty map.
-
-
Method Details
-
size
public int size()Returns the size of this map.- Returns:
- the number of key-value mappings in this map.
-
isEmpty
public boolean isEmpty()Returns if this map is empty.- Returns:
true
iff this map contains no key-value mappings.
-
dichotomicSearch
Finds the index (or insertion point) of a string in a list of strings kept in increasing lexicographyc order.If the list contains the given string, returns its index. Otherwise, returns
-(insertion_point) - 1
whereinsertion_point
is the index of the first string greater thanneedle
; note that this implies that the return value is non-negative iff the list contains the string.- Parameters:
haystack
- the notnull
list of notnull
strings in increasing degree order.needle
- the string to look for, must not benull
.- Returns:
- the index of the given string, or
-insertion_point - 1
if none is present. - See Also:
-
containsKey
Returns if this map contains the specified key.- Parameters:
key
- the key to search for.- Returns:
true
iff this map contains a key-value mappings with the givenkey
.
-
containsValue
public boolean containsValue(int value) Returns if this map contains the specified value.- Parameters:
value
- the value to search for.- Returns:
true
iff this map contains a key-value mappings with the givenvalue
.
-
get
Returns the value to which the specified key is mapped.- Parameters:
key
- the key whose associated value is to be returned.- Returns:
- the value to which the specified key is mapped.
- Throws:
NoSuchElementException
- if this map contains no mapping for the key, or the key isnull
.
-
put
Associates in this map the new key with the specified value.- Parameters:
key
- the key with which the specified value is to be associated.value
- the value to be associated with the specified key.- Throws:
IllegalArgumentException
- if the map already contain a mapping for the key.NullPointerException
- if the key isnull
.
-
remove
Removes the mapping for a key from this map if it is present.- Parameters:
key
- the key whose mapping is to be removed from the map.- Returns:
true
iff this map contained a mapping for the specified key, and hence is modified by this operation.
-
clear
public void clear()Removes all of the mappings from this map. -
equals
-
hashCode
public int hashCode() -
toString
-