1、面向对象补充(详见面试补充) 基于JDK11
静态代码块 > 非静态代码块 > 无参/有参构造
在同一次编译运行时,静态代码块只会被调用一次
List
ArrayList (数组,初始容量为10)
注:
除了通过 Iterator 自己的remove或add方法,迭代器将抛出ConcurrentModificationException 。 因此,面对并发修改,迭代器快速而干净地失败,而不是在未来不确定的时间冒着任意、非确定性行为的风险。
可被序列化,线程不安全
安全的List集合:
CopyOnWriteArrayList:官方解释意思大概为(由百度翻译)
1ArrayList线程安全变体,其中所有可变操作( add 、 set等)都是通过制作底层数组的新副本来实现的。 2这通常成本太高,但当遍历操作大大超过突变时可能比替代方法更有效,并且当您不能或不想同步遍历时很有用,但需要排除并发线程之间的干扰。 “快照”样式的迭代器方法使用对创建迭代器时数组状态的引用。 这个数组在迭代器的生命周期内永远不会改变,所以干扰是不可能的,并且迭代器保证不会抛出ConcurrentModificationException 。 自创建迭代器以来,迭代器不会反映对列表的添加、删除或更改。 不支持迭代器本身的元素更改操作( remove 、 set和add )。 这些方法抛出UnsupportedOperationException 。 3允许所有元素,包括null 。 4内存一致性影响:与其他并发集合一样,在将对象放入CopyOnWriteArrayList之前线程中的操作发生在另一个线程中从CopyOnWriteArrayList访问或删除该元素之后的操作之前。
Collections.synchronizedList(list);
ArrayList容量增加策略,>>1,右移一位除以2,即新容量在OldCapacity的基础上加OldCapacity的一半
1/** 2 * Returns a capacity at least as large as the given minimum capacity. 3 * Returns the current capacity increased by 50% if that suffices. 4 * Will not return a capacity greater than MAX_ARRAY_SIZE unless 5 * the given minimum capacity is greater than MAX_ARRAY_SIZE. 6 * 7 * @param minCapacity the desired minimum capacity 8 * @throws OutOfMemoryError if minCapacity is less than zero 9 */ 10 private int newCapacity(int minCapacity) { 11 // overflow-conscious code 12 int oldCapacity = elementData.length; 13 int newCapacity = oldCapacity + (oldCapacity >> 1); 14 if (newCapacity - minCapacity <= 0) { 15 if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) 16 return Math.max(DEFAULT_CAPACITY, minCapacity); 17 if (minCapacity < 0) // overflow 18 throw new OutOfMemoryError(); 19 return minCapacity; 20 } 21 return (newCapacity - MAX_ARRAY_SIZE <= 0) 22 ? newCapacity 23 : hugeCapacity(minCapacity); 24 }
最大容量默认为Integer的最大值为2的31次方-1 -8
1/** 2 * The maximum size of array to allocate (unless necessary). 3 * Some VMs reserve some header words in an array. 4 * Attempts to allocate larger arrays may result in 5 * OutOfMemoryError: Requested array size exceeds VM limit 6 */ 7private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
如果容量超过了这个值,则会使用Integer的最大值
1private static int hugeCapacity(int minCapacity) { 2 if (minCapacity < 0) // overflow 3 throw new OutOfMemoryError(); 4 return (minCapacity > MAX_ARRAY_SIZE) 5 ? Integer.MAX_VALUE 6 : MAX_ARRAY_SIZE; 7 }
List在对容量进行动态增加时,使用的是数组copy
1Arrays.copyOf(elementData, size) 2System.arraycopy(elementData, 0, a, 0, size);
补:数组Copy
Arrays.copyOf:
System.arraycopy:
LinkedList(链表 -> 双向链表)
注:
除了通过 Iterator 自己的remove或add方法,迭代器将抛出ConcurrentModificationException 。 因此,面对并发修改,迭代器快速而干净地失败,而不是在未来不确定的时间冒着任意、非确定性行为的风险。
Arrays
注意点:arrays.asList()
1/** 2 * 不能对Arrays.asList转换成的List进行remove或者add操作,否则会报一个异常 3 * Exception in thread "main" java.lang.UnsupportedOperationException 4 * 原因好像是因为Object的转换 5 * 调用流程 6 * @SafeVarargs 7 * @SuppressWarnings("varargs") 8 * public static <T> List<T> asList(T... a) { 9 * return new ArrayList<>(a); 10 * } 11 * ArrayList(E[] array) { 12 * a = Objects.requireNonNull(array); 13 * } 14 * public static <T> T requireNonNull(T obj) { 15 * if (obj == null) 16 * throw new NullPointerException(); 17 * return obj; 18 * } 19 */
