Chapter 22 Lists, Stacks, Queues, and Priority Queues
Chapter 22 Lists, Stacks, Queues, and Priority Queues
1.
A data structure is a collection of data organized in some fashion.
In object-oriented thinking, a data structure is an object that stores other objects,
referred to as data or elements. So some people refer a data structure as a container
object or a collection object. To define a data structure is essentially to declare a
class.
2. The Java Collections Framework defines the Java API for handling common data
structures tasks in Java. It defines classes and interfaces for storing and manipulating
data in sets, lists, and maps.
3. Yes. The concrete classes of Set, List, and Map implements the clone() method in the
Cloneable interface.
4. addAll(Collection c).
6. The Collection interface extends the Iterable interface. You can obtain an iterator
from a collection using the iterator() method.
8. Yes.
10. Use the add or remove method to add or remove elements from a list. Use the
listIterator() to obtain an iterator. This iterator allows you to traverse the list bi-
directional.
list1 is [green]
list1 is empty
12. ArrayList and LinkedList can be operated similarly. The critical differences between
them are their internal implementation, which impacts the performance. ArrayList is
efficient for retrieving elements, and for adding and removing elements from the end
of the list. LinkedList is efficient for adding and removing elements anywhere in the
list.
13. All the methods in ArrayList are also in LinkedList except the trim method. The
methods getFirst, getLast, addFirst, addLast are in LinkedList, but not in ArrayList.
15.
The Comparable interface contains the compareTo method and Comparator interface
contains the compare method and equals method. Normally, if the objects of a class
have natural order (e.g., String, Date), let the class implement the Comparable
interface. The Comparator interface is more flexible in the sense that it enables you
to define a new class that contains the compare(Object, Object) method to compare
two objects of other classes.
The Comparable interface is in the java.lang package, and the Comparator interface
is in the java.util package.
16. Since The equals method is also defined in the Object class. Therefore, you will not
get a compilation error if you don’t implement the equals method in your custom
comparator class. However, in some cases implementing this method may improve
performance by allowing programs to determine that two distinct comparators impose
the same order.
17. Yes.
18. The methods for lists are: sort, binarySearch, reverse, shuffle
The methods for collections are: max, min, disjoint, frequency
19.
[blue, green, red, yellow]
[white, black, green, blue]
false
true
2
20. You can use Collections.sort(list) to sort an ArrayList or a LinkedList and use
Arrays.sort(Object[]) to sort an array of strings.
21. You can use Collections.binary(list, key) to perform binary search for an ArrayList or
a LinkedList and use Arrays.binary(Object[], key) to sort an array of strings.
22. Collections.max(Arrays.asList(arrayObject))
23. The MutilpleBallApp program will work if ArrayList is replaced by LinkedList. The
ArrayList is a better choice than the LinkedList for this program because it is more
efficient to adding and removing elements at the end of the list.
24. If you change the MutilpleBallApp program to remove the first ball in the list when
the –1 button is clicked, you should use LinkedList to store the balls in this program.
25. Change line 133 to
26. Vector is the same as ArrayList except that, except that Vector contains the
synchronized methods for accessing and modifying the vector. Since Vector
implements List, you can use the methods in List to add, remove elements from a
vector, and use the size() method to find the size of a vector. To create a vector, use
either its constructors.
27. Stack is a subclass of Vector. The Stack class represents a last-in-first-out stack of
objects. The elements are accessed only from the top of the stack. You can retrieve,
insert, or remove an element from the top of the stack. To add a new element to a
stack, use the push method. To remove an element from the top of the stack, use the
method pop. To find a stack size, use the size() method.
28. Yes, because these classes are subtypes of the Collection interface.
30. Use the constructors of PriorityQueue to create priority queues. By default, the
elements in a priority queue are ordered in their natural order using the compareTo
method in the Comparable interface. The element with the least value is assigned the
highest priority in PriorityQueue.
32. Yes.
33. Omitted