笔者在对map的遍历方法分别对1000/1万/10万条数据的map进行测试时发现方案2比方案1时间上更短:
。。。。
Map<String,String[]> data = new HashMap<String, String[]>();
for (int i = 0; i < 100000; i++)
{
data.put("性能(P)"+i, new String[]{"结果查询","测量管理","阀值管理","自定义指标管理","常用选项",">>"});
}
//以上代码先构建map
方案1:
long st =System.currentTimeMillis();
for (Map.Entry<String, String[]> entry : map.entrySet())
{
for (String string : entry.getValue())
{
System.out.println(string);
}
}
long end = System.currentTimeMillis();
System.out.println("耗时:"+(end-st)/1000.0);
此方法最少的耗时为:4.125
方案2:
long st =System.currentTimeMillis();
Iterator<String> it = map.keySet().iterator();
while(it.hasNext())
{
for (String string : map.get(it.next()))
{
System.out.println(string);
}
} long end = System.currentTimeMillis();
System.out.println("耗时:"+(end-st)/1000.0);
此方案最少的耗时为:4.105