/// <summary> /// Gets a value from the map given a key. /// </summary> /// <param name="the_key">the key to search with.</param> /// <returns>the value linked to the key, or null if the key does not exist.</returns> public V get(K the_key) { //look for the element TreeKeyValue <K, V> entry = my_keys.getElement(new TreeKeyValue <K, V>(the_key, null, null)); if (entry == null) { return(null); } else { return(entry.value); } }
/// <summary> /// Gets a collection of values in the map. /// </summary> /// <returns>a collection of values.</returns> public Collection <V> values() { Collection <V> return_value = new ArrayList <V>(); Iterator <TreeKeyValue <K, V> > it = my_keys.iterator(); //add each value to the collection while (it.hasNext()) { TreeKeyValue <K, V> entry = it.next(); return_value.add(entry.value); } return(return_value); }
/// <summary> /// Gets the set of keys in the map. /// </summary> /// <returns>a set of keys.</returns> public Set <K> keyset() { Set <K> return_value = new TreeSet <K>(); Iterator <TreeKeyValue <K, V> > it = my_keys.iterator(); //add each key to the set while (it.hasNext()) { TreeKeyValue <K, V> entry = it.next(); return_value.add(entry.key); } return(return_value); }
/// <summary> /// Puts a key/value pair in the map. /// </summary> /// <param name="the_key">the key.</param> /// <param name="the_value">the value.</param> /// <returns>the value that was previously associated with the key, otherwise null if /// no key/value pair was in the map before this method call.</returns> public V put(K the_key, V the_value) { //get the element that was previously in this tree location,add the new element and return the old TreeKeyValue <K, V> entry = my_keys.getElement(new TreeKeyValue <K, V>(the_key, the_value, null)); my_keys.add(new TreeKeyValue <K, V>(the_key, the_value, my_comparator)); //add the comparator if given if (entry == null) { return(null); } else { return(entry.value); } }
/// <summary> /// Removes a key/value pair from the map. /// </summary> /// <param name="the_key">the key to search with.</param> /// <returns>a previous value associated with the key, or null if no key/value pair existed.</returns> public V remove(K the_key) { //get the element that is currently in the tree location and then remove the element and return it TreeKeyValue <K, V> find = new TreeKeyValue <K, V>(the_key, null, null); TreeKeyValue <K, V> entry = my_keys.getElement(find); my_keys.remove(find); if (entry == null) { return(null); } else { return(entry.value); } }