现在的位置: 首页 > 综合 > 正文

Java HashMap遍历例子

2018年01月17日 ⁄ 综合 ⁄ 共 719字 ⁄ 字号 评论关闭

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

 

抱歉!评论已关闭.