Collections Framework Flashcards

1
Q

How can you create an unmodifiable List?

A
  1. List.of(arr)
  2. List.copyOf(collection)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the 2 most modern ways of iterating through a Collection?

A
  1. Stream API
  2. obj.forEach(...)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the 2 different ways of creating an unmodifiable Set?

A
  1. Set.of(arrays)
  2. Set.copyOf(collection)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the 3 rules for implementing Comparable<T>?

A

int obj.compareTo(T o)

  1. If obj < o, return -1
  2. If obj == o, return 0
  3. If obj > o return 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the differences between Comparable<T> and Comparator<T>?

A

Comparable<T> is implemented as a class. Comparator<T> is implemented as a lambda

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

If both Comparable<T> and Comparator<T> are used, which one will have precedence?

A

Comparator<T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

True or False

Comparable<T> or Comparator<T> should be consistent with an object’s equals() method

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between map.put(...) and map.putIfAbsent(...)?

A

map.put() will override values if key already exists. map.putIfAbsent() will not override values if key already exists

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the 10 main interfaces of the Collections Framework and their associative implementations?

A
  1. Collection<E>
    • All of the below
  2. List<E>
    • ArrayList<E>
    • LinkedList<E>
  3. Queue<E>
    • ArrayDeque<E>
    • PriorityQueue<E>
    • LinkedList<E>
  4. Deque<E>
    • ArrayDeque<E>
    • LinkedList<E>
  5. Set<E>
    • HashSet<E>
    • LinkedHashSet<E> [ordered]
    • TreeSet<E> [sorted]
    • EnumSet<E>
  6. SortedSet<E>
    • TreeSet<E> [sorted]
  7. NavigableSet<E>
    • TreeSet<E> [sorted]
  8. Map<K,V>
    • HashMap<K,V>
    • LinkedHashMap<K,V> [ordered]
    • TreeMap<K,V> [sorted]
    • EnumMap<K,V> [keys are enums]
  9. SortedMap<K,V>
    • TreeMap<K,V> [sorted]
  10. NavigableMap<K,V>
    • TreeMap<K,V> [sorted]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Are Maps collections?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a Deque?

A

Double ended queue. Meaning, insertion and deletion can be performed at both the head and tail (FIFO and LIFO)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What abstraction and implementation should be used to represent a stack?

A

Deque and ArrayDeque

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

True or False

Great care must be exercised if mutable objects are used inside collections

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can a collection be converted to a standard array?

A

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);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A

True

17
Q

Can maps be iterated by a for loop?

A

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

18
Q

Code an example of converting a Map<String, Integer> to a List

A
Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
List<Map.Entry<String, Integer>> list = new
ArrayList<>(map.entrySet());
19
Q

True or False

SortedMap<K,V> is sorted by its V

A

False. SortedMap<K,V> is sorted by its K

20
Q

Shallow or deep copy?

        Collection<Foo> c = new ArrayList<>();
        c.add(new Foo());
        Collection<Foo> c2 = new ArrayList<>(c);
A

Shallow

21
Q

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

A

True

22
Q

If an unmodifiable collection contains mutable elements, is the collection considered mutable or immutable?

A

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'}]
23
Q

When ArrayDeque or LinkedList are used as a stack, from which side are elements pushed and popped?

A

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) [ ]
24
Q

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);
A

[1, 2, 3]

25
Q

Is it possible to use mutable objects as map keys?

A

Yes. However, the behavior of a map that uses mutable objects as keys is not specified and should be seriously avoided

26
Q

What is the difference between List.copyOf(collection) and Collections.unmodifiableList(list)?

A

List.copyOf returns an unmodifiable list and Collections.unmodifiableList(list) returns an unmodifiable view of a list. This means that changes to the list backing the unmodifiable view will be visible in that view. This is not the case with List.copyOf

27
Q

Code an example of converting an array into a collection

A
Integer[] arr = { 1, 1, 2, 2 };
Set<Integer> set = new HashSet<>();
Collections.addAll(set, arr);
System.out.println(set);