1. HashMap collection (The key is an Integer, the value is a case of String )
2. code example:
1 Packagecn.itcast_02;2 3 ImportJava.util.HashMap;4 ImportJava.util.Set;5 6 /*7 * hashmap<integer,string>8 * Key: Integer9 * Value: StringTen */ One Public classHashMapDemo2 { A Public Static voidMain (string[] args) { - //To create a collection object -Hashmap<integer, string> HM =NewHashmap<integer, string>(); the - //creating elements and adding elements - //integer i = new integer; - //Integer i =; + //String s = "Brigitte"; - //Hm.put (i, s); + AHm.put (27, "Brigitte"); atHm.put (30, "Wind Qing"); -Hm.put (28, "Elina"); -Hm.put (29, "Brigitte"); - - //the following notation is octal, but cannot appear with more than 8 of individual data - //hm.put (003, "Hello"); in //Hm.put (006, "Hello"); - //hm.put (007, "Hello"); to //hm.put (008, "Hello"); + - //Traverse theSet<integer> set =Hm.keyset (); * for(Integer key:set) { $String value =Hm.get (key);Panax NotoginsengSYSTEM.OUT.PRINTLN (key + "---" +value); - } the + //The following method is simply a string representation of the elements of the collection A //System.out.println ("HM:" + HM); the } +}
Run the results as follows:
Java Basic Knowledge Hardening Collection Framework note 55:map Collection HashMap collection (hashmap<integer,string>) case