entrySet 和keySet

public class MapList {
   public static void main(String[] args) {
    Map<String, Object> map = new HashMap<String,Object>();
       for(int i = 0; i < 100000; i++){
       map.put(String.valueOf(i), "java");
       }
       long startTime = System.currentTimeMillis();
       Set<Entry<String,Object>> entrySet = map.entrySet();
       for(Entry<String, Object> entry : entrySet){
        String key = entry.getKey();
        Object value = entry.getValue();
        System.out.println("key: " +key+ "  value:" + value);
       }
       long endTime = System.currentTimeMillis();
       System.out.println("===============================entrySet:"+(endTime-startTime));
      
      
       Map<String, Object> map1 = new HashMap<String,Object>();
       for(int i = 0; i < 100000; i++){
        map1.put(String.valueOf(i), "sun");
        }
       long start = System.currentTimeMillis();
       Set<String> keySet = map1.keySet();
       Iterator<String> it = keySet.iterator();
       while(it.hasNext()){
        String key = it.next();
        Object value = map1.get(key);
        System.out.println("key: " + key +"  value: " + value);
       }
       long end = System.currentTimeMillis();
       System.out.println("=================================keySet:" + (end-start));
   }
}

你可能感兴趣的:(entrySet)