Lecture 5 Flashcards
(36 cards)
What assumptions are made about collection elements?
they have a proper equals()
they have a proper hashCode() method for hashed collections
they have a proper compareTo() method for sorted collections
how does the method removeIf work with collections?
takes a predicate and removes any element if true
myList.removeIf(x->x < 0);
What exception is thrown if an illegal element is added to a collection?
ClassCastException
In a List, elements are ordered
T or F
T
In a List, elements may be duplicate
T or F
T
add() inserts a new element at the front of a List
T or F
F - at the back
How do you get List elements between certain indices?
sublist(fromIndex, toIndex)
inclusive, exclusive
what does unmodifiableList() method do?
return a read only view of a collection
what does synchronizedList() do?
return a syncrnoized, thread safe view of a collection
what does checkedList() method do?
return a dynamically type safe view of a collection
How can you give a method a variable number of arguments?
varargs:
public static int max(int … elements){
int result = Integer.MIN_VALUE;
for (int i : elements)
if (i > result) result = i;
return result
}
How are vararg parameters passed?
In an array
public static void someMethod(int … elements, int c)
^ this signature is a legal method signature in Java
T or F
False - varargs must be in last position
What does @SafeVarargs annotation do?
suppresses compiler warnings about possibly unsafe use of varargs related to subtyping
Object[] Collection.toArray() returns an array of objects that can be modified without changing the original collection
T or F
T
what does Arrays.asList(1, 2, 3); do?
Returns a List<integer> with elements 1, 2 and 3</integer>
Queues are typically LIFO (last in, first out)
T or F
F: Usually FIFO except for priority queues
In a priority queue,
peek(), element() and size() take what time?
add(), remove(), offer(), poll() take what time?
remove() and contains() take what time?
constant time
O(log(n)) time
linear time in size of queue
How do priorityqueues order elements?
Based on some priority for each element where the head has the lowest value
What is an ArrayBlockingQueue<e>?</e>
bounded FIFO queue backed by an array
bounded = can’t change size after creation
new elements inserted at end
What two blocking operations exist for an ArrayBlockingQueue?
attempts to add an element into a full queue block until the queue is no longer full
attempts to take an element from an empty queue block until the queue contains an element
A circular buffer is often implemented using an ____________
ArrayBlockingQueue
Java contains a blocking queue backed by a linked list
T or F
T
BlockingQueue implementations are never thread safe
T or F
F - guaranteed to always be thread safe