Nhu Nguyen 3222b88912 testing pr | преди 6 години | |
---|---|---|
src | преди 6 години | |
.gitignore | преди 6 години | |
README.md | преди 6 години | |
pom.xml | преди 6 години |
Purpose - to understand HashMap, objects and data encapsulation.
Objective - A map/associative array is a data type that allow users to search for a value. The objective of this lab is to understand how to create map and see how we store data affects how its performance.
By default, every Java object has an equals and a hashCode method. Java uses the object's address to calculate the hashCode. By default, two objects are equal if they have the same address. We don't want this. Two objects are equal if they are the same class and have the same values. Your task is to add the equals
and hashCode
to the User
and Cart
class.
equals
method to the User
class. The equal method should return true only if the id
and name
are the same. It return false otherwise.hashCode
method by calling Objects.hash()
and gives it the id
and name
(e.g. Objects.hash(id, name)
)equals
and hashCode
methods with IntelliJ shortcut
Command + N
and select equals and hashCode
. Follow the wizard to generate the code.Details instructions on what each method should do and return is in MyMap
class.
A list map stores every entry in a list. Every time the user calls put
, create a new Entry
and add it to the list. Your task is to make all the test pass.
BONUS: Uncomment the remove method and add the code to pass the tests.
If you still have no idea what a HashMap is, watch this video. Notice I add the bucketIndex
to help you determine where to store the entry. The bucketIndex
uses the hashCode
to try to evenly distribute the entries.
put
- find which bucket it belongs to, then add it to the list of that bucketget
- find which bucket it belongs to, then loop through the list to find the entry corresponding to the keyBONUS: Uncomment the remove method and add the code to pass the tests.
Notice how long the ListMap takes to store and find 70,000 items. Notice how much faster a HashMap is. In the MyHashMap
class, change the BUCKET_SIZE
to 100
. Run the MyHashMapTest
again. Notice how much faster it is. That's the power of designing and using a good data structure. It can speed up your program significantly.