Java HashMap Example with CRUD Operations
Java HashMap Example with CRUD Operations
The `search` method contributes to robustness by allowing a null check to determine whether an account exists. If the HashMap returns null for a given code, the existence of the account can be verified before further operations, thereby preventing null pointer exceptions during subsequent operations .
The presence of duplicate account numbers in the `add` method causes the HashMap to overwrite the previous entry of any duplicate key value pairs. This affects the collection's integrity and intended functionality, as critical information associated with lost entries might result in unintended data loss, misleading available data count, and errors in querying such numbers .
The `delete` method calls `HashMap`'s `remove` function with the specified account number. If the account number does not exist in the `HashMap`, the `remove` method returns `null`. In such cases, the method prints "Empno not found" indicating that the account could not be deleted because it does not exist .
Before executing the `delete` method, the size of the HashMap reflects the total number of account entries added. Post-deletion, the size property of HashMap decreases by one if the specified account number was present and successfully deleted. This change evidences mutable collection properties where operations directly affect its state .
Iterators play a crucial role in the `displayAll` method by facilitating the traversal over the set of keys obtained from the HashMap. The iterator's `hasNext` and `next` methods are used to systematically access each key and retrieve the corresponding `AccountHolder` details for display, making the process efficient and structured .
`AccountHolder` objects are displayed in a sequence determined by the HashMap's keyset iteration, which does not guarantee any specific order. This is because HashMap stores data unordered, based on hash code, and the order of iteration over the keys, therefore, depends on the internal hashing .
The primary function of the `MyHM` class is to demonstrate the use of a `HashMap` to manage `AccountHolder` objects. It adds `AccountHolder` objects to the `HashMap`, displays all of them, allows searching for a specific one by account number, and supports deletion of an entry from the `HashMap` based on the account number .
Account deletion is simulated by invoking the `remove` method of the HashMap with the account number as the key. This method removes the mapping for the specified key if present, effectively simulating the deletion of the `AccountHolder` object associated with that key .
Due to the non-sequential data storage of HashMap, data retrieval through methods like `displayAll` does not preserve any insertion order. This characteristic impacts predictability and consistency in the sequence of data presentation, as seen during display, the order of accounts like 102, 103, etc., does not match the insertion order .
The `add` method uses the `put` method of `HashMap`, which replaces the old value with the new value if the same key (account number) is used again. This means if two `AccountHolder` objects have the same account number, the latter will overwrite the former. As a result, data retrieval for the old value would return the latest entry associated with that key .