HashMap-HashTable-ConcurrentHashMap
HashTable线程安全的HashMap。对读写方法都使用了同步锁。
ConcurrentHashMap也是线程安全的HashMap。支持高并发,性能比HashTable高。首先它只对写入加锁,没有对读加锁。
HashTable对插入和获取操作都使用了同步锁:HashTable不能出入null的key和value
123456789101112131415161718192021public synchronized V put(K key, V value) { // Make sure the value is not null if (value == null) { throw new NullPointerException(); } // Makes sure the key is not already in the hashtable. Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7F
2017-08-11
Java
泛型
泛型注意:任意泛型类型兼容所有原始类型,泛型类型型关系要一一对应。
List<Object>接受List
List<String>接受List
List<Object>不接受List<String>
List<Base>不接受List<Child>
1234567891011121314151617181920212223242526272829303132333435public class Main { public static void main(String[] args) { Main main = new Main(); //没有使用泛型的List List list = new ArrayList(); list.add(new Object()); list.add(new Main()); list.add(1); list.add("1241"); list.add(14);
2017-07-30
Java