Using Java, fill the ¨you must implement¨ spots... import java.util.Map; import
ID: 3761640 • Letter: U
Question
Using Java, fill the ¨you must implement¨ spots...
import java.util.Map;
import java.util.Iterator;
public interface HashFunction<AnyType>
{
int hashCode( AnyType x );
}
public class MyHashMap<KeyType,ValueType> implements Iterable<Map.Entry<KeyType,ValueType>>
{
private HashFunction<KeyType> hash1;
private HashFunction<KeyType> hash2;
private static class Node<KeyType,ValueType>
{
Node( KeyType k, ValueType v, Node<KeyType,ValueType> n )
{ key = k; value = v; next = n; }
public String toString( )
{ return key + "=" + value; }
KeyType key;
ValueType value;
Node<KeyType,ValueType> next;
}
public int [ ] getLengths( )
{ /* You must implement */ }
private static final int DEFAULT_ARRAY_SIZE = 11;
private Node<KeyType,ValueType> [ ] arr = null;
private int theSize = 0;
}
Explanation / Answer
public static <A, B, C> Map<A, C> transitise(Map<A, B> aToB, Map<B, C> bToC)
{
HashMap<A, C> map = new HashMap<A, C>(aToB.size() * 2);
for (Map.Entry<A, B> abEntry : aToB.entrySet())
{
map.put(abEntry.getKey(), bToC.get(abEntry.getValue()));
}
return map;
}
public static void main(String[] args)
{
HashMap<String, Integer> nameToNumber = new HashMap<String, Integer>();
nameToNumber.put("Anna", 12345);
nameToNumber.put("James", 444);
HashMap<Integer, Point> numberToPosition = new HashMap<Integer, Point>();
numberToPosition.put(12345, new Point(3, 3));
numberToPosition.put(444, new Point(1, 55));
for (Map.Entry<String, Point> nTP :
transitise(nameToNumber, numberToPosition).entrySet())
{
System.out.println(nTP.getKey() + " -> " +
nTP.getValue().x + "/" + nTP.getValue().y);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.