0% found this document useful (0 votes)
17 views4 pages

Chapter 22 Lists, Stacks, Queues, and Priority Queues

This document discusses Java data structures including lists, stacks, queues, and priority queues. It covers key concepts such as: - The Java Collections Framework defines common data structures like sets, lists, and maps through interfaces and classes. - ArrayList and LinkedList are both implementations of the List interface but have different performance characteristics for different operations. - PriorityQueue uses the natural ordering of elements or a custom Comparator to order elements based on priority.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Chapter 22 Lists, Stacks, Queues, and Priority Queues

This document discusses Java data structures including lists, stacks, queues, and priority queues. It covers key concepts such as: - The Java Collections Framework defines common data structures like sets, lists, and maps through interfaces and classes. - ArrayList and LinkedList are both implementations of the List interface but have different performance characteristics for different operations. - PriorityQueue uses the natural ordering of elements or a custom Comparator to order elements based on priority.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

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.

A convenience class is an abstract class that partially implements an interface. The


Java Collections Framework defines interfaces, convenience abstract classes, and
concrete classes.

3. Yes. The concrete classes of Set, List, and Map implements the clone() method in the
Cloneable interface.

4. addAll(Collection c).

5. If a method has no meaning in the subclass, you can


implement it in the subclass to throw
java.lang.UnsupportedOperationException, a subclass of
RuntimeException. This is a good design that you can use
in your project. If a method has no meaning in the
subclass, you can implement it as follows:

6. The Collection interface extends the Iterable interface. You can obtain an iterator
from a collection using the iterator() method.

7. Use the next() method.

8. Yes.

9. No. They are implicitly used in a for-each loop.

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.

11. list2 is not changed by all these methods.


What is list1 and list2 after executing set1.addAll(list2);
list1 is [red, yellow, green, red, yellow, blue]

What is list1 and list2 after executing list1.add(list2);

list1 is [red, yellow, green, [red, yellow, blue]]

What is list1 and list2 after executing list1.removeAll(list2);

list1 is [green]

What is list1 and list2 after executing list1.remove(list2);

list1 is [red, yellow, green]

What is list1 and list2 after executing list1.retainAll(list2);

list1 is [red, yellow]

What is list1 after executing list1.clear();

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.

14. A simple way to create a list from an array of objects is to use


new ArrayList(Arrays.asList(arrayObject)) or new
LinkedList(Arrays.asList(arrayObject)).

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

radius = Math.random*11 + 10;

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.

29. java.util.Queue is a subinterface of java.util.Collection, and LinkedList implements


Queue.

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.

31. new PriorityQueue(initialCapacity, Collections.reverseOrder()).

32. Yes.

33. Omitted

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy