The v parameter in the put (k, v) method of the map interface requires the object. Its general usage is as follows:
Import java. util. *; public class TestMap {public static void main (String args []) {Map m1 = new HashMap (); m1.put ("one", new Integer (1 )); // The v parameter is the Integer object m1.put ("two", new Integer (2); m1.put ("three", new Integer (3); System. out. println (m1); if (m1.containsKey ("two") {int I = (Integer) m1.get ("two ")). intValue (); // forcibly convert to an integer object to obtain the value System. out. println (I );}}}
It is convenient to use automatic packaging and package settlement:
Import java. util. *; public class TestMap {public static void main (String args []) {Map m1 = new HashMap (); m1.put ("one", 1); // automatic packaging, note the differences with the above Code: m1.put ("two", 2); m1.put ("three", 3); System. out. println (m1) if (m1.containsKey ("two") {int I = (Integer) m1.get ("two"); // automatically unpacks System. out. println (I );}}}
Topic: the existence of forced conversion causes the emergence of generics.