0% found this document useful (0 votes)
2 views7 pages

Hash

The document contains a Java program demonstrating the use of a HashMap, allowing users to add, remove, and display key-value pairs. It includes a main menu for user interaction and showcases the functionality through sample outputs. The program is structured with a HashmapDemo class that manages the HashMap operations.

Uploaded by

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

Hash

The document contains a Java program demonstrating the use of a HashMap, allowing users to add, remove, and display key-value pairs. It includes a main menu for user interaction and showcases the functionality through sample outputs. The program is structured with a HashmapDemo class that manages the HashMap operations.

Uploaded by

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

Name: Atharv Mohite

Roll no:6
Batch:T1

Code of HashmapDemo:

import [Link];

import [Link];

import [Link];

public class HashmapDemo {

private HashMap<Integer, String> map;

Scanner sc = new Scanner([Link]);

public HashmapDemo() {

map = new HashMap<>();

public void addelement(int key, String value) {

[Link](key, value);

[Link]("Added = Key: " + key + ", Value: " + value);

public void removeElement(int key) {

if ([Link](key)) {

[Link](key);

[Link]("Removed element with Key = " + key);

} else {

[Link]("Key " + key + " not found!");

}
}

public void display() {

[Link]("Displaying elements:");

for ([Link]<Integer, String> entry : [Link]()) {

[Link]("Key: " + [Link]() + " → Value: " + [Link]());

Code of HashMap :

import [Link].*;

public class HashMap {

public static void main(String[] args) {

HashmapDemo demo = new HashmapDemo();

Scanner sc=new Scanner([Link]);

int ch;

do{

[Link]("---HASHMAP MEUN---");

[Link]("\[Link]\[Link]\[Link]\[Link]");

[Link]("enter your choice:");

ch=[Link]();

switch(ch){

case 1:

[Link]("\nEnter the key(int):");

int key=[Link]();

[Link]();
[Link]("\nenter the value (String):");

String value=[Link]();

[Link](key,value);

break;

case 2:

[Link]("enter the key to be removed:");

int remove = [Link]();

[Link](remove);

break;

case 3:

[Link]("Element to the hasmap:");

[Link]();

break;

case 4:

[Link]("Exit ----THANK YOU----");

break;

}while(ch!=4);

Output:

---HASHMAP MEUN---

[Link]

[Link]

[Link]
[Link]

enter your choice:

Enter the key(int):

12

enter the value (String):

Atharv

Added = Key: 12, Value: Atharv

---HASHMAP MEUN---

[Link]

[Link]

[Link]

[Link]

enter your choice:

Element to the hasmap:

Displaying elements:

Key: 12 → Value: Atharv

---HASHMAP MEUN---

[Link]

[Link]

[Link]

[Link]

enter your choice:

Enter the key(int):


22

enter the value (String):

Mohite

Added = Key: 22, Value: Mohite

---HASHMAP MEUN---

[Link]

[Link]

[Link]

[Link]

enter your choice:

Enter the key(int):

23

enter the value (String):

Hello Guys

Added = Key: 23, Value: Hello Guys

---HASHMAP MEUN---

[Link]

[Link]

[Link]

[Link]

enter your choice:

Element to the hasmap:

Displaying elements:

Key: 22 → Value: Mohite


Key: 23 → Value: Hello Guys

Key: 12 → Value: Atharv

---HASHMAP MEUN---

[Link]

[Link]

[Link]

[Link]

enter your choice:

enter the key to be removed:

12

Removed element with Key = 12

---HASHMAP MEUN---

[Link]

[Link]

[Link]

[Link]

enter your choice:

Element to the hasmap:

Displaying elements:

Key: 22 → Value: Mohite

Key: 23 → Value: Hello Guys

---HASHMAP MEUN---

[Link]

[Link]

[Link]

[Link]
enter your choice:

Exit ----THANK YOU----

You might also like