Do we have any other way to add an element into Map without using put() in Java? If there are other ways, how do we do it?
Map<String,String> map = new HashMap<String,String>();
map.put("hi","HI");
How can we add without using put() in this case?
Do we have any other way to add an element into Map without using put() in Java? If there are other ways, how do we do it?
Map<String,String> map = new HashMap<String,String>();
map.put("hi","HI");
How can we add without using put() in this case?
put and putAll are the only two methods for adding new hash entries in Java. If you're looking for a Ruby or Python style way of doing it such as hash['x'] = 'hello world';, then you are going to need to use a JVM language like JRuby or Groovy.
There is no way to change HashMap content without calling put() or other mutation methods. Probably, you are looking for double brace initialization. For more info, see this answer.