Chapter 18 Review

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

1. 18.21 What is a hash code?

A hash code of an object is an integer value that is characteristic of that object.

2. 18.34 How does a stream intermediate operation differ from a terminal operation?

An intermediate operation transforms a stream into another stream, while a terminal operation transforms a stream into a non-stream object or result.

7. 18.7 What is an iterator?

An iterator is an object that can move through a collection and provide access to each element of that collection.

4. 18.32 How are the keys in a SortedMap sorted?

By key order

1. 18.29 Each element in a map is comprised of what two parts?

Each element of map consists of a key and a value

3. 18.3 Describe the differences between a list and a set.

Elements are stored in a list according to position, and a list can have duplicate values. A set has no concept of position and does not allow duplicates.

16. True or False: Duplicates can be stored in a set.

False

The _______ class uses hash codes to store its elements in a way that makes searching faster.

HashSet

3. 18.31 The LinkedHashMap class allows you to retrieve mappings in what two orders?

Insertion order and access order.

6. This is an object that can be used to retrieve the elements in a collection, one after the other. 1. iterator 2. manipulator 3. comparator 4. concrete collection class

Iterator

7. ListIterator is a subinterface of 1. Iterator 2. Collection 3. Comparator 4. List

Iterator

2. 18.9 What kind of memory allocation is used by the LinkedList class?

Linked allocation.

The ________implementation performs best when many elements need to be added or removed at a position in the middle of the list.

LinkedList

3. 18.10 Why is it a common practice to use an interface variable to reference a collection object?

So you can change the class that implements the interface without having to change the rest of the code.

4. 18.11 ListIterator is a subinterface of what other JCF interface?

The Iterator interface.

6. 18.26 Which concrete set class should you use if you need to access its elements in the order that they were inserted?

The LinkedHashSet class.

12. 18.19 Which concrete list class stores its elements in a linked list? ?

The LinkedList class.

4. 18.24 What does the HashSet class's add method do when you try to add a duplicate item?

The duplicate item is not added to the set.

10. 18.17 What element does the set method (in the ListIterator interface) change?

The element returned by the last call to next() or previous().

9. 18.16 What element does the remove method (in the Iterator interface) remove?

The element returned by the last call to the next() method.

8. 18.15 Which method should you call to determine previous() without throwing an exception?

The hasPrevious() method.

2. 18.22 What method do you call to get an object's hash code?

The hashCode() method.

5. 18.25 Any time you override the Object class's equals method, what other method should you also override?

The hashCode() method.

1. 18.1 The interfaces and classes in the Java Collections Framework are part of what package?

The java.util package

2. 18.30 What part of a mapping does the HashMap store by its hash code?

The key.

15. True or False: Duplicates can be stored in a list.

True

18. True or False: A ListIterator can move backward through a list.

True

Sets are also different from lists in another way: they do not allow ______ elements. Each element in a set must be unique.

duplicate

To prevent an exception from being thrown, you should always call a ListIterator's hasNext or hasPrevious method before calling its ____ method.

set

Linked allocation makes it easy to move forward in a list; you can easily pass from one element to the next by following the ______ link.

successor

5. 18.12 When it comes to moving through the elements of a collection, describe a difference between an Iterator and a ListIterator.

A ListIterator object can move forward and backward through a collection, while an Iterator object can only move forward.

6. 18.38 What is a collector?

A collector is an object that performs a reduction on a stream.

3. 18.23 In hashing, what is a collision?

A collision occurs when two different objects hash to the same value.

8. 18.28 What is a comparator? What interface must it implement?

A comparator for a class is an object that can compare two objects of that class and determine whether they are equal, and if not, which comes before the other. A comparator implements the Comparator interface.

4. 18.36 What is a stream pipeline?

A pipeline is a sequence of operations where the output of one operation becomes the input to the next operation in the sequence.

1. 18.33 How do you define a stream of elements?

A stream is an object that permits a pipeline of operations to be performed on a sequence of elements drawn from a source.

7. 18.39 How many reduction operations can be in a stream pipeline?

A stream pipeline can have at most one reduction, which must be the last operation in the pipeline.

5. 18.37 What is the difference between a terminal operation and a reduction?

A terminal operation and a reduction are the same thing.

The ________implementation performs best when most of the elements added to the list are added at the end (the position with the highest index), and most of the elements being removed are removed from the end. T

ArrayList

7. 18.14 When stepping through the elements of a list with a list iterator's next method, how do you determine whether there are more elements in the list to visit?

By calling the hasNext() method.

6. 18.13 When you need a list iterator, how do you get one?

Call the listIterator() method.

18.8 What kind of memory allocation is used by the ArrayList class?

Contiguous allocation.

4. 18.4 A map's elements are comprised of how many parts? What are they?

Each element of a map consists of a key associated with a value.

17. True or False: Collection is a subinterface of List.

False

21. True or False: You can randomly access the elements in a linked list.

False

22. True or False: When you iterate through a HashSet, the objects will be retrieved in the same order `that they were added to the HashSet.

False

5. 18.5 The List and Set interfaces extend what other interface?

List and Set extend the Collection interface.

2. 18.2 What are the three general types of collections?

Lists, sets, and maps

13. 18.20 Which concrete list class would be a good choice to use when your application will be storing a large amount of data in a list, and a lot of insertions and/or deletions in the middle of the list are likely to take place

The LinkedList class.

3. 18.35 Name at least two different classes or interfaces that have methods for creating streams.

The Stream interface, the Collection interface, and the Arrays class all have methods for creating streams.

7. 18.27 Which concrete set class should you use if you need to retrieve its elements in their natural order?

The TreeSet class

11. 18.18 What is the difference between the size and capacity of an ArrayList?

The capacity is the length of the array that underlies the ArrayList, while the size is the number of elements currently stored.

19. True or False: A ListIterator can be used to modify the contents of a list.

True

20. True or False: When the Java compiler processes an enhanced for loop, it actually converts it to a traditional loop that uses an iterator.

True

Occasionally, a hashing algorithm will produce the same hash code for two objects that are not equal. This is called a ______.

collision

6. 18.6 Name two methods in the Iterable interface.

forEach() and iterator().

To prevent an exception from being thrown, you should always call an iterator's_______ method before calling its next method. This ensures that the next method will actually return a reference.

hasNext

A_____ _______ is an integer value that is characteristic of an object

hash code

Because collisions can occur, the HashSet performs two tests, one using the______ method and the other using the _____ method, to determine whether an object's value is already in the set.

hashCode, equals

Sets are useful when you have a _____ collection of data, and you must retrieve an element from the collection by searching for its value.

large

1. This type of collection stores elements in an ordered sequence, and allows duplicates. 1. list 2. set 3. map 4. hash set

list

4. This interface specifies the basic operations of a list. 1. ListCollection 2. List 3. Set 4. ArrayList

list

A______ will allow you to append an element at the end, add an element at the beginning, or insert an element at a specific position within the list

list

2. Each element in one of these is comprised of two parts: a key and a value. 1. list 2. set 3. map 4. hash set

map

An iterator's remove method removes the last element that was returned by the next method. The ______ method must be called before the remove method can be called.

next

A set element is not associated with a ____ within the set.

position

The ________links make it easy to move backward in the list.

predecessor

To prevent an exception from being thrown, you should always call a ListIterator's hasPrevious method before calling its ______ method.

previous

The Set interface does not add any additional methods of its own, but it does place ________ on many of the methods inherited from Collection.

restrictions

3. This type of collection is optimized for searching, stores elements in an unordered sequence, and does not allow duplicates. 1. list 2. set 3. map 4. ArrayList

set

8. This ListIterator method replaces an existing element with a new element. 1. replace 2. update 3. set 4. add

set

A ____ is a collection that lets you quickly find a value in a large amount of data. A ____ stores elements in an unordered sequence, and does not allow duplicates.

set


Ensembles d'études connexes

Microeconomics Practice Questions: Supply

View Set

Chapter 50 - International Style

View Set

Series 66: Portfolio / Fixed Income Basics (Portfolio Basics)

View Set