![]() The ItemTest JUnit test class where I will keep adding test cases is this. If you are new to JUnit, I suggest going through my series on JUnit. I will use JUnit to test operations on both the HashMap and Hashtable implementations. If you are overriding hashCode() you need to override the equals() method also, as present in the preceding code. This means if you have objects with the same state, you will get the same hash code. The hash code implementation generates unique hash code for each object based on their state. The class overrides the hashCode() method. The preceding code example created an Item POJO. Return (i.item.equals(em) & i.price = this.price) We will start with an Item POJO that we will store in both the implementations. ![]() Let us perform some operations on HashMap and Hashtable. Testing Operations on HashMap and Hastable If you try hashing a null key, it will throw a NullPointerException. Since null is not an object it cannot implement the methods. This is because the objects used as keys in a Hashtable implements the hashCode() and equals() methods for their storage and retrieval. On the other hand Hashtable does not allow null keys. The reason for allowing only one null key is because keys in a HashMap has to be unique. HashMap, allows storing one null key and multiple null values. In addition to these differences, one commonly asked question is Why HashMap stores one null key but Hashtable does not? However, The Enumeration returned by Hashtable doesn’t have this behaviour. Thus, in the face of concurrent modification, the iterator fails fast and cleanly, rather than risking undesirable behaviour. It throws ConcurrentModificationException if any thread other than the iterator’s remove() method tries to modify the map structurally.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |