Collections Flashcards

1
Q

What classes extend List?

A

ArrayList, LinkedList

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

What classes extend Queue?

A

LinkedList, Deque

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

What classes extend Set?

A

HashSet, TreeSet

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

What classes extend Map?

A

HashMap, TreeMap

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

What is characteristic for ArrayList?

A

+ quick retrieval of items
- slow add and remove of items

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

What is characteristic for LinkedList?

A

+ quick add items to start and end
- slow retrieval via index

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

What is characteristic for Deque?

A

+ quick add items to start and end
- slow retrieval via index

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

What is characteristic for HashSet?

A

+ quick add of items and duplicate check
- sort is removed

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

What is characteristic for TreeSet?

A

+ sorting
- slow add of items

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

Objects added to TreeSet need to implement?

A

Comparable interface

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

What is characteristic for HashMap?

A

+ quick adding and retrieval of items
- no sort

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

What is characteristic for TreeMap?

A

+ sorted
- slow add and retrieval of items

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

What does equal method in Map and Set do?

A

It checks if key-value pairs in Maps are the same, or values in Set are the same between to collections. Order is not checked, except in some collections like LinkedHashMap

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

How can collections be initialized?

A

Trough construction initialization and immutable factory methods - of(), asList() and copyOf().

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

What happens if you try and change immutable collections?

A

RuntimeException is thrown

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

When initializing new ArrayList, can diamond operator be used with var type?

A

Yes, compiler implicitly adds Object to diamond operator

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

What is the rule about collections that have auto. sort?

A

They cannot contain null values

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

MAP: Map.of()

A

Creates an immutable map containing zero or a specified number of entries

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

How do we create an immutable map containing zero or a specified number of entries?

A

Map.of()

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

MAP: Map.ofEntries()

A

Creates an immutable map with multiple entries

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

How do we create an immutable map with multiple entries?

A

Map.ofEntries()

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

MAP: Map.entry()

A

Creates a map entry (key-value pair) for use in map creation methods

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

How do we create a map entry (key-value pair) for use in map creation methods?

A

Map.entry()

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

MAP: containsKey()

A

Returns true if the map contains the specified key

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
How do we check if a map contains a specific key?
containsKey()
26
MAP: containsValue()
Returns true if the map contains the specified value
27
How do we check if a map contains a specific value?
containsValue()
28
MAP: merge()
Combines two maps or adds a key-value pair if the key doesn't exist
29
How do we combine two maps or add a key-value pair if the key doesn't exist?
merge()
30
MAP: putIfAbsent()
Adds a key-value pair to the map if the key is not already present
31
How do we add a key-value pair to a map if the key is not already present?
putIfAbsent()
32
MAP: forEach()
Iterates through each entry in the map and performs a specified action
33
MAP: replace()
Replaces the entry for a key only if it's currently mapped to a specific value
33
QUEUE: element()
Retrieves, but does not remove, the head of the queue
33
How do we iterate through each entry in a map and perform a specified action?
forEach()
34
MAP: getOrDefault()
Returns the value for a given key, or a default value if the key isn't present
35
QUEUE: add()
Inserts an element at the end of the queue
35
How do we get a value for a given key, or a default value if the key isn't present?
getOrDefault()
36
How do we replace the entry for a key only if it's currently mapped to a specific value?
replace()
36
MAP: replaceAll()
Replaces all values in the map by applying a function to each entry
37
How do we replace all values in a map by applying a function to each entry?
replaceAll()
38
How do we insert an element at the end of the queue?
add()
39
How do we retrieve, but not remove, the head of the queue?
element()
40
QUEUE: remove()
Retrieves and removes the head of the queue
41
How do we retrieve and remove the head of the queue?
remove()
42
DEQUE: addFirst()
Inserts an element at the front of the deque
43
How do we insert an element at the front of the deque?
addFirst()
44
DEQUE: removeFirst()
Retrieves and removes the first element of the deque
45
How do we retrieve and remove the first element of the deque?
removeFirst()
46
DEQUE: getFirst()
Retrieves, but does not remove, the first element of the deque
46
DEQUE: addLast()
Inserts an element at the end of the deque
47
How do we retrieve, but not remove, the first element of the deque?
getFirst()
48
How do we insert an element at the end of the deque?
addLast()
48
DEQUE: removeLast()
Retrieves and removes the last element of the deque
49
How do we retrieve and remove the last element of the deque?
removeLast()
50
DEQUE: getLast()
Retrieves, but does not remove, the last element of the deque
51
What does deque's push() method do?
Adds an element to the front of the deque
52
What does deque's pop() method do?
Removes and returns the first element
53
What does deque's peek() method do?
Retrieves, but does not remove, the first element
54
LIST: add()
Appends the specified element to the end of the list
55
How do we append an element to the end of the list?
add()
56
LIST: remove()
Removes the first occurrence of the specified element from the list
57
How do we remove the first occurrence of a specified element from the list?
remove()
58
LIST: get()
Returns the element at the specified position in the list
59
How do we retrieve the element at a specific position in the list?
get()
60
LIST: replaceAll()
Replaces each element of the list with the result of applying the operator to that element
61
How do we replace all elements in the list by applying a function to each?
replaceAll()
62
LIST: set()
Replaces the element at the specified position in the list with the specified element
63
How do we replace an element at a specific position in the list?
set()
64
LIST: sort()
Sorts the list according to the order induced by the specified Comparator
65
How do we sort the elements in the list?
sort()
66
What is the difference of remove(int) and remove(Integer) methods in List?
int one removes by index, Integer one removes object from List
67
LIST: toArray(new Object[0])
Returns an array containing all of the elements in the list in proper sequence
68
How do we convert a list to an array of Objects?
toArray(new Object[0])
69
LIST: toArray()
Returns an array containing all of the elements in the list in proper sequence (element type depends on the list's type)
70
How do we convert a list to an array of its elements?
toArray()
71
What is the difference between Comparable and Comparator?
Comparable is interface that enables sorting, Comparator is used to create more complex sorting
72
What method does Comparable use?
compareTo(Object o)
73
What is the result of Comparable's compareTo method?
- Negative if instance objects is smaller than comparing object - 0 if instance object is equal to comparing object - Positive if instance object is bigger than comparing object
74
How do we sort ASC and DESC using Comparable?
ASC - instance object - comparing object DESC - comparing object - instance object
75
In what package is Comparable?
java.lang
76
How is Comparable used?
It is implemented by a class
77
What method does Comparator use?
compare(Object a, Object b)
78
How is Comparator used?
It is instantiated using Comparator.comparing()
79
What methods does Comparator also have?
comparingLong(), comparingInt(), comparingDouble(), thenComparingLong(), thenComparingInt(), thenComparingLong()
80
Where can we use Comparator?
In Collections.sort() and Collections.binarySearch()