How to create a hashmap in java class -


this code. intention create hashmap 4 values, export class jar, add project, , use hashmap values there.

i'm getting error in "hmap.put". i'm unable understand i'm doing wrong. please help.

    import java.util.hashmap;      public class myfirstclass {      private hashmap<integer, string> hmap = new hashmap<integer, string>();      hmap.put(2, "jane");     hmap.put(4, "john");     hmap.put(3, "klay");     hmap.put(1, "deena");      public hashmap<integer, string> gethmap()    {         return this.hmap;    }      public void sethmap(hashmap hmap)    {         this.hmap = hmap;    }     } 

there multiple ways this. easiest 1 add brackets put statements:

import java.util.hashmap;  public class myfirstclass {     private hashmap<integer, string> hmap = new hashmap<integer, string>();      {         hmap.put(2, "jane");         hmap.put(4, "john");         hmap.put(3, "klay");         hmap.put(1, "deena");     }      public hashmap<integer, string> gethmap() {         return this.hmap;     }      public void sethmap(hashmap hmap) {         this.hmap = hmap;     } } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -