0% found this document useful (0 votes)
36 views5 pages

Java HashMap Example with CRUD Operations

The document defines a MyHM class that uses a HashMap to store account holder objects with integer keys. It contains methods to add account holders to the map, search for an account by key, delete an account by key, and display all accounts. The main method creates a MyHM object that adds sample data, searches for an account, deletes it, and displays the remaining accounts.

Uploaded by

Robert Caine
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views5 pages

Java HashMap Example with CRUD Operations

The document defines a MyHM class that uses a HashMap to store account holder objects with integer keys. It contains methods to add account holders to the map, search for an account by key, delete an account by key, and display all accounts. The main method creates a MyHM object that adds sample data, searches for an account, deletes it, and displays the remaining accounts.

Uploaded by

Robert Caine
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

package [Link].

testpack;
import [Link].*;
import [Link].*;
public class MyHM
{
HashMap<Integer,AccountHolder> hm;

public static void main(String[] args)
{
new MyHM();
}
public MyHM()
{
AccountHolder h;
hm=new HashMap<Integer,AccountHolder>();
[Link]("Hash Map object created ");
add();
displayAll();
h=search(103);
if(h==null)
{
[Link]("Account not found");
}
else
{
[Link](h);
}
delete(103);
}
public void add()
{
AccountHolder h1=new AccountHolder(101,"Mahesh", 10000);
AccountHolder h2=new AccountHolder(102,"Suresh", 15000);
AccountHolder h3=new AccountHolder(103,"Jitesh", 10000);
AccountHolder h4=new AccountHolder(104,"Hitesh", 20000);
AccountHolder h5=new AccountHolder(105,"Ramesh", 12000);
[Link]([Link](),h1);
[Link]([Link](),h2);
[Link]([Link](),h3);
[Link]([Link](),h4);
[Link]([Link](),h5);
[Link]("Total recordes=" +[Link]());
[Link]("===========================");
}
public AccountHolder search(int code)
{
AccountHolder h=[Link](code);
return h ;
}
public AccountHolder delete(int code)
{
AccountHolder h=[Link](code);
if(h==null)
{
[Link]("Empno not found");
}
else
{
[Link](code+ " deleted");
}
[Link]("=======================");
return h;
}
public void displayAll()
{
Set<Integer> s=[Link]();
Iterator<Integer> iter=[Link]();
while([Link]())
{
int code=[Link]();
AccountHolder h=search(code);
[Link](h);
}
[Link]("============================");
}
}
output <<data not stored sequentially>>
102-Suresh-15000
103-Jitesh-10000
101-Mahesh-10000
104-Hitesh-20000
105-Ramesh-12000
============================
103-Jitesh-10000
103 deleted
=======================
package [Link];
import [Link].*;
import [Link].*;
public class MyHM
{
HashMap<Integer,AccountHolder> hm;

public static void main(String[] args)
{
new MyHM();
}
public MyHM()
{
AccountHolder h;
hm=new HashMap<Integer,AccountHolder>();
[Link]("Hash Map object created ");
add();
displayAll();
h=search(103);
if(h==null)
{
[Link]("Account not found");
}
else
{
[Link](h);
}
delete(103);
}
public void add()
{
AccountHolder h1=new AccountHolder(101,"Mahesh", 10000);
AccountHolder h2=new AccountHolder(102,"Suresh", 15000);
AccountHolder h3=new AccountHolder(103,"Jitesh", 10000);
AccountHolder h4=new AccountHolder(104,"Hitesh", 20000);
AccountHolder h5=new AccountHolder(102,"Ramesh", 12000);
[Link]([Link](),h1);
[Link]([Link](),h2);
[Link]([Link](),h3);
[Link]([Link](),h4);
[Link]([Link](),h5);
[Link]("Total recordes=" +[Link]());
[Link]("===========================");
}
public AccountHolder search(int code)
{
AccountHolder h=[Link](code);
return h ;
}
public AccountHolder delete(int code)
{
AccountHolder h=[Link](code);
if(h==null)
{
[Link]("Empno not found");
}
else
{
[Link](code+ " deleted");
}
[Link]("=======================");
return h;
}
public void displayAll()
{
Set<Integer> s=[Link]();
Iterator<Integer> iter=[Link]();
while([Link]())
{
int code=[Link]();
AccountHolder h=search(code);
[Link](h);
}
[Link]("============================");
}
}
output
Hash Map object created
Account holder with data created
Account holder with data created
Account holder with data created
Account holder with data created
Account holder with data created
Total recordes=4
===========================
102-Ramesh-12000
103-Jitesh-10000
101-Mahesh-10000
104-Hitesh-20000
============================
103-Jitesh-10000
103 deleted
=======================
old value is overridden

Common questions

Powered by AI

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 .

You might also like