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);
True or False: ArrayList
By increasing its capacity once, at the start, you can prevent several reallocations later. Because reallocations are costly in terms of time, preventing unnecessary ones improves performance
True
Can maps be iterated by a for loop?
No because they do not implement the Iterable
interface. However, a collection view of a map can be obtained and iterated over by a for loop. Also, maps have a forEach(...)
method that can be used directly
Code an example of converting a Map<String, Integer>
to a List
Map<String, Integer> map = new HashMap<>(); map.put("a", 1); List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
True or False
SortedMap<K,V>
is sorted by its V
False. SortedMap<K,V>
is sorted by its K
Shallow or deep copy?
Collection<Foo> c = new ArrayList<>(); c.add(new Foo()); Collection<Foo> c2 = new ArrayList<>(c);
Shallow
True or False
Any changes made to a backing collection are visible in the view collection. Correspondingly, any changes made to the view collection, if changes are permitted, are written through to the backing collection
True
If an unmodifiable collection contains mutable elements, is the collection considered mutable or immutable?
The collection is considered mutable. For example:
List<User> users = new ArrayList<>(); User user = new User("Gianmarco"); users.add(user); List<User> unmodifiable = List.copyOf(users); System.out.println(unmodifiable); // prints [{ name='Gianmarco'}] user.setName("Jaime"); System.out.println(unmodifiable); // prints [{ name='Jaime'}]
When ArrayDeque
or LinkedList
are used as a stack, from which side are elements pushed and popped?
Elements are pushed and popped from the head of the deque
For example:
1) [ ] push 1 2) [1] push 2 3) [2, 1] pop 4) [1] pop 5) [ ]
What is printed by the following code?
Collection<Integer> c = new ArrayList<>(); c.add(1); c.add(1); c.add(2); c.add(3); Set<Integer> set = new HashSet<>(c); System.out.println(set);
[1, 2, 3]