0% found this document useful (0 votes)
159 views

Hash Map in Java

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
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views

Hash Map in Java

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
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

package com.accenture.

testpack;
import java.util.*;
import com.accenture1.mypack.*;
public class MyHM
{
HashMap<Integer,AccountHolder> hm;

public static void main(String[] args)
{
new MyHM();
}
public MyHM()
{
AccountHolder h;
hm=new HashMap<Integer,AccountHolder>();
System.out.println("Hash Map object created ");
add();
displayAll();
h=search(103);
if(h==null)
{
System.out.println("Account not found");
}
else
{
System.out.println(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);
hm.put(h1.getAccno(),h1);
hm.put(h2.getAccno(),h2);
hm.put(h3.getAccno(),h3);
hm.put(h4.getAccno(),h4);
hm.put(h5.getAccno(),h5);
System.out.println("Total recordes=" +hm.size());
System.out.println("===========================");
}
public AccountHolder search(int code)
{
AccountHolder h=hm.get(code);
return h ;
}
public AccountHolder delete(int code)
{
AccountHolder h=hm.remove(code);
if(h==null)
{
System.out.println("Empno not found");
}
else
{
System.out.println(code+ " deleted");
}
System.out.println("=======================");
return h;
}
public void displayAll()
{
Set<Integer> s=hm.keySet();
Iterator<Integer> iter=s.iterator();
while(iter.hasNext())
{
int code=iter.next();
AccountHolder h=search(code);
System.out.println(h);
}
System.out.println("============================");
}
}
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 com.accenture.testpack;
import java.util.*;
import com.accenture1.mypack.*;
public class MyHM
{
HashMap<Integer,AccountHolder> hm;

public static void main(String[] args)
{
new MyHM();
}
public MyHM()
{
AccountHolder h;
hm=new HashMap<Integer,AccountHolder>();
System.out.println("Hash Map object created ");
add();
displayAll();
h=search(103);
if(h==null)
{
System.out.println("Account not found");
}
else
{
System.out.println(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);
hm.put(h1.getAccno(),h1);
hm.put(h2.getAccno(),h2);
hm.put(h3.getAccno(),h3);
hm.put(h4.getAccno(),h4);
hm.put(h5.getAccno(),h5);
System.out.println("Total recordes=" +hm.size());
System.out.println("===========================");
}
public AccountHolder search(int code)
{
AccountHolder h=hm.get(code);
return h ;
}
public AccountHolder delete(int code)
{
AccountHolder h=hm.remove(code);
if(h==null)
{
System.out.println("Empno not found");
}
else
{
System.out.println(code+ " deleted");
}
System.out.println("=======================");
return h;
}
public void displayAll()
{
Set<Integer> s=hm.keySet();
Iterator<Integer> iter=s.iterator();
while(iter.hasNext())
{
int code=iter.next();
AccountHolder h=search(code);
System.out.println(h);
}
System.out.println("============================");
}
}
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

You might also like