Important Flashcards
What does map.getOrDefault(k, default v) method do?
If the key exists, returns its value
Otherwise, return the default value
What does map.entrySet() do?
Returns the map as a Set view
Ie.,
HashMap: {Top=10, Shoes=20}
Set View: [Top=10, Shoes=20]
What does the toArray() method do to a LinkedList?
Converts the Linked List object into an array of object type and returns it
How do you check if a map contains a key or a value?
use map.containsKey(k)
or
map.containsValue(v)
What does the Hash Map values() method do?
Gets the value set
List 11 examples of HashMap methods
put(key, value)
get(key)
getOrDefault(key, defaultValue)
remove(key)
containsKey(key)
containsValue(value)
keySet()
entrySet()
clear()
size()
isEmpty()
How do you get the length of a string?
string.length()
How do you get a character at a given index of a string?
string.charAt(i)
How many ASCII characters are there?
There are 128 ASCII characters, but there is also an extended version of 256 ASCII characters
How many letters are in the alphabet?
26
How could you find the longest substring without repeated characters?
What is the Fibonacci sequence?
Each number is the sum of the 2 preceding numbers starting from 0 and 1
What data structure is an efficient way to get a Fibonacci number and why?
Defining a Hash Map outside the method because it can cache the results
public int fib(int n) {
if(n<-1){
return n;
}
if(map.containsKey(n)){
return map.get(n);
} else {
int result = fib(n-1) + fib(n-2);
map.put(n, result);
return result;
}
}