Chapter 22 - Lists, Stacks, Queues, and Priority Queues Flashcards
In object-oriented thinking, what is a data structure?
A data structure is a collection of data organized in some fashion. In object-oriented thinking, a data structure, also known as a container or container object, is an object that stores other objects, referred to as data or elements.
What is the Java Collections Framework?
The Java collections framework (JCF) is a set of classes and interfaces that implement commonly reusable collection data structures
The Java Collections Framework supports two types of containers. What are these two types?
One for storing a collection of elements is simply called a collection.
The other, for storing key/value pairs, is called a map.
What are the three different kinds of collections data structures?
- Sets store a group of nonduplicate elements.
- Lists store an ordered collection of elements.
- Queues store objects that are processed in first-in, first-out fashion.
Which package are the Java Collections Framework classes grouped in?
They’re all grouped in the java.util package.
What are the concrete classes in the Java Collections Framework?
TreeSet, HashSet, LinkedHashSet, Vector, Stack, ArrayList, LinkedList, and PriorityQueue
Which of the concrete classes in the Java Collections Framework implement the java.lang.Clonable interface and the java.io.Serializable interface?
All classes except java.util.PriorityQueue implement the Clonable interface.
All classes implement the Serializable interface.
What is an iterator?
Each collection has an Iterator object that can be used to traverse all the elements in the collection.
The Collection interface extends the Iterable interface. The Iterable interface defines the iterator method, which returns an iterator. Methods:
next() : gives access to next element
hasNext() : true if there are elements yet to be read
remove() : removes the last element returned by the iterator.
Describe the List interface.
The List interface extends the Collection interface and defines a collection for storing elements in a sequential order. To create a list, use on of its two concrete classes: ArrayList or LinkedList.
The List interface contains methods for storing elements in sequence, and permits duplicates.
What does the listIterator method of the List interface do?
listIterator() returns an instance of ListIterator. The ListIterator interface extends the Iterator interface to add bidirectional traversal of the list. The methods are:
add(object) : Adds the specified object to the list.
hasPrevious() : Returns true if this list iterator has more elements when traversing backward.
nextIndex() : Returns the index of the next element.
previous() : Returns the previous element in this list iterator.
previousIndex() : Returns the index of the previous element.
set() : Replaces the last element returned by the previous or next method with the specified element.
When should you use an ArrayList, and when should you use a LinkedList?
ArrayList is efficient for retrieving elements and LinkedList is efficient for inserting and removing elements at the beginning of the list. Both have the same performance for inserting and removing elements in the middle or at the end of the list. The type of work you need to do should tell you which list to use.
Does an ArrayList automatically grow/shrink?
An ArrayList automatically grows. It’s always at least as large as the list size.
An ArrayList does not automatically shrink. You can (and often should) use the trimToSize() method to reduce the array capacity to the size of the list.
If you must traverse a LinkedList, how should you do it?
The get(i) method is available for a linked list, but it is a time-consuming operation. Do not use it to traverse all the elements in a list. Instead you should use an iterator. Note that a for-each loop uses an iterator implicitly.
Do not do this:
for(int i = 0; i < linkedList.size(); i++) {
- doSomeThing linkedList.get(i);
}
Instead, do this:
for(listElementType e: linkedList) {
- doSomeThing e;
}
What is the Comparator interface?
Comparator can be used to compare the objects of a class that doesn't implement Comparable. You can define a comparator to compare the elements of different classes. To do so, define a class that implements the java.util.Comparator interface. The Comparator interface has two methods, compare and equals.
public int compare(T element1, T element2)
returns a negative value if element1 is less than element2, a positive value if element1 is greater than element2, and zero if they are equal.
public boolean equals(Object element)
Returns true if the specified object is also a comparator and imposes the same ordering as this comparator.
Explain the terms “natural order” and “comparator”.
Comparing elements using the Comparable interface is referred to as comparing using natural order, and comparing elements using the Comparator interface is referred to as comparing using comparator.
What is the java.util.Collections class for? (not the Collection interface)
The Collections class contains static methods to perform common operations in a collection and a list. For working with lists, the Collections class contains the static methods: sort, binarySearch, reverse, shuffle, copy, and fill. For working with collections, the Collections class contains the static methods: max, min, disjoint, and frequency.
How can you randomly reorder the elements of a list?
You can use the shuffle method (one of many useful methods in the Collections class) like this:
List list = Arrays.asList(“yellow”, “red”, “green”, “blue”);
Collections.shuffle(list);
The list is now randomly shuffled.
Describe the Vector and Stack classes.
The Vector and Stack classes were supported before the Java Collections Framework was introduced in Java 1.2. They contain synchronized methods for accessing data. Synchronized methods can prevent data corruption when a vector/stack is accessed and modified by two or more threads concurrently.
Except for the synchronized methods, Vector is the same as ArrayList. ArrayList is much faster if you don’t need synchronized methods.