/* HashMap */ public static void hashMap(){ Map<String,String> hashMap = new HashMap<String, String>(); for(int i=0;i<100000;i++) hashMap.put(i+"", i+"v"); long time = System.currentTimeMillis(); System.out.println("==============方式1:通过遍历keySet()遍历HashMap的value"); Iterator<String> it = hashMap.keySet().iterator(); while(it.hasNext()){ hashMap.get(it.next()); //System.out.println(hashMap.get(it.next())); } System.out.println("用时:"+(System.currentTimeMillis() - time)); time = System.currentTimeMillis(); System.out.println("==============方式2:通过遍历values()遍历HashMap的value"); Collection<String> values = hashMap.values(); for(Iterator<String> valIt = values.iterator();valIt.hasNext();){ valIt.next(); } System.out.println("用时:"+(System.currentTimeMillis() - time)); time = System.currentTimeMillis(); System.out.println("==============方式3:通过entrySet().iterator()遍历HashMap的key和映射的value"); Iterator<Entry<String, String>> entryIt = hashMap.entrySet().iterator(); while(entryIt.hasNext()){ Entry<String, String> entry = entryIt.next(); entry.getKey(); entry.getValue(); //System.out.println("key:"+entry.getKey()+" value:"+entry.getValue()); } System.out.println("用时:"+(System.currentTimeMillis() - time)); }