I NEED HELP CODING DualHashMap.java Using Java language. Your task is to write t
ID: 3817856 • Letter: I
Question
I NEED HELP CODING DualHashMap.java
Using Java language.
Your task is to write the DualHashMap class so it works in combination with the code in lab9.zip. DualHashMap should implement the DualMap interface so the main method in DualMapTest runs correctly.
The idea of the DualMap interface is to maintain a one-to-one mapping between two sets (the "keys" and the "values"). This is similar to the Map interface.
In the Map interface, each key is associated with one value, but duplicate values are allowed. For example, the keys might be id numbers and the values might be names. Two id numbers might map to the same name because two people might have the same name.
In a DualMap, duplicate values are not allowed (so not a good choice for id numbers to names). A DualMap also has a reverseGet method that returns the key for a specified value.
As with Maps, DualMaps have two generic type parameters (see the Pair2 class from the notes for an example). Your DualHashMap.java does not need to know the types for keys and values, but to work properly, both types should have an equals method and a hashcode method. You do not need to implement equals or hashcode as the types used in DualMapTest.java have these methods.
To implement DualHashMap, there should be two HashMap instance variables for the two mappings to be maintained: one from keys to values and the other from values back to keys. The constructor for DualHashMap needs to create these HashMaps. DualMap.java provides information on how the methods of a DualMap are supposed to operate. When you run DualMapTest you should get output like:
The first 10 lines of your output should be identical. The last 11 lines will very likely be different because the numbers are randomized.
lab9.zip includes:
/**
* Test the implementation of DualMap
*
*/
public class DualMapTest {
public static void main(String[] args) {
monthsTest();
daysTest();
numbersTest();
}
public static void monthsTest() {
String[] months1 = { "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November",
"December" };
String[] months2 = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec" };
DualMap<String, String> testdm = new DualHashMap<String, String>();
for (int i = 0; i < months1.length; i++) {
testdm.put(months1[i], months2[i]);
}
// This should print:
// Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
for (String s : months1) {
System.out.print(testdm.get(s) + " ");
}
System.out.println();
// This should print:
// January February March April May June July August September October
// November December
for (String s : months2) {
System.out.print(testdm.reverseGet(s) + " ");
}
System.out.println();
for (int i = 0; i < months1.length; i += 2) {
testdm.remove(months1[i], months2[i]);
}
// This should print:
// null Feb null Apr null Jun null Aug null Oct null Dec
for (String s : months1) {
System.out.print(testdm.get(s) + " ");
}
System.out.println();
// This should print:
// null February null April null June null August null October null
// December
for (String s : months2) {
System.out.print(testdm.reverseGet(s) + " ");
}
System.out.println();
}
public static void daysTest() {
String[] days1 = { "Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday" };
String[] days2 = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
String[] days3 = { "U", "M", "T", "W", "R", "F", "S" };
DualMap<String, String> testdm = new DualHashMap<String, String>();
for (int i = 0; i < days1.length; i++) {
testdm.put(days1[i], days2[i]);
testdm.put(days2[i], days3[i]);
}
// This should print:
// U M T W R F S
for (String s : days2) {
System.out.print(testdm.get(s) + " ");
}
System.out.println();
// This should print:
// Sunday Monday Tuesday Wednesday Thursday Friday Saturday
for (String s : days2) {
System.out.print(testdm.reverseGet(s) + " ");
}
System.out.println();
for (int i = 0; i < days1.length; i++) {
testdm.put(days1[i], days3[i]);
}
// This should print:
// null null null null null null null
for (String s : days2) {
System.out.print(testdm.get(s) + " ");
}
System.out.println();
// This should print:
// null null null null null null null
for (String s : days2) {
System.out.print(testdm.reverseGet(s) + " ");
}
System.out.println();
// This should print:
// U M T W R F S
for (String s : days1) {
System.out.print(testdm.get(s) + " ");
}
System.out.println();
// This should print:
// Sunday Monday Tuesday Wednesday Thursday Friday Saturday
for (String s : days3) {
System.out.print(testdm.reverseGet(s) + " ");
}
System.out.println();
}
public static void numbersTest() {
Scanner scan = null;
try {
scan = new Scanner(new File("numbers.txt"));
} catch (FileNotFoundException e) {
throw new RuntimeException("unable to open numbers.txt");
}
DualMap<Integer, List<String>> testdm = new DualHashMap<Integer, List<String>>();
while (scan.hasNextLine()) {
int n = scan.nextInt();
String s = scan.nextLine();
Scanner scanline = new Scanner(s);
List<String> list = new ArrayList<String>();
while (scanline.hasNext()) {
list.add(scanline.next());
}
scanline.close();
testdm.put(n, list);
}
scan.close();
Random random = new Random();
// should print 10 random numbers as Lists of Strings
for (int i = 0; i < 10; i++) {
System.out.println(testdm.get(random.nextInt(32769)));
}
// should print 10 semi-random numbers as Integers
for (int i = 0; i < 10; i++) {
List<String> part1 = testdm.get(1 + random.nextInt(31));
List<String> part2 = testdm.get(1 + random.nextInt(999));
// Need to create a new list to avoid damaging the Lists
// in the DualMap.
List<String> list = new ArrayList<String>();
for (String s : part1)
list.add(s);
list.add("thousand");
for (String s : part2)
list.add(s);
System.out.print(testdm.reverseGet(list) + " ");
}
System.out.println();
}
}
/**
* A DualMap maintains a one-to-one mapping between keys and value. A DualMap
* object should contain a Map from keys to values and a Map from values to
* keys. A DualMap cannot contain duplicate keys or duplicate values.
*
*/
public interface DualMap<K, V> {
/**
* Associates the specified value with the specified key in this dual map.
* After dualmap.put(key, value) is performed, then dualmap.get(key) should
* return value and dualmap.reverseGet(value) should return key.
* <p>
* If the dual map previously contained a mapping for the key or the value,
* the old association(s) should be removed.
*
* @param key
* key to be associated with the specified value.
* @param value
* value to be associated with the specified key
*/
public void put(K key, V value);
/**
* Removes the mapping from the key to the value from this dual map if it is
* present. After dualmap.put(key, value) and dualmap.remove(key, value),
* then dualmap.get(key) should return null and dualmap.reverseGet(value)
* should return null.
*
* @param key
* key whose mapping to value is to be removed
* @param value
* value whose mapping from key is to be removed
*/
public void remove(K key, V value);
/**
* Returns the value that maps from the specified key, or null if this dual
* map contains no mapping for the key.
*
* @param key
* the key that maps to the value that should be returned
* @return the value that maps from the specified key, or null if this dual
* map contains no mapping for the key
*/
public V get(K key);
/**
* Returns the key that maps to the specified value, or null if this dual
* map contains no mapping for the value.
*
* @param value
* the value that maps from the key that should be returned
* @return the key that maps to the specified value, or null if this dual
* map contains no mapping for the value
*/
public K reverseGet(V value);
}
testfile:
0 zero
1 one
2 two
3 three
4 four
5 five
6 six
7 seven
8 eight
9 nine
10 ten
11 eleven
12 twelve
13 thirteen
14 fourteen
15 fifteen
16 sixteen
17 seventeen
18 eighteen
19 nineteen
20 twenty
21 twenty-one
22 twenty-two
23 twenty-three
24 twenty-four
25 twenty-five
26 twenty-six
27 twenty-seven
28 twenty-eight
29 twenty-nine
30 thirty
Explanation / Answer
PROGRAM CODE:
package map;
import java.util.HashMap;
import java.util.Map;
public class DualHashMap<K,V> implements DualMap<K, V>{
private Map<K, V> keyMap;
private Map<V, K> valueMap;
public DualHashMap() {
keyMap = new HashMap<K, V>();
valueMap = new HashMap<V , K>();
}
@Override
public void put(K key, V value) {
if(keyMap.containsKey(key) || valueMap.containsKey(value) || keyMap.containsValue(value) || valueMap.containsValue(key))
{
remove(key, value);
}
keyMap.put(key, value);
valueMap.put(value, key);
}
@Override
public void remove(K key, V value) {
keyMap.remove(key);
valueMap.remove(value);
}
@Override
public V get(K key) {
return keyMap.get(key);
}
@Override
public K reverseGet(V value) {
return valueMap.get(value);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.