Collections Flashcards
What classes extend List?
ArrayList, LinkedList
What classes extend Queue?
LinkedList, Deque
What classes extend Set?
HashSet, TreeSet
What classes extend Map?
HashMap, TreeMap
What is characteristic for ArrayList?
+ quick retrieval of items
- slow add and remove of items
What is characteristic for LinkedList?
+ quick add items to start and end
- slow retrieval via index
What is characteristic for Deque?
+ quick add items to start and end
- slow retrieval via index
What is characteristic for HashSet?
+ quick add of items and duplicate check
- sort is removed
What is characteristic for TreeSet?
+ sorting
- slow add of items
Objects added to TreeSet need to implement?
Comparable interface
What is characteristic for HashMap?
+ quick adding and retrieval of items
- no sort
What is characteristic for TreeMap?
+ sorted
- slow add and retrieval of items
What does equal method in Map and Set do?
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 can collections be initialized?
Trough construction initialization and immutable factory methods - of(), asList() and copyOf().
What happens if you try and change immutable collections?
RuntimeException is thrown
When initializing new ArrayList, can diamond operator be used with var type?
Yes, compiler implicitly adds Object to diamond operator
What is the rule about collections that have auto. sort?
They cannot contain null values
MAP: Map.of()
Creates an immutable map containing zero or a specified number of entries
How do we create an immutable map containing zero or a specified number of entries?
Map.of()
MAP: Map.ofEntries()
Creates an immutable map with multiple entries
How do we create an immutable map with multiple entries?
Map.ofEntries()
MAP: Map.entry()
Creates a map entry (key-value pair) for use in map creation methods
How do we create a map entry (key-value pair) for use in map creation methods?
Map.entry()
MAP: containsKey()
Returns true if the map contains the specified key
How do we check if a map contains a specific key?
containsKey()
MAP: containsValue()
Returns true if the map contains the specified value
How do we check if a map contains a specific value?
containsValue()
MAP: merge()
Combines two maps or adds a key-value pair if the key doesn’t exist
How do we combine two maps or add a key-value pair if the key doesn’t exist?
merge()
MAP: putIfAbsent()
Adds a key-value pair to the map if the key is not already present
How do we add a key-value pair to a map if the key is not already present?
putIfAbsent()
MAP: forEach()
Iterates through each entry in the map and performs a specified action
MAP: replace()
Replaces the entry for a key only if it’s currently mapped to a specific value
QUEUE: element()
Retrieves, but does not remove, the head of the queue
How do we iterate through each entry in a map and perform a specified action?
forEach()
MAP: getOrDefault()
Returns the value for a given key, or a default value if the key isn’t present
QUEUE: add()
Inserts an element at the end of the queue
How do we get a value for a given key, or a default value if the key isn’t present?
getOrDefault()
How do we replace the entry for a key only if it’s currently mapped to a specific value?
replace()
MAP: replaceAll()
Replaces all values in the map by applying a function to each entry
How do we replace all values in a map by applying a function to each entry?
replaceAll()
How do we insert an element at the end of the queue?
add()
How do we retrieve, but not remove, the head of the queue?
element()
QUEUE: remove()
Retrieves and removes the head of the queue
How do we retrieve and remove the head of the queue?
remove()
DEQUE: addFirst()
Inserts an element at the front of the deque
How do we insert an element at the front of the deque?
addFirst()
DEQUE: removeFirst()
Retrieves and removes the first element of the deque
How do we retrieve and remove the first element of the deque?
removeFirst()
DEQUE: getFirst()
Retrieves, but does not remove, the first element of the deque
DEQUE: addLast()
Inserts an element at the end of the deque
How do we retrieve, but not remove, the first element of the deque?
getFirst()
How do we insert an element at the end of the deque?
addLast()
DEQUE: removeLast()
Retrieves and removes the last element of the deque
How do we retrieve and remove the last element of the deque?
removeLast()
DEQUE: getLast()
Retrieves, but does not remove, the last element of the deque
What does deque’s push() method do?
Adds an element to the front of the deque
What does deque’s pop() method do?
Removes and returns the first element
What does deque’s peek() method do?
Retrieves, but does not remove, the first element
LIST: add()
Appends the specified element to the end of the list
How do we append an element to the end of the list?
add()
LIST: remove()
Removes the first occurrence of the specified element from the list
How do we remove the first occurrence of a specified element from the list?
remove()
LIST: get()
Returns the element at the specified position in the list
How do we retrieve the element at a specific position in the list?
get()
LIST: replaceAll()
Replaces each element of the list with the result of applying the operator to that element
How do we replace all elements in the list by applying a function to each?
replaceAll()
LIST: set()
Replaces the element at the specified position in the list with the specified element
How do we replace an element at a specific position in the list?
set()
LIST: sort()
Sorts the list according to the order induced by the specified Comparator
How do we sort the elements in the list?
sort()
What is the difference of remove(int) and remove(Integer) methods in List?
int one removes by index, Integer one removes object from List
LIST: toArray(new Object[0])
Returns an array containing all of the elements in the list in proper sequence
How do we convert a list to an array of Objects?
toArray(new Object[0])
LIST: toArray()
Returns an array containing all of the elements in the list in proper sequence (element type depends on the list’s type)
How do we convert a list to an array of its elements?
toArray()
What is the difference between Comparable and Comparator?
Comparable is interface that enables sorting, Comparator is used to create more complex sorting
What method does Comparable use?
compareTo(Object o)
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
How do we sort ASC and DESC using Comparable?
ASC - instance object - comparing object
DESC - comparing object - instance object
In what package is Comparable?
java.lang
How is Comparable used?
It is implemented by a class
What method does Comparator use?
compare(Object a, Object b)
How is Comparator used?
It is instantiated using Comparator.comparing()
What methods does Comparator also have?
comparingLong(), comparingInt(), comparingDouble(), thenComparingLong(), thenComparingInt(), thenComparingLong()
Where can we use Comparator?
In Collections.sort() and Collections.binarySearch()