HashMap构造函数:默认容量为16,DEFAULT_LOAD_FACTOR-扩容因子取0.75(泊松分布)。
/**
* Constructs an empty <tt>HashMap</tt> with the default initial capacity
* (16) and the default load factor (0.75).
*/
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}
ConcurrentHashMap解决HashMap不安全问题。
/**
* @author luffy
**/
public class HashMapDemo {
public static void main(String[] args){
//Map<String,String> map = new HashMap<>();
//Map<String,String> map = Collections.synchronizedMap(new HashMap<>());
Map<String,String> map = new ConcurrentHashMap<>();
for(int i =0 ;i< 30;i++){
new Thread(()->{
map.put(Thread.currentThread().getName(),UUID.randomUUID().toString().substring(0,10));
System.out.println(map);
},String.valueOf(i)).start();
}
}
}
网友评论