Lecture 5 Flashcards
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