Collections Framework Flashcards
How can you create an unmodifiable List
?
List.of(arr)
List.copyOf(collection)
What are the 2 most modern ways of iterating through a Collection
?
- Stream API
obj.forEach(...)
What are the 2 different ways of creating an unmodifiable Set
?
Set.of(arrays)
Set.copyOf(collection)
What are the 3 rules for implementing Comparable<T>
?
int obj.compareTo(T o)
- If obj < o, return -1
- If obj == o, return 0
- If obj > o return 1
What are the differences between Comparable<T>
and Comparator<T>
?
Comparable<T>
is implemented as a class. Comparator<T>
is implemented as a lambda
If both Comparable<T>
and Comparator<T>
are used, which one will have precedence?
Comparator<T>
True or False
Comparable<T>
or Comparator<T>
should be consistent with an object’s equals()
method
True
What is the difference between map.put(...)
and map.putIfAbsent(...)
?
map.put()
will override values if key already exists. map.putIfAbsent()
will not override values if key already exists
What are the 10 main interfaces of the Collections Framework and their associative implementations?
-
Collection<E>
- All of the below
-
List<E>
ArrayList<E>
LinkedList<E>
-
Queue<E>
ArrayDeque<E>
PriorityQueue<E>
LinkedList<E>
-
Deque<E>
ArrayDeque<E>
LinkedList<E>
-
Set<E>
HashSet<E>
-
LinkedHashSet<E>
[ordered] -
TreeSet<E>
[sorted] EnumSet<E>
-
SortedSet<E>
-
TreeSet<E>
[sorted]
-
-
NavigableSet<E>
-
TreeSet<E>
[sorted]
-
-
Map<K,V>
HashMap<K,V>
-
LinkedHashMap<K,V>
[ordered] -
TreeMap<K,V>
[sorted] -
EnumMap<K,V>
[keys are enums]
-
SortedMap<K,V>
-
TreeMap<K,V>
[sorted]
-
-
NavigableMap<K,V>
-
TreeMap<K,V>
[sorted]
-
True or False
When dealing with sorted data structures, its elements must implement Comparable<T>
or a Comparator<T>
must be provided at creation time
True
Are Maps collections?
No
What is a Deque?
Double ended queue. Meaning, insertion and deletion can be performed at both the head and tail (FIFO and LIFO)
What abstraction and implementation should be used to represent a stack?
Deque
and ArrayDeque
True or False
Great care must be exercised if mutable objects are used inside collections
True
How can a collection be converted to a standard array?
Every collection has a toArray(...)
method
Collection<Integer> c = new ArrayList<>(); c.add(1); c.add(2); c.add(3); Integer[] arr = c.toArray(new Integer[c.size()]); Arrays.stream(arr).forEach(System.out::println);