Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,913 questions

51,846 answers

573 users

How to update value of HashMap using key in Java

2 Answers

0 votes
import java.util.HashMap;

public class MyClass {
    public static void main(String args[]) {
        HashMap<String, String> hm = new HashMap<>();
 
        hm.put("Java", "ABC");
        hm.put("C++", "AAB");
        hm.put("Python", "ACB");
        hm.put("C", "AAA");
        hm.put("PHP", "ACD");
 
        String value = hm.get("Python");

        value = "BBB";

        hm.put("Python", value);
        
        System.out.println(hm);
    }
}
  
  
  
  
/*
run:
    
{Java=ABC, C++=AAB, C=AAA, PHP=ACD, Python=BBB}
  
*/

 



answered Jan 21, 2022 by avibootz
0 votes
import java.util.HashMap;

public class MyClass {
    public static void main(String args[]) {
        HashMap<String, Integer> hm = new HashMap<>();
 
        hm.put("Java", 1);
        hm.put("C++", 2);
        hm.put("Python", 3);
        hm.put("C", 4);
        hm.put("PHP", 5);
 
        String key = "Python";
        
        hm.put(key, hm.get(key) + 10); 

        
        System.out.println(hm);
    }
}
  
  
  
  
/*
run:
    
{Java=1, C++=2, C=4, PHP=5, Python=13}
  
*/

 



answered Jan 21, 2022 by avibootz

Related questions

1 answer 146 views
1 answer 101 views
101 views asked Oct 24, 2023 by avibootz
1 answer 142 views
2 answers 200 views
1 answer 149 views
1 answer 201 views
...