Class Dictionary

java.lang.Object
src.Dictionary
All Implemented Interfaces:
Cloneable

public class Dictionary extends Object implements Cloneable
Class that represents a python dictionary.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
    Dictionary(Object[][] array)
     
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the value for the key.
    void
    Clears the dictionary.
    Returns a shallow copy of the dictionary.
    Returns a shallow copy of the dictionary.
    void
    Removes the key, value pair from the dictionary.
    boolean
    Checks if two dictionaries are equal (deep).
    static Dictionary
    fromKeys(Object[] keys)
    Returns a new dictionary with keys from array and values set to None.
    static Dictionary
    fromKeys(Object[] keys, Object defaultValue)
    Returns a new dictionary with keys from array and values set to defaultValue.
    get(Object key)
    Returns the value for the key.
    get(Object key, Object defaultValue)
    Returns the value for the key, or defaultValue if not found.
    boolean
    has(Object key)
    Returns if the key exists in the dictionary.
    int
    Returns the hash code of the dictionary.
    Returns an array of key, value pairs.
    Returns an array of the keys from the dictionary.
    int
    Returns the number of items in the dictionary.
    Returns an array with the dictionary keys.
    Returns a new dictionary with the keys and values of the other two.
    pop(Object key)
    Removes the key, value pair and returns the value with the key.
    pop(Object key, Object defaultValue)
    Removes the key, value pair and return the value with the key, if not found returns defaultValue.
    Removes the last key, value pair and return the pair.
    void
    put(Object key, Object value)
    Add a key, value pair to the dictionary.
    Returns an array with the dictionary keys in reversed order of insertion.
    Return the value of the key, if it does not exist the key is inserted.
    setDefault(Object key, Object value)
    Return the value of the key, if it does not exist the key is inserted.
    Returns a string representation of the dictionary.
    void
    update(Object[][] array)
    Updates the values of the keys in the given key, value array and if does not exists, insert them.
    void
    Updates the values of the keys in the given dictionary and if does not exist, insert them.
    Returns an array of the values from the dictionary.

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Dictionary

      public Dictionary()
    • Dictionary

      public Dictionary(Object[][] array)
    • Dictionary

      public Dictionary(Dictionary dict)
  • Method Details

    • clear

      public void clear()
      Clears the dictionary.
    • copy

      public Dictionary copy() throws CloneNotSupportedException
      Returns a shallow copy of the dictionary.
      Returns:
      Copy of dictionary
      Throws:
      CloneNotSupportedException - if the dictionary cannot be cloned
    • clone

      public Dictionary clone() throws CloneNotSupportedException
      Returns a shallow copy of the dictionary.
      Overrides:
      clone in class Object
      Returns:
      Cloned dictionary
      Throws:
      CloneNotSupportedException
    • fromKeys

      public static Dictionary fromKeys(Object[] keys)
      Returns a new dictionary with keys from array and values set to None.
      Parameters:
      keys - Array of keys
      Returns:
      Dictionary with keys
    • fromKeys

      public static Dictionary fromKeys(Object[] keys, Object defaultValue)
      Returns a new dictionary with keys from array and values set to defaultValue.
      Parameters:
      keys - Array of keys
      defaultValue - Default value for keys
      Returns:
      Dictionary with keys
    • get

      public Object get(Object key) throws NonHashableKey
      Returns the value for the key.
      Parameters:
      key - Key to search
      Returns:
      Value for the key, null if not found
      Throws:
      NonHashableKey
    • access

      public Object access(Object key) throws KeyError
      Returns the value for the key.
      Parameters:
      key - Key to search
      Returns:
      Value for the key
      Throws:
      KeyError - if the key is not found
    • get

      public Object get(Object key, Object defaultValue)
      Returns the value for the key, or defaultValue if not found.
      Parameters:
      key - Key to search
      defaultValue - if key doesnt exist, return it
      Returns:
      Value for the key, defaultValue if not found
    • items

      public DictItems items()
      Returns an array of key, value pairs.
      Returns:
      an DictItems object with the dictionary's (key, value) pairs.
    • keys

      public DictKeys keys()
      Returns an array of the keys from the dictionary.
      Returns:
      a DictKeys object with the dictionary's keys.
    • values

      public DictValues values()
      Returns an array of the values from the dictionary.
      Returns:
      an array of values.
    • list

      public Object[] list()
      Returns an array with the dictionary keys.
      Returns:
      an array with the keys.
    • reversed

      public Object[] reversed()
      Returns an array with the dictionary keys in reversed order of insertion.
      Returns:
      an array with the keys.
    • pop

      public Object pop(Object key) throws KeyError
      Removes the key, value pair and returns the value with the key.
      Parameters:
      key - a key to remove.
      Returns:
      the value of the key.
      Throws:
      KeyError
    • pop

      public Object pop(Object key, Object defaultValue)
      Removes the key, value pair and return the value with the key, if not found returns defaultValue.
      Parameters:
      key - a key to remove.
      defaultValue - a default value to return if the key does not exist.
      Returns:
      the value of the key.
    • popItem

      public Object[] popItem() throws KeyError
      Removes the last key, value pair and return the pair.
      Returns:
      the last key, value pair
      Throws:
      KeyError
    • setDefault

      public Object setDefault(Object key)
      Return the value of the key, if it does not exist the key is inserted.
      Parameters:
      key - a key to get.
      Returns:
      the current value of the key.
    • setDefault

      public Object setDefault(Object key, Object value)
      Return the value of the key, if it does not exist the key is inserted.
      Parameters:
      key - a key to get.
      value - a default value to set.
      Returns:
      the current value of the key.
    • put

      public void put(Object key, Object value) throws NonHashableKey
      Add a key, value pair to the dictionary.
      Parameters:
      key - a key to add.
      value - a value for the key.
      Throws:
      NonHashableKey
    • update

      public void update(Dictionary dict)
      Updates the values of the keys in the given dictionary and if does not exist, insert them.
      Parameters:
      dict - a dictionary to merge.
    • update

      public void update(Object[][] array)
      Updates the values of the keys in the given key, value array and if does not exists, insert them.
      Parameters:
      array - an array to merge
    • has

      public boolean has(Object key)
      Returns if the key exists in the dictionary.
      Parameters:
      key - a key to search.
      Returns:
      whether the key exist in the dictionary.
    • delete

      public void delete(Object key) throws KeyError
      Removes the key, value pair from the dictionary.
      Parameters:
      key - a key to remove.
      Throws:
      KeyError - if the key is not found.
    • merge

      public Dictionary merge(Dictionary dict)
      Returns a new dictionary with the keys and values of the other two.
      Parameters:
      dict - a dictionary to merge.
      Returns:
      a new dictionary with the keys and values of the other two.
    • toString

      public String toString()
      Returns a string representation of the dictionary.
      Overrides:
      toString in class Object
      Returns:
      a string representation of the dictionary.
    • equals

      public boolean equals(Object obj)
      Checks if two dictionaries are equal (deep).
      Overrides:
      equals in class Object
      Parameters:
      obj - Object to compare
      Returns:
      true if the dictionaries are equal, false otherwise
    • hashCode

      public int hashCode()
      Returns the hash code of the dictionary.
      Overrides:
      hashCode in class Object
      Returns:
      hash code of the dictionary.
    • length

      public int length()
      Returns the number of items in the dictionary.
      Returns:
      nItems in the dictionary.