16 Generic Collections
Objectives
In this chapter you’ll:
Learn what collections are.
Use class Arrays for array
primitive types.
Use prebuilt generic data
structures from the collections
framework.
Use various algorithms of the
Self-Review Exercises 2
Self-Review Exercises
16.1 Fill in the blanks in each of the following statements:
a) A(n) is used to iterate through a collection and can remove elements from the
collection during the iteration.
b) An element in a List can be accessed by using the element’s .
c) Assuming that myArray contains references to Double objects, occurs when
d) Class provides the capabilities of array-like data structures that can resize
e) You can use a(n) to create a collection that offers only read-only access to oth-
f) Assuming that myArray contains references to Double objects, occurs when
the statement “double number = myArray[0];” executes.
g) Collections algorithm determines if two collections have elements in com
mon.
16.2 Determine whether each statement is true or false. If false, explain why.
a) Values of primitive types may be stored directly in a collection.
b) A Set can contain duplicate values.
c) A Map can contain duplicate keys.
d) A LinkedList can contain duplicate values.
e) Collections is an interface.
f) Iterators can remove elements.
g) With hashing, as the load factor increases, the chance of collisions decreases.
h) A PriorityQueue permits null elements.
Exercises
NOTE: Solutions to the programming exercises are located in the ch16solutions folder.
Each exercise has its own folder named ex16_## where ## is a two-digit number represent-
ing the exercise number. For example, exercise 16.9’s solution is located in the folder
ex16_09.
© 2018 Pearson Education, Inc., 330 Hudson Street, NY NY 10013. All rights reserved.
16.3 Define each of the following terms:
a) Collection
b) Collections
c) Comparator
d) List
e) load factor
f) collision
g) space/time trade-off in hashing
h) HashMap
16.4 Explain why inserting additional elements into an ArrayList object whose current size is
less than its capacity is a relatively fast operation and why inserting additional elements into an
ArrayList object whose current size is at its capacity is a relatively slow operation.
16.5 Briefly answer the following questions:
a) What is the primary difference between a Set and a Map?
b) What happens when you add a primitive type (e.g., double) value to a collection?
c) Can you print all the elements in a collection without using an Iterator? If yes, how?
16.6 Explain briefly the operation of each of the following Iterator-related methods:
a) iterator
b) hasNext
c) next
Exercises 4
16.7 Explain briefly the operation of each of the following methods of class HashMap:
b) get
c) isEmpty
d) containsKey
16.8 Determine whether each of the following statements is true or false. If false, explain why.
a) Elements in a Collection must be sorted in ascending order before a binarySearch may
be performed.
b) Method first gets the first element in a TreeSet.
c) A List created with Arrays method asList is resizable.