Preface:
1. Declare a map: Map map = new hashmap ();
2. Place values in map. Note: map is stored in the form of key-value. For example:
Map. Put ("SA", "dd ");
3. Select string STR = map. Get ("sa"). tostring () from map. The result is str = "DD ";
4. traverse a map to obtain the key and Value
Map map = new hashmap ();
Iterator it = map. entryset (). iterator ();
While (it. hasnext ())
{
Map. Entry entry = (Map. Entry) it. Next ();
Object key = entry. getkey ();
Object value = entry. getvalue ();
}
The Java code is as follows:
package Test01;import java.util.HashMap;import java.util.Iterator;import java.util.Map;public class Test03 {public static void main(String[] args){a();b();}@SuppressWarnings("unchecked")public static void a(){@SuppressWarnings("rawtypes")Map map = new HashMap();map.put("1","aa");map.put("2","bb");map.put("3","cc");map.put("4","dd");map.put("5","ee");map.put("6","ff");map.put("7","gg");String str = map.get("5").toString();System.out.println(str);}@SuppressWarnings("unchecked")public static void b(){@SuppressWarnings("rawtypes")Map map = new HashMap();map.put("1","aa");map.put("2","bb");map.put("3","cc");map.put("4","dd");map.put("5","ee");map.put("6","ff");map.put("7","gg");@SuppressWarnings("rawtypes")Iterator it = map.entrySet().iterator() ;while (it.hasNext()){@SuppressWarnings("rawtypes")Map.Entry entry = (Map.Entry) it.next() ;Object key = entry.getKey() ;Object value = entry.getValue() ;System.out.print("["+key+"、");System.out.print(value+"]");System.out.print(",");}}}
After the code is run, the effect is as follows:
Obtain a value in the map and traverse the map to obtain the values of all keys and values in the map.