顺道看几个hashMap的问题 new HashMap(10000,0.75f),:自己设置的初始化大小为10000,负载因子是0.75. 当录入10000条数据的时候,会扩容吗? 因为自己设置的初始化大小为10000,但是HashMap底层会把初始化大小优化为大于的10000的最小的2的N次方,即2的14次方=16384.然后16384*0.75=12288>10000,所以不会扩容
public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("zhang1", 2); map.put("zhang2", 4); map.put("zhang3", 7); map.put("zhang4", 33); map.put("zhang5", 6); }Iterator遍历
public static void show3(Map<String, Integer> map) { Long start=System.currentTimeMillis(); Iterator<Map.Entry<String,Integer>> it=map.entrySet().iterator(); while (it.hasNext()){ Map.Entry<String,Integer> entry= it.next(); System.out.println(entry); } Long end=System.currentTimeMillis(); System.out.println(end-start+"2====="); }entrySet
public static void show2(Map<String, Integer> map) { for (Map.Entry<String,Integer> entry: map.entrySet()) { System.out.println("@@@@@@"+entry + "-------" ); System.out.println(entry.getKey()+"++++++++++++"+entry.getValue()); } }values遍历
public static void show1(Map<String, Integer> map) { for (Integer v : map.values()) { System.out.println(v + "-------" ); } }keySet遍历
public static void show(Map<String, Integer> map) { for (String key : map.keySet()) { System.out.println(key + "-------" + map.get(key)); } }