Answers for "replace in hashmap"

0

Java Hashmap Replace Elements

import java.util.*;  
public class hashmapeg3{  
 public static void main(String args[]){  
   HashMap<Integer,String> map=new HashMap<Integer,String>();//Creating HashMap    
   map.put(1,"Cat");  //Put elements in Map  
   map.put(2,"Dog");    
   map.put(3,"Markhor");   
   map.put(4,"Peacock");   
       
   System.out.println("HashMap after put(): ");  
   for(Map.Entry m : map.entrySet()){    
    System.out.println(m.getKey()+" "+m.getValue());    
   }  
  // change element with key 2
    map.replace(2, "Wolf");
    System.out.println("HashMap using replace(): ");
       for(Map.Entry m : map.entrySet()){    
    System.out.println(m.getKey()+" "+m.getValue());    
   } 
}  
}
Posted by: Guest on May-10-2022
0

java hashmap replace

Hashmap<String, String> map = new HashMap<>();
map.put("Hello", "you");
map.replace("Hello", "World");
map.get("Hello"); // World
Posted by: Guest on May-16-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language