Hash Flashcards
When adding all elements from an array to a hash? Why can’t you use the hash.addAll function?
Because it treats the primitive array as one big object. To add everything from an array to a hash, you must use a typical for loop iterator.
Why use a HashSet over a linked list?
-Linked List have a slow lookup/search
- arrays don’t have random access so accessing an element takes O(n)
Why use a boolean array?
We use a boolean array as a hashset that marks if we’ve seen that value before.
What does the Hash do to a String?
Returns an integer
For example, if we use “Nick” it might return 4 to represent the length of the name
Then we pass it to the 4th spot
Hash Function
A function that takes an input and retunrs an integer based on that input’s value
Hash Code
The value returned by a Hash Function that determines the numeric mapping of this element
HashSet
A data structure in which elements are placed in their respective array locations based on their hash code
HashSet Add
O(1)
HashSet Remove
O(1)
HashSet Contains
O(1)
How to deal with collisions in a hashset?
- ## Open Addressing: Just put it in the empty spot
How does contains work in a hashset?
- Get the location through the same hash method and see if its there
What if the contains can’t find it in our hashset?
- Get the location, if we don’t see it, keep going through the list until we find it or if we find a null, then return false because then its impossible to be in our hashset