Class StringToIntMap

java.lang.Object
it.unimi.di.prog2.e12.StringToIntMap

public class StringToIntMap extends Object
A map from String to Integer.

A map is a collection that associates keys to values. In this case, the keys are strings and the values are integers. The map cannot contain duplicate keys, which means that each key can be associated to at most one value.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new empty map.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Removes all of the mappings from this map.
    boolean
    Returns if this map contains the specified key.
    boolean
    containsValue(int value)
    Returns if this map contains the specified value.
    int
    get(String key)
    Returns the value to which the specified key is mapped.
    boolean
    Returns if this map is empty.
    void
    put(String key, int value)
    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
    Returns the size of this map.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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.
    • containsKey

      public boolean containsKey(String key)
      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 given key.
    • 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 given value.
    • get

      public int get(String key) throws NoSuchElementException
      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 is null.
    • put

      public void put(String key, int value)
      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 is null.
    • remove

      public boolean remove(String key)
      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.