Question: ENTRY.JAVA CLASS * @param - type of the key * @param - type of the values associated to key */ public class Entry { private
ENTRY.JAVA CLASS
* @param
/** * @param key */ public Entry(K key) { this.setKey(key); values = new ArrayList
/** * @return the key */ public K getKey() { return key; }
/** * @param key the key to set */ public void setKey(K key) { this.key = key; } /** * values getter * @return values */ public List
MULTIMAP.JAVA CLASS
@param
/** * @return Returns true if this map contains no key-value mappings. */ public boolean isEmpty();
/** * Returns a list of values to which the specified key is mapped, or null if * this map contains no mapping for the key. Throws: NullPointerException - * if the specified key is null.IllegalStateException - if the key is not in * the multimap. * * @param key * - the key whose associated value is to be returned * @return the list of values mapped to the key or null if the map contained * no mapping for the key */ public List
/** * Replaces the entry for the specified key only if currently mapped to the * specified value. Throws: NullPointerException - if the specified key or * oldValue or newValue is null. IllegalStateException - if the key is not * in the multimap, or oldValue is not already associated with key; or if * oldValue and newValue are the same. * * @param key * - key with which the specified value is associated * @param oldValue * - value expected to be associated with the specified key * @param newValue * - value to be associated with the specified key * @return true if the value was replaced */ public boolean replace(K key, V oldValue, V newValue);
/** * Removes the mapping for a key from this map if it is present. The key and * its associated values are removed. Throws: NullPointerException - if the * specified key is null. IllegalStateException - if key is not in the * multimap * * @param key * - key whose mapping is to be removed from the multimap * @return the list of values mapped to the key or null if the map contained * no mapping for the key */ public List
/** * Removes the entry for the specified key only if it is currently mapped to * the specified value. value is removed from the list of values. If value * is the only value associated with key, then key is also removed. Throws: * NullPointerException - if the specified key or value is null. * IllegalStateException - if key is not in the multimap. * key is not removed if it is not mapped with value * * @param key * - key with which the specified value is associated * @param value * - value expected to be associated with the specified key * @return true if the value was removed. */ public boolean remove(Object key, Object value);
/** * @return the number of keys mappings in this multimap */ public int size();
}
QUESTION TO BE ANSWERED
I- Entry class
Download the starter package in eclipse. It contains two classes Multimap.java and Entry.java.
Entry.java has two data fields; key that will store the key and values (a list) that will store all the
values associated with key. For example, Allentown can be the key and the precipitations the values.
I.1 Add equals method
Before we can test the Entry class we need to add the equals method.
Add a public boolean equals(Object o)method to Entry class. You should
return true only if the keys are the same. do not compare the values.
I.2 Test Entry.java
Create EntryTest.java and test Entry.java thoroughly. Do not forget to extend TestCase.
To test Entry with the information in the table above, declare an Entry (in this case map) as
Entry
I - Implement Multimap
Create a class ListMultimap.java that implements Multimap.
Let eclipse add the unimplemented methods.
Add a ArrayList
Declare a no argument constructor and instantiate the ArrayList in it.
Create a class to ListMultimapTest.java to test ListMultimap.java.
It is good practice to test each method before implementing it
Note that eclipse will not be able to infer generic arguments in all situations especially
when manipulating the values field of an Entry object.
Do not forget to test the exceptions.
A multimap is a collection that associates more that one value to a specific key.
Lets imagine that we are trying to store the following table in a program.
Average monthly precipitation in inch. The city will be the key and the
monthly precipitation numbers the values.
| City | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec |
| Philadelphia | 3.03 | 2.64 | 3.78 | 3.54 | 3.7 | 3.43 | 4.33 | 3.5 | 3.78 | 3.19 | 2.99 | 3.54 |
| Allentown | 3.03 | 2.72 | 3.39 | 3.54 | 4.13 | 4.29 | 4.96 | 3.7 | 4.61 | 3.9 | 3.5 | 3.58 |
| Phoenix | 0.91 | 0.91 | 0.98 | 0.28 | 0.12 | 0.04 | 1.06 | 0.98 | 0.63 | 0.59 | 0.67 | 0.87 |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
