Java program to remove key value from hashmap

1.Basic java example program to remove value from hashmap.

  • using  Object remove(Object key) method we can remove key value pair of hashmap.


  1. package com.javaremovevaluehashmap;
  2.  
  3. import java.util.Hashmap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7.  
  8. public class HashmapExample{
  9.  
  10. public static void main(String[] args) {
  11.   
  12. //create an Hashmap object
  13. HashMap<String, String> hashmap = new HashMap();
  14.        
  15. //Add key values pairs to hashmap
  16.         
  17. hashmap.put("1","One");
  18. hashmap.put("2","Two");
  19. hashmap.put("3","Three");
  20. hashmap.put("4","Four");
  21. hashmap.put("5","Five");
  22. hashmap.put("6","Six");       
  23.       String key=null;
  24.       String value="java programming basics Interview questions";   
  25. hashmap.put(key,value);
  26.  
  27. /*
  28. To remove a key value pair from HashMap use
  29. Object remove(Object key) method of HashMap class.
  30. It returns either the value mapped with the key or null if no value
  31. was mapped.    
  32.  */      
  33.  
  34.  Object object = hashmap.remove("4");
  35.         System.out.println(object + " Removed from HashMap");
  36.     
  37.  
  38. if(!hashmap.isEmpty()){
  39.  
  40. Iterator it=hashmap.entrySet().iterator();
  41.              
  42. while(it.hasNext()){

  43. Map.Entry obj=(Entry) it.next();
  44. System.out.print(obj.getKey()+" ");
  45. System.out.println(obj.getValue());
  46.              
  47. }           
  48. }   
  49.  
  50. }
  51.  
  52. }
     



Output:

  1. Four Removed from HashMap
  2. null java programming basics Interview questions
  3. 1 One
  4. 2 Two
  5. 3 Three
  6. 5 Five
  7. 6 Six

Java Basic example program to check particular value exists in hashmap

1.Basic Java example program to check particular value exists in hashmap


  1. package com.javacheckvaluehashmap;
  2.  
  3. import java.util.Hashmap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7.  
  8. public class HashmapExample{
  9.  
  10. public static void main(String[] args) {
  11.   
  12. //create an Hashmap object
  13. HashMap<String, String> hashmap = new HashMap();
  14.        
  15. //Add key values pairs to hashmap
  16.         
  17. hashmap.put("1","One");
  18. hashmap.put("2","Two");
  19. hashmap.put("3","Three");
  20. hashmap.put("4","Four");
  21. hashmap.put("5","Five");
  22. hashmap.put("6","Six");       
  23.         
  24.  
  25.  /*
  26.  To check whether a particular value exists in HashMap use
  27.  boolean containsValue(Object value) method of HashMap class.
  28. containsValue(Object value) returns true if the HashMap contains mapping for specified
  29. value otherwise false.*/
  30.        
  31. boolean isExists = hashmap.containsValue("Six");
  32. System.out.println("The value Six exists in HashMap ? : " + isExists);
  33.     
  34.  
  35. if(!hashmap.isEmpty()){
  36.  
  37. Iterator it=hashmap.entrySet().iterator();
  38.              
  39. while(it.hasNext()){

  40. Map.Entry obj=(Entry) it.next();
  41. System.out.print(obj.getKey()+" ");
  42. System.out.println(obj.getValue());
  43.              
  44. }           
  45. }   
  46.  
  47. }
  48.  
  49. }
     



Output:

  1. The value Six exists in HashMap ? : true
  2. 1 One
  3. 2 Two
  4. 3 Three
  5. 4 Four
  6. 5 Five
  7. 6 Six

check particular value exits in hashmap

Basic Java Example program Check if a particular key exists HashMap

1.Basic Java example program to check particular key exists in hashmap

  1. package com.javacheckkeyhashmap;
  2.  
  3. import java.util.Hashmap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7.  
  8. public class HashmapExample{
  9.  
  10. public static void main(String[] args) {
  11.   
  12. //create an Hashmap object
  13. HashMap<String, String> hashmap = new HashMap();
  14.        
  15. //Add key values pairs to hashmap
  16.         
  17. hashmap.put("1","One");
  18. hashmap.put("2","Two");
  19. hashmap.put("3","Three");
  20. hashmap.put("4","Four");
  21. hashmap.put("5","Five");
  22. hashmap.put("6","Six");       
  23.         
  24.  
  25.  /*
  26.   To check whether a particular key exists in HashMap use
  27.    boolean containsKey(Object key) method of HashMap class.
  28.   containsKey(Object key) returns true if the HashMap contains mapping for specified key
  29.   otherwise false.
  30.  */
  31.  
  32. boolean isExists = hashmap.containsKey("4");
  33. System.out.println("4 exists in HashMap ? : " + isExists);
  34.     
  35.  
  36. if(!hashmap.isEmpty()){
  37.  
  38. Iterator it=hashmap.entrySet().iterator();
  39.              
  40. while(it.hasNext()){

  41. Map.Entry obj=(Entry) it.next();
  42. System.out.print(obj.getKey()+" ");
  43. System.out.println(obj.getValue());
  44.              
  45. }           
  46. }   
  47.  
  48. }
  49.  
  50. }
     



Output:

  1. 4 exists in HashMap ? : true
  2. 1 One
  3. 2 Two
  4. 3 Three
  5. 4 Four
  6. 5 Five
  7. 6 Six
Select Menu