0% found this document useful (0 votes)
22 views1 page

HashMapDemo

Uploaded by

roshan.prajapati
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)
22 views1 page

HashMapDemo

Uploaded by

roshan.prajapati
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
You are on page 1/ 1

import java.util.

HashMap;

class HashMapDemo {
public static void main(String[] args) {

// create a hashmap
HashMap<Integer, String> languages = new HashMap<>();

System.out.println("Initial HashMap: " + numbers);


// put() method to add elements
languages.put(1, "Java");
languages.put(2, "Python");
languages.put(3, "JavaScript");
System.out.println("HashMap: " + languages);

// get() method to get value


String value = languages.get(1);
System.out.println("Value at index 1: " + value);

// return set view of keys


// using keySet()
System.out.println("Keys: " + languages.keySet());

// return set view of values


// using values()
System.out.println("Values: " + languages.values());

// return set view of key/value pairs


// using entrySet()
System.out.println("Key/Value mappings: " + languages.entrySet());

// change element with key 2


languages.replace(2, "C++");
System.out.println("HashMap using replace(): " + languages);

// remove element associated with key 2


String value = languages.remove(2);
System.out.println("Removed value: " + value);
System.out.println("Updated HashMap: " + languages);

}
}

You might also like