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
Post a Comment