Generics and Collections Flashcards
When using primitive operands, how does the == operator determine equality?
By comparing their underlying values
Which phrase describes the compiler replacing all generic parameters with existing types that match the required bounds or Object if the type parameter is unbounded?
type erasure
Which interface provides a natural order of elements for a Map?
SortedMap
What is a raw type?
A traditional collection that uses Object to store its elements.
Which four methods will return the first element in a Deque?
pop, poll, peek, remove,
Also removeFirst, pollFirst, peekFirst, element, getFirst
Which limitation on instantiation is a direct result of type erasure?
Generic parameters cannot be directly instantiated.
Which keyword is used in conjunction with the ? character to specify a lower bound for allowable data types?
super
Which type of collection cannot contain duplicate elements?
Set
Which conversion between raw and parameterized types will generate an unchecked conversion warning?
Conversion from raw type to parameterized type
When is the diamond operator used with a constructor to infer a type for a generic parameter?
The type is explicitly specified in the variable declaration.
Which keyword is used in conjunction with the ? character to specify an upper bound for allowable data types?
extends
Which interface should be implemented to sort elements in an order other than the default order in collection?
Comparator
Which keyword applies to either an interface or a superclass when bounding generic parameters?
extends
Which operator indicates an empty set of type parameters using type inference?
Diamond (<>) operator
Which conversion does a compiler automatically provide between primitive types and corresponding object wrapper classes?
Autoboxing
When using reference operands, how does the == operator determine equality?
By comparing their object references
What is the primary storage and retrieval mechanism for a Map?
name/value pairs
Which interface should be implemented by objects that can be sorted as elements in a collection?
Comparable
Which conversion between raw and parameterized types will NOT generate an unchecked conversion warning?
Conversion from parameterized type to raw type
Which character indicates a wildcard for allowable data types?
?
What collection classes implement Deque and can be used to represent a LIFO stack?
ArrayDeque, ConcurrentLinkedDeque, LinkedBlockingDeque, LinkedList
These are preferred over Stack
LIFO
Last In First Out
ArrayDeque
Implements Deque and Queue
Resizable-array implementation of the Deque interface. Not thread safe.
ConcurrentLinkedDeque
Implements Deque and Queue
An unbounded concurrent deque based on linked nodes. Null elements not allowed
LinkedBlockingDeque
Implements BlockingQueue, Deque, Queue
An optionally-bounded blocking deque based on linked nodes.
LinkedList
Implements Deque, Queue
Doubly-linked list implementation of the List and Deque interfaces. Implements all optional list operations, and permits all elements (including null).
The add Queue method is equivalent to what Deque method?
addLast
The offer Queue method is equivalent to what Deque method?
offerLast
The remove Queue method is equivalent to what Deque method?
removeFirst
The poll Queue method is equivalent to what Deque method?
pollFirst
The element Queue method is equivalent to what Deque method?
getFirst
The peek Queue method is equivalent to what Deque method?
peekFirst
The push Stack method is equivalent to what Deque method?
addFirst
The pop Stack method is equivalent to what Deque method?
removeFirst
The peek Stack method is equivalent to what Deque method?
peekFirst
Which queue and deque methods will throw an exception if an element cannot be added due to capacity restructions?
add, addFirst, addLast, push
What exception is thrown by a class implementing Queue or Deque if a capacity restruction is violated on an insert?
IllegalStateException
Which queue and deque methods will return a special value if an element cannot be added due to capacity restructions?
offer, offerFirst, offerLast
Which blockingQueue methods will block (wait) if an element cannot be added due to capacity restructions?
put, putFirst, putLast
Which blockingQueue methods will wait for a specified time if an element cannot be added due to capacity restructions?
offer, offerFirst, offerLast
If timeout occurs a special value is returned.
Which retrieveal queue and deque methods will throw an exception if the queue is empty?
remove, removeFirst, removeLast
Which retrieveal queue and deque methods will return a special value if the queue is empty?
poll, pollFirst, pollLast
Which retrieveal queue and deque methods will wait for a value if the queue is empty?
take, takeFirst, takeLast
Which inspection queue and deque methods will throw an exception if the queue is empty?
element, getFirst, getLast
Which inspection queue and deque methods will return a special value if the queue is empty?
peek, peekFirst, peekLast
Name two classes that implement the Map interface?
AbstractMap, ConcurrentHashMap, ConcurrentSkipListMap, HashMap, Hashtable, IdentityHashMap, LinkedHashMap, TreeMap, WeakHashMap
Name two subinterfaces that extend the Map interfaces
ConcurrentMap, ConcurrentNavigableMap, NavigableMap
Can the list return from Arrays.asList can be modified?
No, The Arrays.asList method returns a List object that is backed by a fixed-length array. You cannot modify the List object returned by this array, so calling methods such as add() or remove() will result in throwing an UnsupportedOperationException.
What is a SkipList
A skip list is a data structure that allows fast search within an ordered sequence of elements. Fast search is made possible by maintaining a linked hierarchy of subsequences, each skipping over fewer elements.
What java collection or “collection like” classes implement a skiplist structure?
ConcurrentSkipListSet, ConcurrentSkipListMap
What is the difference between a LinkedListDeque and ArrayDeque?
LinkedList is maintaining nodes where the array is just using one big array. Array is better provided that it is sized correctly otherwise it has to double the size and copy if the number of elements exceeds the array size.
For TreeSet, if an iterator is created and the TreeSet ojbect is modified (insert/remove) what happens with the iterator is used?
The iterators returned by this class’s iterator method are fail-fast: if the set is modified at any time after the iterator is created, in any way except through the iterator’s own remove method, the iterator will throw a ConcurrentModificationException.
What is fail safe
TBD