dietionary oey that is already in the diah ieitial specifications spocifies the
ID: 3772688 • Letter: D
Question
dietionary oey that is already in the diah ieitial specifications spocifies the public void clearO 56 CS111C Final Exam. Fall 2015 Name: 1. Write a static method named iteratorToDictionarywithCou parameter. An Iterator for objects of generic type T. It should returm an Arr has keys of type T, iter The method should accept one rayDictionary object that nts. and values of type Integer. The dictionary should have a key for each object in the rator, and the the value associated with the key should be the number of times that the value is present in the iterator. Assume that you have a working version of ArrayDictionary to use and that it implements the DictionaryInterface (p. 556).Explanation / Answer
public static <T> ArrayDictionary<T, Integer> iteratortodictionarywithcounts(Iterator<T> iterator) {
ArrayDictionary<T, Integer> countMap = null;
if (iterator != null) {
countMap = new ArrayDictionary<T, Integer>();
while (iterator.hasNext()) {
T t = iterator.next();
if (!countMap.contains(t)) {
countMap.add(t, 1);
} else {
countMap.add(t, 1 + countMap.getValue(t));
}
}
}
return countMap;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.