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
Q

How do we check if a map contains a specific key?

A

containsKey()

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

MAP: containsValue()

A

Returns true if the map contains the specified value

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

How do we check if a map contains a specific value?

A

containsValue()

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

MAP: merge()

A

Combines two maps or adds a key-value pair if the key doesn’t exist

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

How do we combine two maps or add a key-value pair if the key doesn’t exist?

A

merge()

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

MAP: putIfAbsent()

A

Adds a key-value pair to the map if the key is not already present

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

How do we add a key-value pair to a map if the key is not already present?

A

putIfAbsent()

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

MAP: forEach()

A

Iterates through each entry in the map and performs a specified action

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

MAP: replace()

A

Replaces the entry for a key only if it’s currently mapped to a specific value

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

QUEUE: element()

A

Retrieves, but does not remove, the head of the queue

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

How do we iterate through each entry in a map and perform a specified action?

A

forEach()

34
Q

MAP: getOrDefault()

A

Returns the value for a given key, or a default value if the key isn’t present

35
Q

QUEUE: add()

A

Inserts an element at the end of the queue

35
Q

How do we get a value for a given key, or a default value if the key isn’t present?

A

getOrDefault()

36
Q

How do we replace the entry for a key only if it’s currently mapped to a specific value?

A

replace()

36
Q

MAP: replaceAll()

A

Replaces all values in the map by applying a function to each entry

37
Q

How do we replace all values in a map by applying a function to each entry?

A

replaceAll()

38
Q

How do we insert an element at the end of the queue?

A

add()

39
Q

How do we retrieve, but not remove, the head of the queue?

A

element()

40
Q

QUEUE: remove()

A

Retrieves and removes the head of the queue

41
Q

How do we retrieve and remove the head of the queue?

A

remove()

42
Q

DEQUE: addFirst()

A

Inserts an element at the front of the deque

43
Q

How do we insert an element at the front of the deque?

A

addFirst()

44
Q

DEQUE: removeFirst()

A

Retrieves and removes the first element of the deque

45
Q

How do we retrieve and remove the first element of the deque?

A

removeFirst()

46
Q

DEQUE: getFirst()

A

Retrieves, but does not remove, the first element of the deque

46
Q

DEQUE: addLast()

A

Inserts an element at the end of the deque

47
Q

How do we retrieve, but not remove, the first element of the deque?

A

getFirst()

48
Q

How do we insert an element at the end of the deque?

A

addLast()

48
Q

DEQUE: removeLast()

A

Retrieves and removes the last element of the deque

49
Q

How do we retrieve and remove the last element of the deque?

A

removeLast()

50
Q

DEQUE: getLast()

A

Retrieves, but does not remove, the last element of the deque

51
Q

What does deque’s push() method do?

A

Adds an element to the front of the deque

52
Q

What does deque’s pop() method do?

A

Removes and returns the first element

53
Q

What does deque’s peek() method do?

A

Retrieves, but does not remove, the first element

54
Q

LIST: add()

A

Appends the specified element to the end of the list

55
Q

How do we append an element to the end of the list?

A

add()

56
Q

LIST: remove()

A

Removes the first occurrence of the specified element from the list

57
Q

How do we remove the first occurrence of a specified element from the list?

A

remove()

58
Q

LIST: get()

A

Returns the element at the specified position in the list

59
Q

How do we retrieve the element at a specific position in the list?

A

get()

60
Q

LIST: replaceAll()

A

Replaces each element of the list with the result of applying the operator to that element

61
Q

How do we replace all elements in the list by applying a function to each?

A

replaceAll()

62
Q

LIST: set()

A

Replaces the element at the specified position in the list with the specified element

63
Q

How do we replace an element at a specific position in the list?

A

set()

64
Q

LIST: sort()

A

Sorts the list according to the order induced by the specified Comparator

65
Q

How do we sort the elements in the list?

A

sort()

66
Q

What is the difference of remove(int) and remove(Integer) methods in List?

A

int one removes by index, Integer one removes object from List

67
Q

LIST: toArray(new Object[0])

A

Returns an array containing all of the elements in the list in proper sequence

68
Q

How do we convert a list to an array of Objects?

A

toArray(new Object[0])

69
Q

LIST: toArray()

A

Returns an array containing all of the elements in the list in proper sequence (element type depends on the list’s type)

70
Q

How do we convert a list to an array of its elements?

A

toArray()

71
Q

What is the difference between Comparable and Comparator?

A

Comparable is interface that enables sorting, Comparator is used to create more complex sorting

72
Q

What method does Comparable use?

A

compareTo(Object o)

73
Q

What is the result of Comparable’s compareTo method?

A
  • 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
Q

How do we sort ASC and DESC using Comparable?

A

ASC - instance object - comparing object
DESC - comparing object - instance object

75
Q

In what package is Comparable?

A

java.lang

76
Q

How is Comparable used?

A

It is implemented by a class

77
Q

What method does Comparator use?

A

compare(Object a, Object b)

78
Q

How is Comparator used?

A

It is instantiated using Comparator.comparing()

79
Q

What methods does Comparator also have?

A

comparingLong(), comparingInt(), comparingDouble(), thenComparingLong(), thenComparingInt(), thenComparingLong()

80
Q

Where can we use Comparator?

A

In Collections.sort() and Collections.binarySearch()