1.Basic java example program to remove value from hashmap.
Output:
- using Object remove(Object key) method we can remove key value pair of hashmap.
- package com.javaremovevaluehashmap;
- import java.util.Hashmap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Map.Entry;
- public class HashmapExample{
- public static void main(String[] args) {
- //create an Hashmap object
- HashMap<String, String> hashmap = new HashMap();
- //Add key values pairs to hashmap
- hashmap.put("1","One");
- hashmap.put("2","Two");
- hashmap.put("3","Three");
- hashmap.put("4","Four");
- hashmap.put("5","Five");
- hashmap.put("6","Six");
- String key=null;
- String value="java programming basics Interview questions";
- hashmap.put(key,value);
- /*
- To remove a key value pair from HashMap use
- Object remove(Object key) method of HashMap class.
- It returns either the value mapped with the key or null if no value
- was mapped.
- */
- Object object = hashmap.remove("4");
- System.out.println(object + " Removed from HashMap");
- if(!hashmap.isEmpty()){
- Iterator it=hashmap.entrySet().iterator();
- while(it.hasNext()){
- Map.Entry obj=(Entry) it.next();
- System.out.print(obj.getKey()+" ");
- System.out.println(obj.getValue());
- }
- }
- }
- }
Output:
- Four Removed from HashMap
- null java programming basics Interview questions
- 1 One
- 2 Two
- 3 Three
- 5 Five
- 6 Six
Bagikan
Java program to remove key value from hashmap
4/
5
Oleh
Kris Kimcil