map的两种迭代方法


map的两种迭代方法:
	public static void main(String[] args) {
		Map hashmap = new HashMap();
		for (int i = 0; i < 1000; i++) {
			hashmap.put("" + i, "hello" + i);
		}

		long s1 = System.currentTimeMillis();
		Iterator iterator = hashmap.keySet().iterator();
		while (iterator.hasNext()) {
			String key = (String) iterator.next();
			String value = (String) hashmap.get(key);
			System.out.println("key:" + key + " value:" + value);
		}
		long e1 = System.currentTimeMillis();

		long s2 = System.currentTimeMillis();
		Iterator iterator1 = hashmap.entrySet().iterator();
		while (iterator1.hasNext()) {
			Entry entry = (Entry) iterator1.next();
			Object value = entry.getValue();
			Object key = entry.getKey();
			System.out.println("key:" + key + " value:" + value);
		}
		long e2 = System.currentTimeMillis();
		
		System.out.println(e1-s1);
		System.out.println(e2-s2);//大部分情况下还是第二种比较快
	}


你可能感兴趣的:(object,String,HashMap,iterator)