- package sn.len.demo;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- //HashMap推荐使用这种遍历方法,因为效率相对较高。HashTable也类似
- public class DemoList
- {
- @SuppressWarnings({ "rawtypes", "unused" })
- public static void main(String[] args)
- {
- Map<String,String> map=new HashMap<String, String>();
- map.put("1", "One");
- map.put("2", "Two");
- map.put("3", "Three");
- map.put("4", "Four");
- map.put("5", "Five");
- map.put("6", "Six");
- Iterator mapite=map.entrySet().iterator();
- while(mapite.hasNext())
- {
- Map.Entry testDemo=(Map.Entry)mapite.next();
- Object key=testDemo.getKey();
- Object value=testDemo.getValue();
- System.out.println(key+"-------"+value);
- }
- }
- }
//结果如下。
3-------Three
2-------Two
1-------One
6-------Six
5-------Five
4-------Four