Code:
public class MyClass {
private Map<Integer,String> myMap;
...........................
void methodFillMap(){
myMap=new HashMap<Integer, String>();
.....................
}
}
Or better like:
public class MyClass {
private Map<Integer,String> myMap=new HashMap<Integer, String>();
...........................
void methodFillMap(){
myMap.put(.....);
.....................
}
}
Are these 2 ways of creating map the same by efficiency and functionality?
No they are different functionally. In your first case every time the method methodFillMap is called a new map is created and you will lose information from the old Map whereas in your second case the object will persist with the information.