11 Collections and Maps


(a), (d), and (e)

Set, Collection and Map are core interfaces in the collections framework. LinkedList is a class that implements the List interface. There is no class or interface named Bag.


(b) and (e)

The java.util package provides map implementations named HashMap and TreeMap. It does not provide any implementations named HashList, ArraySet, and ArrayMap.



The List interface is implemented by collections that maintain sequences of possibly non-unique elements. Elements retain their ordering in the sequence. Collection classes implementing SortedSet only allow unique elements that are maintained in a sorted order.


(a) and (c)

Some operations on a collection may throw an UnsupportedOperationException. This exception type is unchecked, and the code is not required to explicitly handle unchecked exceptions. A List allows duplicate elements. An ArrayList implements a resizable array. The capacity of the array will be expanded automatically when needed. The List interface defines a get() method, but there is no method by that name in the Collection interface.



The program will compile without error, and will print all primes below 25 when run. All the collection implementations used in the program implement the Collection interface. The implementation instances are interchangeable when denoted by Collection references. None of the operations performed on the implementations will throw an UnsupportedOperationException. The program finds the primes below 25 by removing all values divisible by 2, 3, and 5 from the set of values from 2 through 25.


(a), (b), and (d)

The methods add(), retainAll(), and iterator() are defined in the Collection interface. The get() and indexOf() methods are defined in the List interface.



The remove() method removes the last element returned by either next() or previous(). The four next() calls return A, B, C, and D. D is subsequently removed. The two previous() calls return C and B. B is subsequently removed.


(b) and (d)

The methods add() and retainAll(), return the value true if the collection was modified during the operation. The contains() and containsAll() methods return a boolean value, but these membership operations never modify the current collection, and the return value indicates the result of the membership test. The clear() method does not have a return value.


(c) and (d)

The Map interface defines the methods remove() and values(). It does not define methods contains(), addAll(), and toArray(). Methods with these names are defined in the Collection interface, but Map does not inherit from Collection.


(b) and (d)

Although all the keys in a map must be unique, multiple identical values may exist. Since values are not unique, the values() method returns a Collection instance and not a Set instance. The collection objects returned by the keySet(), entrySet(), and values() methods are backed by the original Map object. This means that changes made in one are reflected in the other. Although implementations of SortedMap keep the entries sorted on the keys, this is not a requirement for classes that implement Map. For instance, the entries in a HashMap are not sorted.



[1, 3, 2] is printed. First, "1" and "2" are appended to an empty list. Next, "3" is inserted between "1" and "2", and then the list is duplicated. The original list is concatenated with the copy. The sequence of elements in the list is now "1", "3", "2", "1", "3", "2". Then a sublist view allowing access to elements from index 2 to index 5 (exclusive) is created (i.e., the subsequence "2", "1", "3"). The sublist is cleared, thus removing the elements. This is reflected in the original list and the sequence of elements is now "1", "3", "2".


(c) and (e)

The classes TreeSet and TreeMap implement the comparator() method. The comparator() method is defined in the SortedSet and SortedMap interfaces, and the TreeSet and TreeMap classes implement these interfaces.


(a) and (d)

The key of a Map.Entry cannot be changed since the key is used for locating the entry within the list. Although iterators obtained for the entry set of a map have a remove() method, the entries themselves do not. Map.Entry has a method named setValue, but its return type is Object.


(a) and (c)

(b) is eliminated since the hashCode() method cannot claim inequality if the equals() method claims equality. (d) and (e) are eliminated since the equal() method must be reflexive, and the hashCode() method must consistently return the same hash value during the execution.


(b), (d), and (e)

(a) and (c) fail to satisfy the properties of an equivalence relation. (a) is not transitive and (c) is not symmetric.



Of all the collection classes in the java.util package, only Vector and HashTable are thread-safe. The Collections class contains a static synchronizedCollectionType() method that creates thread-safe instances based on collections, which are not.


(a) and (e)

(b) is not correct since it will throw an ArithmeticException when called on a newly created Measurement object. (c) and (d) are not correct since they may return unequal hash values for two objects that are equal according to the equals() method.