大佬教程收集整理的这篇文章主要介绍了备战-Java 容器,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
备战-Java 容器
玉阶生白露,夜久侵罗袜。
简介:备战-Java 容器
容器主要包括 Collection 和 Map 两种,Collection 存储着对象的集合,而 Map 存储着key-value 键值对(两个对象)的映射表。
TreeSet:基于红黑树实现,支持有序性操作,例如根据一个范围查找元素的操作。但是查找效率不如 HashSet,HashSet 查找的时间复杂度为 O(1),TreeSet 则为 O(logN)。
HashSet:基于哈希表实现,支持快速查找,但不支持有序性操作。并且失去了元素的插入顺序信息,也就是说使用 Iterator 遍历 HashSet 得到的结果是不确定的。
LinkedHashSet:具有 HashSet 的查找效率,并且内部使用双向链表维护元素的插入顺序。
ArrayList:基于动态数组实现,支持随机访问。
Vector:和 ArrayList 类似,但它是线程安全的。
LinkedList:基于双向链表实现,只能顺序访问,但是可以快速地在链表中间插入和删除元素。不仅如此,LinkedList 还可以用作栈、队列和双向队列。
LinkedList:可以用它来实现双向队列。
PriorityQueue:基于堆结构实现,可以用它来实现优先队列。
TreeMap:基于红黑树实现。
HashMap:基于哈希表实现。
HashTable:和 HashMap 类似,但它是线程安全的,这意味着同一时刻多个线程同时写入 HashTable 不会导致数据不一致。它是遗留类,不应该去使用它,而是使用 ConcurrentHashMap 来支持线程安全,ConcurrentHashMap 的效率会更高,因为 ConcurrentHashMap 引入了分段锁。
LinkedHashMap:使用双向链表来维护元素的顺序,顺序为插入顺序或者最近最少使用(LRU)顺序。(LRU算法是least Recently Used的缩写,即最近最少使用)
Collection 继承了 Iterable 接口,其中的 iterator() 方法能够产生一个 Iterator 对象,通过这个对象就可以迭代遍历 Collection 中的元素。
从 JDK 1.5 之后可以使用 foreach 方法来遍历实现了 Iterable 接口的聚合对象。
@H_197_183@ @H_197_183@1 List<String> list = @H_856_197@new ArrayList<>(); 2 list.add("a"); 3 list.add("b"); 4 @H_856_197@for (String item : list) { 5 System.out.println(item); 6 }
java.util.Arrays.asList() 可以把数组类型转换为 List 类型。
@H_197_183@ @H_197_183@1 @SafeVarargs 2 @H_856_197@public @H_856_197@static <T> List<T> asList(T... a)
值得注意的是 asList() 的参数为泛型的变长参数,不能使用基本类型数组作为参数,只能使用相应的包装类型数组。
1 Integer[] arr = {1, 2, 3};
2 List list = Arrays.asList(arr);
也可以使用以下方式调用 asList():
@H_197_183@List list = Arrays.asList(1, 2, 3);
如果没有特别说明,以下源码分析基于 JDK 1.8。
在 IDEA 中 双击 shift 键调出 Search EveryWhere,查找源码文件,找到之后就可以阅读源码。
因为 ArrayList 是基于数组实现的,所以支持快速随机访问。RandomAccess 接口标识着该类支持快速随机访问,其默认数组大小为10
@H_197_183@ @H_197_183@
1 /* 2 * Copyright (C) 1997, 2017, Oracle and/or its affiliates. All rights reserved. 3 * OracLE PROPRIETARY/CONFIDENTIAl. Use is subject to license terms. 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 * 13 * 14 * 15 * 16 * 17 * 18 * 19 * 20 * 21 * 22 * 23 * 24 */ 25 26 @H_856_197@package java.util; 27 28 @H_856_197@import java.util.function.Consumer; 29 @H_856_197@import java.util.function.PreDicate; 30 @H_856_197@import java.util.function.UnaryOperator; 31 @H_856_197@import sun.misc.SharedSecrets; 32 33 /** 34 * Resizable-array implementation of the <tt>List</tt> interface. Implements 35 * all optional list operations, and permits all elements, including 36 * <tt>null</tt>. In addition to implemenTing the <tt>List</tt> interface, 37 * this class provides methods to manipulate the size of the array that is 38 * used internally to store the list. (This class is roughly equivalent to 39 * <tt>Vector</tt>, except that it is unsynchronized.) 40 * 41 * <p>The <tt>size</tt>, <tt>isEmpty</tt>, <tt>get</tt>, <tt>set</tt>, 42 * <tt>iterator</tt>, and <tt>listIterator</tt> operations run in constant 43 * time. The <tt>add</tt> operation runs in <i>amortized constant time</i>, 44 * that is, adding n elements requires O(n) time. All of the other operations 45 * run in linear time (roughly speaking). The constant factor is low compared 46 * to that for the <tt>LinkedList</tt> implementation. 47 * 48 * <p>Each <tt>ArrayList</tt> instance has a <i>capacity</i>. The capacity is 49 * the size of the array used to store the elements in the list. it is always 50 * at least as large as the list size. As elements are added to an ArrayList, 51 * its capacity grows automatically. The details of the growth policy are not 52 * specified beyond the fact that adding an element has constant amortized 53 * time cost. 54 * 55 * <p>An application can increase the capacity of an <tt>ArrayList</tt> instance 56 * before adding a large number of elements using the <tt>ensureCapacity</tt> 57 * operation. This may reduce the amount of incremental realLOCATIOn. 58 * 59 * <p><strong>Note that this implementation is not synchronized.</strong> 60 * If multiple threads access an <tt>ArrayList</tt> instance concurrently, 61 * and at least one of the threads modifies the list structurally, it 62 * <i>must</i> be synchronized externally. (A structural modification is 63 * any operation that adds or deletes one or more elements, or explicitly 64 * resizes the BACking array; merely setTing the value of an element is not 65 * a structural modification.) This is typically accomplished by 66 * synchronizing on some object that naturally encapsulates the list. 67 * 68 * If no such object exists, the list should be "wrapped" using the 69 * {@link Collections#synchronizedList Collections.synchronizedList} 70 * method. This is best done at creation time, to prevent accidental 71 * unsynchronized access to the list:<pre> 72 * List list = Collections.synchronizedList(new ArrayList(...));</pre> 73 * 74 * <p><a name="fail-fast"> 75 * The iterators returned by this class's {@link #iterator() iterator} and 76 * {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>:</a> 77 * if the list is structurally modified at any time after the iterator is 78 * created, in any way except through the iterator's own 79 * {@link ListIterator#remove() removE} or 80 * {@link ListIterator#add(Object) adD} methods, the iterator will throw a 81 * {@link ConcurrentModificationException}. Thus, in the face of 82 * concurrent modification, the iterator fails quickly and cleanly, rather 83 * than risking arbitrary, non-deterministic behavior at an undetermined 84 * time in the future. 85 * 86 * <p>Note that the fail-fast behavior of an iterator cAnnot be guaranteed 87 * as it is, generally speaking, impossible to make any hard guarantees in the 88 * presence of unsynchronized concurrent modification. Fail-fast iterators 89 * throw {@code ConcurrentModificationException} on a best-effort basis. 90 * Therefore, it would be wrong to write a program that depended on this 91 * exception for its correctness: <i>the fail-fast behavior of iterators 92 * should be used only to detect bugs.</i> 93 * 94 * <p>This class is a member of the 95 * <a href="{@docRoot}/../technotes/guides/collections/index.html"> 96 * Java Collections Framework</a>. 97 * 98 * @author Josh Bloch 99 * @author Neal Gafter 100 * @see Collection 101 * @see List 102 * @see LinkedList 103 * @see Vector 104 * @since 1.2 105 */ 106 107 @H_856_197@public @H_856_197@class ArrayList<E> @H_856_197@extends AbstractList<E> 108 @H_856_197@implements List<E>, RandomAccess, Cloneable, java.io.serializable 109 { 110 @H_856_197@private @H_856_197@static @H_856_197@final @H_856_197@long serialVersionUID = 8683452581122892189L; 111 112 /** 113 * Default initial capacity. 114 */ 115 @H_856_197@private @H_856_197@static @H_856_197@final @H_856_197@int DEFAULT_CAPACITY = 10; 116 117 /** 118 * Shared empty array instance used for empty instances. 119 */ 120 @H_856_197@private @H_856_197@static @H_856_197@final Object[] EMPTY_ELEMENTDATA = {}; 121 122 /** 123 * Shared empty array instance used for default sized empty instances. We 124 * disTinguish this from EMPTY_ELEMENTDATA to know how much to inflate when 125 * first element is added. 126 */ 127 @H_856_197@private @H_856_197@static @H_856_197@final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; 128 129 /** 130 * The array buffer into which the elements of the ArrayList are stored. 131 * The capacity of the ArrayList is the length of this array buffer. Any 132 * empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA 133 * will be expanded to DEFAULT_CAPACITY when the first element is added. 134 */ 135 @H_856_197@transient Object[] elementData; // non-private to simplify nested class access 136 137 /** 138 * The size of the ArrayList (the number of elements it contains). 139 * 140 * @serial 141 */ 142 @H_856_197@private @H_856_197@int size; 143 144 /** 145 * Constructs an empty list with the specified initial capacity. 146 * 147 * @param initialCapacity the initial capacity of the list 148 * @throws IllegalArgumentexception if the specified initial capacity 149 * is negative 150 */ 151 @H_856_197@public ArrayList(@H_856_197@int initialCapacity) { 152 @H_856_197@if (initialCapacity > 0) { 153 @H_856_197@this.elementData = @H_856_197@new Object[initialCapacity]; 154 } @H_856_197@else @H_856_197@if (initialCapacity == 0) { 155 @H_856_197@this.elementData = EMPTY_ELEMENTDATA; 156 } @H_856_197@else { 157 @H_856_197@throw @H_856_197@new IllegalArgumentexception("Illegal Capacity: "+ 158 initialCapacity); 159 } 160 } 161 162 /** 163 * Constructs an empty list with an initial capacity of ten. 164 */ 165 @H_856_197@public ArrayList() { 166 @H_856_197@this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; 167 } 168 169 /** 170 * Constructs a list containing the elements of the specified 171 * collection, in the order they are returned by the collection's 172 * iterator. 173 * 174 * @param c the collection whose elements are to be placed into this list 175 * @throws NullPointerException if the specified collection is null 176 */ 177 @H_856_197@public ArrayList(Collection<? @H_856_197@extends E> C) { 178 elementData = c.toArray(); 179 @H_856_197@if ((size = elementData.length) != 0) { 180 // c.toArray might (incorrectly) not return Object[] (see 6260652) 181 @H_856_197@if (elementData.getClass() != Object[].@H_856_197@class) 182 elementData = Arrays.copyOf(elementData, size, Object[].@H_856_197@class); 183 } @H_856_197@else { 184 // replace with empty array. 185 @H_856_197@this.elementData = EMPTY_ELEMENTDATA; 186 } 187 } 188 189 /** 190 * Trims the capacity of this <tt>ArrayList</tt> instance to be the 191 * list's current size. An application can use this operation to minimize 192 * the storage of an <tt>ArrayList</tt> instance. 193 */ 194 @H_856_197@public @H_856_197@void trimToSize() { 195 modCount++; 196 @H_856_197@if (size < elementData.length) { 197 elementData = (size == 0) 198 ? EMPTY_ELEMENTDATA 199 : Arrays.copyOf(elementData, sizE); 200 } 201 } 202 203 /** 204 * Increases the capacity of this <tt>ArrayList</tt> instance, if 205 * necessary, to ensure that it can hold at least the number of elements 206 * specified by the minimum capacity argument. 207 * 208 * @param minCapacity the desired minimum capacity 209 */ 210 @H_856_197@public @H_856_197@void ensureCapacity(@H_856_197@int minCapacity) { 211 @H_856_197@int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA) 212 // any size if not default element table 213 ? 0 214 // larger than default for default empty table. It's already 215 // supposed to be at default size. 216 : DEFAULT_CAPACITY; 217 218 @H_856_197@if (minCapacity > minExpand) { 219 ensureExplicitCapacity(minCapacity); 220 } 221 } 222 223 @H_856_197@private @H_856_197@static @H_856_197@int calculateCapacity(Object[] elementData, @H_856_197@int minCapacity) { 224 @H_856_197@if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { 225 @H_856_197@return Math.max(DEFAULT_CAPACITY, minCapacity); 226 } 227 @H_856_197@return minCapacity; 228 } 229 230 @H_856_197@private @H_856_197@void ensureCapacityInternal(@H_856_197@int minCapacity) { 231 ensureExplicitCapacity(calculateCapacity(elementData, minCapacity)); 232 } 233 234 @H_856_197@private @H_856_197@void ensureExplicitCapacity(@H_856_197@int minCapacity) { 235 modCount++; 236 237 // overflow-conscious code 238 @H_856_197@if (minCapacity - elementData.length > 0) 239 grow(minCapacity); 240 } 241 242 /** 243 * The maximum size of array to allocate. 244 * Some VMs reserve some header words in an array. 245 * Attempts to allocate larger arrays may result in 246 * OutOfMemoryError: requested array size exceeds VM limit 247 */ 248 @H_856_197@private @H_856_197@static @H_856_197@final @H_856_197@int MAX_ARRAY_SIZE = Integer.max_value - 8; 249 250 /** 251 * Increases the capacity to ensure that it can hold at least the 252 * number of elements specified by the minimum capacity argument. 253 * 254 * @param minCapacity the desired minimum capacity 255 */ 256 @H_856_197@private @H_856_197@void grow(@H_856_197@int minCapacity) { 257 // overflow-conscious code 258 @H_856_197@int oldCapacity = elementData.length; 259 @H_856_197@int newCapacity = oldCapacity + (oldCapacity >> 1); 260 @H_856_197@if (newCapacity - minCapacity < 0) 261 newCapacity = minCapacity; 262 @H_856_197@if (newCapacity - MAX_ARRAY_SIZE > 0) 263 newCapacity = hugeCapacity(minCapacity); 264 // minCapacity is usually close to size, so this is a win: 265 elementData = Arrays.copyOf(elementData, newCapacity); 266 } 267 268 @H_856_197@private @H_856_197@static @H_856_197@int hugeCapacity(@H_856_197@int minCapacity) { 269 @H_856_197@if (minCapacity < 0) // overflow 270 @H_856_197@throw @H_856_197@new OutOfMemoryError(); 271 @H_856_197@return (minCapacity > MAX_ARRAY_SIZE) ? 272 Integer.max_value : 273 MAX_ARRAY_SIZE; 274 } 275 276 /** 277 * Returns the number of elements in this list. 278 * 279 * @return the number of elements in this list 280 */ 281 @H_856_197@public @H_856_197@int size() { 282 @H_856_197@return size; 283 } 284 285 /** 286 * Returns <tt>true</tt> if this list contains no elements. 287 * 288 * @return <tt>true</tt> if this list contains no elements 289 */ 290 @H_856_197@public @H_856_197@Boolean isEmpty() { 291 @H_856_197@return size == 0; 292 } 293 294 /** 295 * Returns <tt>true</tt> if this list contains the specified element. 296 * More formally, returns <tt>true</tt> if and only if this list contains 297 * at least one element <tt>e</tt> such that 298 * <tt>(o==null ? e==null : o.equals(E))</tt>. 299 * 300 * @param o element whose presence in this list is to be tested 301 * @return <tt>true</tt> if this list contains the specified element 302 */ 303 @H_856_197@public @H_856_197@Boolean contains(Object o) { 304 @H_856_197@return indexOf(o) >= 0; 305 } 306 307 /** 308 * Returns the index of the first occurrence of the specified element 309 * in this list, or -1 if this list does not contain the element. 310 * More formally, returns the loWest index <tt>i</tt> such that 311 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, 312 * or -1 if there is no such index. 313 */ 314 @H_856_197@public @H_856_197@int indexOf(Object o) { 315 @H_856_197@if (o == @H_856_197@null) { 316 @H_856_197@for (@H_856_197@int i = 0; i < size; i++) 317 @H_856_197@if (elementData[i]==@H_856_197@null) 318 @H_856_197@return i; 319 } @H_856_197@else { 320 @H_856_197@for (@H_856_197@int i = 0; i < size; i++) 321 @H_856_197@if (o.equals(elementData[i])) 322 @H_856_197@return i; 323 } 324 @H_856_197@return -1; 325 } 326 327 /** 328 * Returns the index of the last occurrence of the specified element 329 * in this list, or -1 if this list does not contain the element. 330 * More formally, returns the highest index <tt>i</tt> such that 331 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, 332 * or -1 if there is no such index. 333 */ 334 @H_856_197@public @H_856_197@int lasTindexOf(Object o) { 335 @H_856_197@if (o == @H_856_197@null) { 336 @H_856_197@for (@H_856_197@int i = size-1; i >= 0; i--) 337 @H_856_197@if (elementData[i]==@H_856_197@null) 338 @H_856_197@return i; 339 } @H_856_197@else { 340 @H_856_197@for (@H_856_197@int i = size-1; i >= 0; i--) 341 @H_856_197@if (o.equals(elementData[i])) 342 @H_856_197@return i; 343 } 344 @H_856_197@return -1; 345 } 346 347 /** 348 * Returns a shallow copy of this <tt>ArrayList</tt> instance. (The 349 * elements themselves are not copied.) 350 * 351 * @return a clone of this <tt>ArrayList</tt> instance 352 */ 353 @H_856_197@public Object clone() { 354 @H_856_197@try { 355 ArrayList<?> v = (ArrayList<?>) @H_856_197@super.clone(); 356 v.elementData = Arrays.copyOf(elementData, sizE); 357 v.modCount = 0; 358 @H_856_197@return v; 359 } @H_856_197@catch (CloneNotSupportedException E) { 360 // this shouldn't happen, since we are Cloneable 361 @H_856_197@throw @H_856_197@new InternalError(E); 362 } 363 } 364 365 /** 366 * Returns an array containing all of the elements in this list 367 * in proper sequence (from first to last element). 368 * 369 * <p>The returned array will be "safe" in that no references to it are 370 * maintained by this list. (In other words, this method must allocate 371 * a new array). The caller is thus free to modify the returned array. 372 * 373 * <p>This method acts as bridge between array-based and collection-based 374 * APIs. 375 * 376 * @return an array containing all of the elements in this list in 377 * proper sequence 378 */ 379 @H_856_197@public Object[] toArray() { 380 @H_856_197@return Arrays.copyOf(elementData, sizE); 381 } 382 383 /** 384 * Returns an array containing all of the elements in this list in proper 385 * sequence (from first to last element); the runtime type of the returned 386 * array is that of the specified array. If the list fits in the 387 * specified array, it is returned therein. Otherwise, a new array is 388 * allocated with the runtime type of the specified array and the size of 389 * this list. 390 * 391 * <p>If the list fits in the specified array with room to spare 392 * (i.e., the array has more elements than the list), the element in 393 * the array immediately following the end of the collection is set to 394 * <tt>null</tt>. (This is useful in determining the length of the 395 * list <i>only</i> if the caller knows that the list does not contain 396 * any null elements.) 397 * 398 * @param a the array into which the elements of the list are to 399 * be stored, if it is big enough; otherwise, a new array of the 400 * same runtime type is allocated for this purpose. 401 * @return an array containing the elements of the list 402 * @throws ArrayStoreException if the runtime type of the specified array 403 * is not a supertype of the runtime type of every element in 404 * this list 405 * @throws NullPointerException if the specified array is null 406 */ 407 @SuppressWarnings("unchecked") 408 @H_856_197@public <T> T[] toArray(T[] a) { 409 @H_856_197@if (a.length < sizE) 410 // Make a new array of a's runtime type, but my contents: 411 @H_856_197@return (T[]) Arrays.copyOf(elementData, size, a.getClass()); 412 System.arraycopy(elementData, 0, a, 0, sizE); 413 @H_856_197@if (a.length > sizE) 414 a[size] = @H_856_197@null; 415 @H_856_197@return a; 416 } 417 418 // Positional Access Operations 419 420 @SuppressWarnings("unchecked") 421 E elementData(@H_856_197@int indeX) { 422 @H_856_197@return (E) elementData[index]; 423 } 424 425 /** 426 * Returns the element at the specified position in this list. 427 * 428 * @param index index of the element to return 429 * @return the element at the specified position in this list 430 * @throws IndexOutOfBoundsException {@inheritDoc} 431 */ 432 @H_856_197@public E get(@H_856_197@int indeX) { 433 rangecheck(indeX); 434 435 @H_856_197@return elementData(indeX); 436 } 437 438 /** 439 * replaces the element at the specified position in this list with 440 * the specified element. 441 * 442 * @param index index of the element to replace 443 * @param element element to be stored at the specified position 444 * @return the element previously at the specified position 445 * @throws IndexOutOfBoundsException {@inheritDoc} 446 */ 447 @H_856_197@public E set(@H_856_197@int index, E element) { 448 rangecheck(indeX); 449 450 E oldValue = elementData(indeX); 451 elementData[index] = element; 452 @H_856_197@return oldValue; 453 } 454 455 /** 456 * Appends the specified element to the end of this list. 457 * 458 * @param e element to be appended to this list 459 * @return <tt>true</tt> (as specified by {@link Collection#adD}) 460 */ 461 @H_856_197@public @H_856_197@Boolean add(E E) { 462 ensureCapacityInternal(size + 1); // Increments modCount!! 463 elementData[size++] = e; 464 @H_856_197@return @H_856_197@true; 465 } 466 467 /** 468 * Inserts the specified element at the specified position in this 469 * list. Shifts the element currently at that position (if any) and 470 * any subsequent elements to the right (adds one to their inDices). 471 * 472 * @param index index at which the specified element is to be inserted 473 * @param element element to be inserted 474 * @throws IndexOutOfBoundsException {@inheritDoc} 475 */ 476 @H_856_197@public @H_856_197@void add(@H_856_197@int index, E element) { 477 rangecheckForAdd(indeX); 478 479 ensureCapacityInternal(size + 1); // Increments modCount!! 480 System.arraycopy(elementData, index, elementData, index + 1, 481 size - indeX); 482 elementData[index] = element; 483 size++; 484 } 485 486 /** 487 * Removes the element at the specified position in this list. 488 * Shifts any subsequent elements to the left (subtracts one from their 489 * inDices). 490 * 491 * @param index the index of the element to be removed 492 * @return the element that was removed from the list 493 * @throws IndexOutOfBoundsException {@inheritDoc} 494 */ 495 @H_856_197@public E remove(@H_856_197@int indeX) { 496 rangecheck(indeX); 497 498 modCount++; 499 E oldValue = elementData(indeX); 500 501 @H_856_197@int numMoved = size - index - 1; 502 @H_856_197@if (numMoved > 0) 503 System.arraycopy(elementData, index+1, elementData, index, 504 numMoved); 505 elementData[--size] = @H_856_197@null; // clear to let GC do its work 506 507 @H_856_197@return oldValue; 508 } 509 510 /** 511 * Removes the first occurrence of the specified element from this list, 512 * if it is present. If the list does not contain the element, it is 513 * unchanged. More formally, removes the element with the loWest index 514 * <tt>i</tt> such that 515 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> 516 * (if such an element exists). Returns <tt>true</tt> if this list 517 * contained the specified element (or equivalently, if this list 518 * changed as a result of the call). 519 * 520 * @param o element to be removed from this list, if present 521 * @return <tt>true</tt> if this list contained the specified element 522 */ 523 @H_856_197@public @H_856_197@Boolean remove(Object o) { 524 @H_856_197@if (o == @H_856_197@null) { 525 @H_856_197@for (@H_856_197@int index = 0; index < size; index++) 526 @H_856_197@if (elementData[index] == @H_856_197@null) { 527 fastRemove(indeX); 528 @H_856_197@return @H_856_197@true; 529 } 530 } @H_856_197@else { 531 @H_856_197@for (@H_856_197@int index = 0; index < size; index++) 532 @H_856_197@if (o.equals(elementData[index])) { 533 fastRemove(indeX); 534 @H_856_197@return @H_856_197@true; 535 } 536 } 537 @H_856_197@return @H_856_197@false; 538 } 539 540 /* 541 * Private remove method that skips bounds checking and does not 542 * return the value removed. 543 */ 544 @H_856_197@private @H_856_197@void fastRemove(@H_856_197@int indeX) { 545 modCount++; 546 @H_856_197@int numMoved = size - index - 1; 547 @H_856_197@if (numMoved > 0) 548 System.arraycopy(elementData, index+1, elementData, index, 549 numMoved); 550 elementData[--size] = @H_856_197@null; // clear to let GC do its work 551 } 552 553 /** 554 * Removes all of the elements from this list. The list will 555 * be empty after this call returns. 556 */ 557 @H_856_197@public @H_856_197@void clear() { 558 modCount++; 559 560 // clear to let GC do its work 561 @H_856_197@for (@H_856_197@int i = 0; i < size; i++) 562 elementData[i] = @H_856_197@null; 563 564 size = 0; 565 } 566 567 /** 568 * Appends all of the elements in the specified collection to the end of 569 * this list, in the order that they are returned by the 570 * specified collection's Iterator. The behavior of this operation is 571 * undefined if the specified collection is modified while the operation 572 * is in progress. (This implies that the behavior of this call is 573 * undefined if the specified collection is this list, and this 574 * list is nonempty.) 575 * 576 * @param c collection containing elements to be added to this list 577 * @return <tt>true</tt> if this list changed as a result of the call 578 * @throws NullPointerException if the specified collection is null 579 */ 580 @H_856_197@public @H_856_197@Boolean addAll(Collection<? @H_856_197@extends E> C) { 581 Object[] a = c.toArray(); 582 @H_856_197@int numNew = a.length; 583 ensureCapacityInternal(size + numNew); // Increments modCount 584 System.arraycopy(a, 0, elementData, size, numNew); 585 size += numNew; 586 @H_856_197@return numNew != 0; 587 } 588 589 /** 590 * Inserts all of the elements in the specified collection into this 591 * list, starTing at the specified position. Shifts the element 592 * currently at that position (if any) and any subsequent elements to 593 * the right (increases their inDices). The new elements will appear 594 * in the list in the order that they are returned by the 595 * specified collection's iterator. 596 * 597 * @param index index at which to insert the first element from the 598 * specified collection 599 * @param c collection containing elements to be added to this list 600 * @return <tt>true</tt> if this list changed as a result of the call 601 * @throws IndexOutOfBoundsException {@inheritDoc} 602 * @throws NullPointerException if the specified collection is null 603 */ 604 @H_856_197@public @H_856_197@Boolean addAll(@H_856_197@int index, Collection<? @H_856_197@extends E> C) { 605 rangecheckForAdd(indeX); 606 607 Object[] a = c.toArray(); 608 @H_856_197@int numNew = a.length; 609 ensureCapacityInternal(size + numNew); // Increments modCount 610 611 @H_856_197@int numMoved = size - index; 612 @H_856_197@if (numMoved > 0) 613 System.arraycopy(elementData, index, elementData, index + numNew, 614 numMoved); 615 616 System.arraycopy(a, 0, elementData, index, numNew); 617 size += numNew; 618 @H_856_197@return numNew != 0; 619 } 620 621 /** 622 * Removes from this list all of the elements whose index is between 623 * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. 624 * Shifts any succeeding elements to the left (reduces their indeX). 625 * This call shortens the list by {@code (toIndex - fromIndeX)} elements. 626 * (If {@code toIndex==fromIndex}, this operation has no effect.) 627 * 628 * @throws IndexOutOfBoundsException if {@code fromIndex} or 629 * {@code toIndex} is out of range 630 * ({@code fromIndex < 0 || 631 * fromIndex >= size() || 632 * toIndex > size() || 633 * toIndex < fromIndex}) 634 */ 635 @H_856_197@protected @H_856_197@void removeRange(@H_856_197@int fromIndex, @H_856_197@int toIndex) { 636 modCount++; 637 @H_856_197@int numMoved = size - toIndex; 638 System.arraycopy(elementData, toIndex, elementData, fromIndex, 639 numMoved); 640 641 // clear to let GC do its work 642 @H_856_197@int newSize = size - (toIndex-fromIndeX); 643 @H_856_197@for (@H_856_197@int i = newSize; i < size; i++) { 644 elementData[i] = @H_856_197@null; 645 } 646 size = newSize; 647 } 648 649 /** 650 * checks if the given index is in range. If not, throws an appropriate 651 * runtime exception. This method does *not* check if the index is 652 * negative: it is always used immediately prior to an array access, 653 * which throws an ArrayIndexOutOfBoundsException if index is negative. 654 */ 655 @H_856_197@private @H_856_197@void rangecheck(@H_856_197@int indeX) { 656 @H_856_197@if (index >= sizE) 657 @H_856_197@throw @H_856_197@new IndexOutOfBoundsException(outOfBoundsMsg(indeX)); 658 } 659 660 /** 661 * A version of rangecheck used by add and addAll. 662 */ 663 @H_856_197@private @H_856_197@void rangecheckForAdd(@H_856_197@int indeX) { 664 @H_856_197@if (index > size || index < 0) 665 @H_856_197@throw @H_856_197@new IndexOutOfBoundsException(outOfBoundsMsg(indeX)); 666 } 667 668 /** 669 * Constructs an IndexOutOfBoundsException detail message. 670 * Of the many possible refactorings of the error handling code, 671 * this "outlining" performs best with both server and client VMs. 672 */ 673 @H_856_197@private String outOfBoundsMsg(@H_856_197@int indeX) { 674 @H_856_197@return "Index: "+index+", Size: "+size; 675 } 676 677 /** 678 * Removes from this list all of its elements that are contained in the 679 * specified collection. 680 * 681 * @param c collection containing elements to be removed from this list 682 * @return {@code truE} if this list changed as a result of the call 683 * @throws ClassCastException if the class of an element of this list 684 * is incompatible with the specified collection 685 * (<a href="Collection.html#optional-reStrictions">optional</a>) 686 * @throws NullPointerException if this list contains a null element and the 687 * specified collection does not permit null elements 688 * (<a href="Collection.html#optional-reStrictions">optional</a>), 689 * or if the specified collection is null 690 * @see Collection#contains(Object) 691 */ 692 @H_856_197@public @H_856_197@Boolean removeAll(Collection<?> C) { 693 Objects.requireNonNull(c); 694 @H_856_197@return batchRemove(c, @H_856_197@false); 695 } 696 697 /** 698 * Retains only the elements in this list that are contained in the 699 * specified collection. In other words, removes from this list all 700 * of its elements that are not contained in the specified collection. 701 * 702 * @param c collection containing elements to be retained in this list 703 * @return {@code truE} if this list changed as a result of the call 704 * @throws ClassCastException if the class of an element of this list 705 * is incompatible with the specified collection 706 * (<a href="Collection.html#optional-reStrictions">optional</a>) 707 * @throws NullPointerException if this list contains a null element and the 708 * specified collection does not permit null elements 709 * (<a href="Collection.html#optional-reStrictions">optional</a>), 710 * or if the specified collection is null 711 * @see Collection#contains(Object) 712 */ 713 @H_856_197@public @H_856_197@Boolean retainAll(Collection<?> C) { 714 Objects.requireNonNull(c); 715 @H_856_197@return batchRemove(c, @H_856_197@true); 716 } 717 718 @H_856_197@private @H_856_197@Boolean batchRemove(Collection<?> c, @H_856_197@Boolean complement) { 719 @H_856_197@final Object[] elementData = @H_856_197@this.elementData; 720 @H_856_197@int r = 0, w = 0; 721 @H_856_197@Boolean modified = @H_856_197@false; 722 @H_856_197@try { 723 @H_856_197@for (; r < size; r++) 724 @H_856_197@if (c.contains(elementData[r]) == complement) 725 elementData[w++] = elementData[r]; 726 } @H_856_197@finally { 727 // Preserve behavioral compatibility with AbstractCollection, 728 // even if c.contains() throws. 729 @H_856_197@if (r != sizE) { 730 System.arraycopy(elementData, r, 731 elementData, w, 732 size - r); 733 w += size - r; 734 } 735 @H_856_197@if (w != sizE) { 736 // clear to let GC do its work 737 @H_856_197@for (@H_856_197@int i = w; i < size; i++) 738 elementData[i] = @H_856_197@null; 739 modCount += size - w; 740 size = w; 741 modified = @H_856_197@true; 742 } 743 } 744 @H_856_197@return modified; 745 } 746 747 /** 748 * Save the state of the <tt>ArrayList</tt> instance to a stream (that 749 * is, serialize it). 750 * 751 * @serialData The length of the array BACking the <tt>ArrayList</tt> 752 * instance is emitted (int), followed by all of its elements 753 * (each an <tt>Object</tt>) in the proper order. 754 */ 755 @H_856_197@private @H_856_197@void writeObject(java.io.objectOutputStream s) 756 @H_856_197@throws java.io.IOException{ 757 // Write out element count, and any hidden stuff 758 @H_856_197@int expectedModCount = modCount; 759 s.defaultWriteObject(); 760 761 // Write out size as capacity for behavioural compatibility with clone() 762 s.writeInt(sizE); 763 764 // Write out all elements in the proper order. 765 @H_856_197@for (@H_856_197@int i=0; i<size; i++) { 766 s.writeObject(elementData[i]); 767 } 768 769 @H_856_197@if (modCount != expectedModCount) { 770 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 771 } 772 } 773 774 /** 775 * Reconstitute the <tt>ArrayList</tt> instance from a stream (that is, 776 * deserialize it). 777 */ 778 @H_856_197@private @H_856_197@void readObject(java.io.objecTinputStream s) 779 @H_856_197@throws java.io.IOException, ClassnotFoundException { 780 elementData = EMPTY_ELEMENTDATA; 781 782 // Read in size, and any hidden stuff 783 s.defaultReadObject(); 784 785 // Read in capacity 786 s.readInt(); // ignored 787 788 @H_856_197@if (size > 0) { 789 // be like clone(), allocate array based upon size not capacity 790 @H_856_197@int capacity = calculateCapacity(elementData, sizE); 791 SharedSecrets.getJavaOISAccess().checkArray(s, Object[].@H_856_197@class, capacity); 792 ensureCapacityInternal(sizE); 793 794 Object[] a = elementData; 795 // Read in all elements in the proper order. 796 @H_856_197@for (@H_856_197@int i=0; i<size; i++) { 797 a[i] = s.readObject(); 798 } 799 } 800 } 801 802 /** 803 * Returns a list iterator over the elements in this list (in proper 804 * sequencE), starTing at the specified position in the list. 805 * The specified index inDicates the first element that would be 806 * returned by an initial call to {@link ListIterator#next next}. 807 * An initial call to {@link ListIterator#previous previous} would 808 * return the element with the specified index minus one. 809 * 810 * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. 811 * 812 * @throws IndexOutOfBoundsException {@inheritDoc} 813 */ 814 @H_856_197@public ListIterator<E> listIterator(@H_856_197@int indeX) { 815 @H_856_197@if (index < 0 || index > sizE) 816 @H_856_197@throw @H_856_197@new IndexOutOfBoundsException("Index: "+indeX); 817 @H_856_197@return @H_856_197@new ListItr(indeX); 818 } 819 820 /** 821 * Returns a list iterator over the elements in this list (in proper 822 * sequencE). 823 * 824 * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>. 825 * 826 * @see #listIterator(int) 827 */ 828 @H_856_197@public ListIterator<E> listIterator() { 829 @H_856_197@return @H_856_197@new ListItr(0); 830 } 831 832 /** 833 * Returns an iterator over the elements in this list in proper sequence. 834 * 835 * <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>. 836 * 837 * @return an iterator over the elements in this list in proper sequence 838 */ 839 @H_856_197@public Iterator<E> iterator() { 840 @H_856_197@return @H_856_197@new Itr(); 841 } 842 843 /** 844 * An optimized version of AbstractList.Itr 845 */ 846 @H_856_197@private @H_856_197@class Itr @H_856_197@implements Iterator<E> { 847 @H_856_197@int cursor; // index of next element to return 848 @H_856_197@int lastRet = -1; // index of last element returned; -1 if no such 849 @H_856_197@int expectedModCount = modCount; 850 851 Itr() {} 852 853 @H_856_197@public @H_856_197@Boolean hasNext() { 854 @H_856_197@return cursor != size; 855 } 856 857 @SuppressWarnings("unchecked") 858 @H_856_197@public E next() { 859 checkForComodification(); 860 @H_856_197@int i = cursor; 861 @H_856_197@if (i >= sizE) 862 @H_856_197@throw @H_856_197@new NoSucHelementexception(); 863 Object[] elementData = ArrayList.@H_856_197@this.elementData; 864 @H_856_197@if (i >= elementData.length) 865 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 866 cursor = i + 1; 867 @H_856_197@return (E) elementData[lastRet = i]; 868 } 869 870 @H_856_197@public @H_856_197@void remove() { 871 @H_856_197@if (lastRet < 0) 872 @H_856_197@throw @H_856_197@new IllegalStateException(); 873 checkForComodification(); 874 875 @H_856_197@try { 876 ArrayList.@H_856_197@this.remove(lastRet); 877 cursor = lastRet; 878 lastRet = -1; 879 expectedModCount = modCount; 880 } @H_856_197@catch (IndexOutOfBoundsException eX) { 881 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 882 } 883 } 884 885 @Override 886 @SuppressWarnings("unchecked") 887 @H_856_197@public @H_856_197@void forEachRemaining(Consumer<? @H_856_197@super E> consumer) { 888 Objects.requireNonNull(consumer); 889 @H_856_197@final @H_856_197@int size = ArrayList.@H_856_197@this.size; 890 @H_856_197@int i = cursor; 891 @H_856_197@if (i >= sizE) { 892 @H_856_197@return; 893 } 894 @H_856_197@final Object[] elementData = ArrayList.@H_856_197@this.elementData; 895 @H_856_197@if (i >= elementData.length) { 896 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 897 } 898 @H_856_197@while (i != size && modCount == expectedModCount) { 899 consumer.accept((E) elementData[i++]); 900 } 901 // update once at end of iteration to reduce heap write traffic 902 cursor = i; 903 lastRet = i - 1; 904 checkForComodification(); 905 } 906 907 @H_856_197@final @H_856_197@void checkForComodification() { 908 @H_856_197@if (modCount != expectedModCount) 909 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 910 } 911 } 912 913 /** 914 * An optimized version of AbstractList.ListItr 915 */ 916 @H_856_197@private @H_856_197@class ListItr @H_856_197@extends Itr @H_856_197@implements ListIterator<E> { 917 ListItr(@H_856_197@int indeX) { 918 @H_856_197@super(); 919 cursor = index; 920 } 921 922 @H_856_197@public @H_856_197@Boolean hasPrevious() { 923 @H_856_197@return cursor != 0; 924 } 925 926 @H_856_197@public @H_856_197@int nexTindex() { 927 @H_856_197@return cursor; 928 } 929 930 @H_856_197@public @H_856_197@int previousIndex() { 931 @H_856_197@return cursor - 1; 932 } 933 934 @SuppressWarnings("unchecked") 935 @H_856_197@public E previous() { 936 checkForComodification(); 937 @H_856_197@int i = cursor - 1; 938 @H_856_197@if (i < 0) 939 @H_856_197@throw @H_856_197@new NoSucHelementexception(); 940 Object[] elementData = ArrayList.@H_856_197@this.elementData; 941 @H_856_197@if (i >= elementData.length) 942 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 943 cursor = i; 944 @H_856_197@return (E) elementData[lastRet = i]; 945 } 946 947 @H_856_197@public @H_856_197@void set(E E) { 948 @H_856_197@if (lastRet < 0) 949 @H_856_197@throw @H_856_197@new IllegalStateException(); 950 checkForComodification(); 951 952 @H_856_197@try { 953 ArrayList.@H_856_197@this.set(lastRet, E); 954 } @H_856_197@catch (IndexOutOfBoundsException eX) { 955 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 956 } 957 } 958 959 @H_856_197@public @H_856_197@void add(E E) { 960 checkForComodification(); 961 962 @H_856_197@try { 963 @H_856_197@int i = cursor; 964 ArrayList.@H_856_197@this.add(i, E); 965 cursor = i + 1; 966 lastRet = -1; 967 expectedModCount = modCount; 968 } @H_856_197@catch (IndexOutOfBoundsException eX) { 969 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 970 } 971 } 972 } 973 974 /** 975 * Returns a view of the portion of this list between the specified 976 * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive. (If 977 * {@code fromIndex} and {@code toIndex} are equal, the returned list is 978 * empty.) The returned list is BACked by this list, so non-structural 979 * changes in the returned list are reflected in this list, and vice-versa. 980 * The returned list supports all of the optional list operations. 981 * 982 * <p>This method eliminates the need for explicit range operations (of 983 * the sort that commonly exist for arrays). Any operation that expects 984 * a list can be used as a range operation by passing a subList view 985 * instead of a whole list. For example, the following idiom 986 * removes a range of elements from a list: 987 * <pre> 988 * list.subList(from, to).clear(); 989 * </pre> 990 * Similar idioms may be constructed for {@link #indexOf(Object)} and 991 * {@link #lasTindexOf(Object)}, and all of the algorithms in the 992 * {@link Collections} class can be applied to a subList. 993 * 994 * <p>The semantics of the list returned by this method become undefined if 995 * the BACking list (i.e., this list) is <i>structurally modified</i> in 996 * any way other than via the returned list. (Structural modifications are 997 * those that change the size of this list, or otherwise perturb it in such 998 * a fashion that iterations in progress may yield incorrect results.) 999 * 1000 * @throws IndexOutOfBoundsException {@inheritDoc} 1001 * @throws IllegalArgumentexception {@inheritDoc} 1002 */ 1003 @H_856_197@public List<E> subList(@H_856_197@int fromIndex, @H_856_197@int toIndex) { 1004 subListRangecheck(fromIndex, toIndex, sizE); 1005 @H_856_197@return @H_856_197@new SubList(@H_856_197@this, 0, fromIndex, toIndex); 1006 } 1007 1008 @H_856_197@static @H_856_197@void subListRangecheck(@H_856_197@int fromIndex, @H_856_197@int toIndex, @H_856_197@int sizE) { 1009 @H_856_197@if (fromIndex < 0) 1010 @H_856_197@throw @H_856_197@new IndexOutOfBoundsException("fromIndex = " + fromIndeX); 1011 @H_856_197@if (toIndex > sizE) 1012 @H_856_197@throw @H_856_197@new IndexOutOfBoundsException("toIndex = " + toIndex); 1013 @H_856_197@if (fromIndex > toIndex) 1014 @H_856_197@throw @H_856_197@new IllegalArgumentexception("fromIndex(" + fromIndex + 1015 ") > toIndex(" + toIndex + ")"); 1016 } 1017 1018 @H_856_197@private @H_856_197@class SubList @H_856_197@extends AbstractList<E> @H_856_197@implements RandomAccess { 1019 @H_856_197@private @H_856_197@final AbstractList<E> parent; 1020 @H_856_197@private @H_856_197@final @H_856_197@int parentOffset; 1021 @H_856_197@private @H_856_197@final @H_856_197@int offset; 1022 @H_856_197@int size; 1023 1024 SubList(AbstractList<E> parent, 1025 @H_856_197@int offset, @H_856_197@int fromIndex, @H_856_197@int toIndex) { 1026 @H_856_197@this.parent = parent; 1027 @H_856_197@this.parentOffset = fromIndex; 1028 @H_856_197@this.offset = offset + fromIndex; 1029 @H_856_197@this.size = toIndex - fromIndex; 1030 @H_856_197@this.modCount = ArrayList.@H_856_197@this.modCount; 1031 } 1032 1033 @H_856_197@public E set(@H_856_197@int index, E E) { 1034 rangecheck(indeX); 1035 checkForComodification(); 1036 E oldValue = ArrayList.@H_856_197@this.elementData(offset + indeX); 1037 ArrayList.@H_856_197@this.elementData[offset + index] = e; 1038 @H_856_197@return oldValue; 1039 } 1040 1041 @H_856_197@public E get(@H_856_197@int indeX) { 1042 rangecheck(indeX); 1043 checkForComodification(); 1044 @H_856_197@return ArrayList.@H_856_197@this.elementData(offset + indeX); 1045 } 1046 1047 @H_856_197@public @H_856_197@int size() { 1048 checkForComodification(); 1049 @H_856_197@return @H_856_197@this.size; 1050 } 1051 1052 @H_856_197@public @H_856_197@void add(@H_856_197@int index, E E) { 1053 rangecheckForAdd(indeX); 1054 checkForComodification(); 1055 parent.add(parentOffset + index, E); 1056 @H_856_197@this.modCount = parent.modCount; 1057 @H_856_197@this.size++; 1058 } 1059 1060 @H_856_197@public E remove(@H_856_197@int indeX) { 1061 rangecheck(indeX); 1062 checkForComodification(); 1063 E result = parent.remove(parentOffset + indeX); 1064 @H_856_197@this.modCount = parent.modCount; 1065 @H_856_197@this.size--; 1066 @H_856_197@return result; 1067 } 1068 1069 @H_856_197@protected @H_856_197@void removeRange(@H_856_197@int fromIndex, @H_856_197@int toIndex) { 1070 checkForComodification(); 1071 parent.removeRange(parentOffset + fromIndex, 1072 parentOffset + toIndex); 1073 @H_856_197@this.modCount = parent.modCount; 1074 @H_856_197@this.size -= toIndex - fromIndex; 1075 } 1076 1077 @H_856_197@public @H_856_197@Boolean addAll(Collection<? @H_856_197@extends E> C) { 1078 @H_856_197@return addAll(@H_856_197@this.size, c); 1079 } 1080 1081 @H_856_197@public @H_856_197@Boolean addAll(@H_856_197@int index, Collection<? @H_856_197@extends E> C) { 1082 rangecheckForAdd(indeX); 1083 @H_856_197@int cSize = c.size(); 1084 @H_856_197@if (cSize==0) 1085 @H_856_197@return @H_856_197@false; 1086 1087 checkForComodification(); 1088 parent.addAll(parentOffset + index, c); 1089 @H_856_197@this.modCount = parent.modCount; 1090 @H_856_197@this.size += cSize; 1091 @H_856_197@return @H_856_197@true; 1092 } 1093 1094 @H_856_197@public Iterator<E> iterator() { 1095 @H_856_197@return listIterator(); 1096 } 1097 1098 @H_856_197@public ListIterator<E> listIterator(@H_856_197@final @H_856_197@int indeX) { 1099 checkForComodification(); 1100 rangecheckForAdd(indeX); 1101 @H_856_197@final @H_856_197@int offset = @H_856_197@this.offset; 1102 1103 @H_856_197@return @H_856_197@new ListIterator<E>() { 1104 @H_856_197@int cursor = index; 1105 @H_856_197@int lastRet = -1; 1106 @H_856_197@int expectedModCount = ArrayList.@H_856_197@this.modCount; 1107 1108 @H_856_197@public @H_856_197@Boolean hasNext() { 1109 @H_856_197@return cursor != SubList.@H_856_197@this.size; 1110 } 1111 1112 @SuppressWarnings("unchecked") 1113 @H_856_197@public E next() { 1114 checkForComodification(); 1115 @H_856_197@int i = cursor; 1116 @H_856_197@if (i >= SubList.@H_856_197@this.sizE) 1117 @H_856_197@throw @H_856_197@new NoSucHelementexception(); 1118 Object[] elementData = ArrayList.@H_856_197@this.elementData; 1119 @H_856_197@if (offset + i >= elementData.length) 1120 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 1121 cursor = i + 1; 1122 @H_856_197@return (E) elementData[offset + (lastRet = i)]; 1123 } 1124 1125 @H_856_197@public @H_856_197@Boolean hasPrevious() { 1126 @H_856_197@return cursor != 0; 1127 } 1128 1129 @SuppressWarnings("unchecked") 1130 @H_856_197@public E previous() { 1131 checkForComodification(); 1132 @H_856_197@int i = cursor - 1; 1133 @H_856_197@if (i < 0) 1134 @H_856_197@throw @H_856_197@new NoSucHelementexception(); 1135 Object[] elementData = ArrayList.@H_856_197@this.elementData; 1136 @H_856_197@if (offset + i >= elementData.length) 1137 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 1138 cursor = i; 1139 @H_856_197@return (E) elementData[offset + (lastRet = i)]; 1140 } 1141 1142 @SuppressWarnings("unchecked") 1143 @H_856_197@public @H_856_197@void forEachRemaining(Consumer<? @H_856_197@super E> consumer) { 1144 Objects.requireNonNull(consumer); 1145 @H_856_197@final @H_856_197@int size = SubList.@H_856_197@this.size; 1146 @H_856_197@int i = cursor; 1147 @H_856_197@if (i >= sizE) { 1148 @H_856_197@return; 1149 } 1150 @H_856_197@final Object[] elementData = ArrayList.@H_856_197@this.elementData; 1151 @H_856_197@if (offset + i >= elementData.length) { 1152 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 1153 } 1154 @H_856_197@while (i != size && modCount == expectedModCount) { 1155 consumer.accept((E) elementData[offset + (i++)]); 1156 } 1157 // update once at end of iteration to reduce heap write traffic 1158 lastRet = cursor = i; 1159 checkForComodification(); 1160 } 1161 1162 @H_856_197@public @H_856_197@int nexTindex() { 1163 @H_856_197@return cursor; 1164 } 1165 1166 @H_856_197@public @H_856_197@int previousIndex() { 1167 @H_856_197@return cursor - 1; 1168 } 1169 1170 @H_856_197@public @H_856_197@void remove() { 1171 @H_856_197@if (lastRet < 0) 1172 @H_856_197@throw @H_856_197@new IllegalStateException(); 1173 checkForComodification(); 1174 1175 @H_856_197@try { 1176 SubList.@H_856_197@this.remove(lastRet); 1177 cursor = lastRet; 1178 lastRet = -1; 1179 expectedModCount = ArrayList.@H_856_197@this.modCount; 1180 } @H_856_197@catch (IndexOutOfBoundsException eX) { 1181 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 1182 } 1183 } 1184 1185 @H_856_197@public @H_856_197@void set(E E) { 1186 @H_856_197@if (lastRet < 0) 1187 @H_856_197@throw @H_856_197@new IllegalStateException(); 1188 checkForComodification(); 1189 1190 @H_856_197@try { 1191 ArrayList.@H_856_197@this.set(offset + lastRet, E); 1192 } @H_856_197@catch (IndexOutOfBoundsException eX) { 1193 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 1194 } 1195 } 1196 1197 @H_856_197@public @H_856_197@void add(E E) { 1198 checkForComodification(); 1199 1200 @H_856_197@try { 1201 @H_856_197@int i = cursor; 1202 SubList.@H_856_197@this.add(i, E); 1203 cursor = i + 1; 1204 lastRet = -1; 1205 expectedModCount = ArrayList.@H_856_197@this.modCount; 1206 } @H_856_197@catch (IndexOutOfBoundsException eX) { 1207 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 1208 } 1209 } 1210 1211 @H_856_197@final @H_856_197@void checkForComodification() { 1212 @H_856_197@if (expectedModCount != ArrayList.@H_856_197@this.modCount) 1213 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 1214 } 1215 }; 1216 } 1217 1218 @H_856_197@public List<E> subList(@H_856_197@int fromIndex, @H_856_197@int toIndex) { 1219 subListRangecheck(fromIndex, toIndex, sizE); 1220 @H_856_197@return @H_856_197@new SubList(@H_856_197@this, offset, fromIndex, toIndex); 1221 } 1222 1223 @H_856_197@private @H_856_197@void rangecheck(@H_856_197@int indeX) { 1224 @H_856_197@if (index < 0 || index >= @H_856_197@this.sizE) 1225 @H_856_197@throw @H_856_197@new IndexOutOfBoundsException(outOfBoundsMsg(indeX)); 1226 } 1227 1228 @H_856_197@private @H_856_197@void rangecheckForAdd(@H_856_197@int indeX) { 1229 @H_856_197@if (index < 0 || index > @H_856_197@this.sizE) 1230 @H_856_197@throw @H_856_197@new IndexOutOfBoundsException(outOfBoundsMsg(indeX)); 1231 } 1232 1233 @H_856_197@private String outOfBoundsMsg(@H_856_197@int indeX) { 1234 @H_856_197@return "Index: "+index+", Size: "+@H_856_197@this.size; 1235 } 1236 1237 @H_856_197@private @H_856_197@void checkForComodification() { 1238 @H_856_197@if (ArrayList.@H_856_197@this.modCount != @H_856_197@this.modCount) 1239 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 1240 } 1241 1242 @H_856_197@public Spliterator<E> spliterator() { 1243 checkForComodification(); 1244 @H_856_197@return @H_856_197@new ArrayListSpliterator<E>(ArrayList.@H_856_197@this, offset, 1245 offset + @H_856_197@this.size, @H_856_197@this.modCount); 1246 } 1247 } 1248 1249 @Override 1250 @H_856_197@public @H_856_197@void forEach(Consumer<? @H_856_197@super E> action) { 1251 Objects.requireNonNull(action); 1252 @H_856_197@final @H_856_197@int expectedModCount = modCount; 1253 @SuppressWarnings("unchecked") 1254 @H_856_197@final E[] elementData = (E[]) @H_856_197@this.elementData; 1255 @H_856_197@final @H_856_197@int size = @H_856_197@this.size; 1256 @H_856_197@for (@H_856_197@int i=0; modCount == expectedModCount && i < size; i++) { 1257 action.accept(elementData[i]); 1258 } 1259 @H_856_197@if (modCount != expectedModCount) { 1260 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 1261 } 1262 } 1263 1264 /** 1265 * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em> 1266 * and <em>fail-fast</em> {@link Spliterator} over the elements in this 1267 * list. 1268 * 1269 * <p>The {@code Spliterator} reports {@link Spliterator#SIZED}, 1270 * {@link Spliterator#SUBSIZED}, and {@link Spliterator#ordered}. 1271 * Overriding implementations should document the reporTing of additional 1272 * characteristic values. 1273 * 1274 * @return a {@code Spliterator} over the elements in this list 1275 * @since 1.8 1276 */ 1277 @Override 1278 @H_856_197@public Spliterator<E> spliterator() { 1279 @H_856_197@return @H_856_197@new ArrayListSpliterator<>(@H_856_197@this, 0, -1, 0); 1280 } 1281 1282 /** Index-based split-by-two, lazily initialized Spliterator */ 1283 @H_856_197@static @H_856_197@final @H_856_197@class ArrayListSpliterator<E> @H_856_197@implements Spliterator<E> { 1284 1285 /* 1286 * If ArrayLists were immutable, or structurally immutable (no 1287 * adds, removes, etC), we could implement their spliterators 1288 * with Arrays.spliterator. Instead we detect as much 1289 * interference during traversal as practical without 1290 * sacrificing much perfoRMANce. We rely primarily on 1291 * modCounts. these are not guaranteed to detect concurrency 1292 * violations, and are sometimes overly conservative about 1293 * within-thread interference, but detect enough problems to 1294 * be worthwhile in practice. To carry this out, we (1) lazily 1295 * initialize fence and expectedModCount until the latest 1296 * point that we need to commit to the state we are checking 1297 * against; thus improving precision. (This doesn't apply to 1298 * SubLists, that create spliterators with current non-lazy 1299 * values). (2) We perform only a single 1300 * ConcurrentModificationException check at the end of forEach 1301 * (the most perfoRMANce-sensitive method). When using forEach 1302 * (as opposed to iterators), we can normally only detect 1303 * interference after actions, not before. Further 1304 * CME-triggering checks apply to all other possible 1305 * violations of assumptions for example null or too-small 1306 * elementData array given its size(), that could only have 1307 * occurred due to interference. This allows the inner loop 1308 * of forEach to run without any further checks, and 1309 * simplifies lambda-resolution. While this does entail a 1310 * number of checks, note that in the common case of 1311 * list.stream().forEach(a), no checks or other computation 1312 * occur anywhere other than inside forEach itself. The other 1313 * less-often-used methods cAnnot take advantage of most of 1314 * these streamlinings. 1315 */ 1316 1317 @H_856_197@private @H_856_197@final ArrayList<E> list; 1318 @H_856_197@private @H_856_197@int index; // current index, modified on advance/split 1319 @H_856_197@private @H_856_197@int fence; // -1 until used; then one past last index 1320 @H_856_197@private @H_856_197@int expectedModCount; // initialized when fence set 1321 1322 /** Create new spliterator covering the given range */ 1323 ArrayListSpliterator(ArrayList<E> list, @H_856_197@int origin, @H_856_197@int fence, 1324 @H_856_197@int expectedModCount) { 1325 @H_856_197@this.list = list; // OK if null unless traversed 1326 @H_856_197@this.index = origin; 1327 @H_856_197@this.fence = fence; 1328 @H_856_197@this.expectedModCount = expectedModCount; 1329 } 1330 1331 @H_856_197@private @H_856_197@int getFence() { // initialize fence to size on first use 1332 @H_856_197@int hi; // (a specialized variant appears in method forEach) 1333 ArrayList<E> lst; 1334 @H_856_197@if ((hi = fencE) < 0) { 1335 @H_856_197@if ((lst = list) == @H_856_197@null) 1336 hi = fence = 0; 1337 @H_856_197@else { 1338 expectedModCount = lst.modCount; 1339 hi = fence = lst.size; 1340 } 1341 } 1342 @H_856_197@return hi; 1343 } 1344 1345 @H_856_197@public ArrayListSpliterator<E> trySplit() { 1346 @H_856_197@int hi = getFence(), lo = index, mid = (lo + hi) >>> 1; 1347 @H_856_197@return (lo >= mid) ? @H_856_197@null : // divide range in half unless too small 1348 @H_856_197@new ArrayListSpliterator<E>(list, lo, index = mid, 1349 expectedModCount); 1350 } 1351 1352 @H_856_197@public @H_856_197@Boolean tryAdvance(Consumer<? @H_856_197@super E> action) { 1353 @H_856_197@if (action == @H_856_197@null) 1354 @H_856_197@throw @H_856_197@new NullPointerException(); 1355 @H_856_197@int hi = getFence(), i = index; 1356 @H_856_197@if (i < hi) { 1357 index = i + 1; 1358 @SuppressWarnings("unchecked") E e = (E)list.elementData[i]; 1359 action.accept(E); 1360 @H_856_197@if (list.modCount != expectedModCount) 1361 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 1362 @H_856_197@return @H_856_197@true; 1363 } 1364 @H_856_197@return @H_856_197@false; 1365 } 1366 1367 @H_856_197@public @H_856_197@void forEachRemaining(Consumer<? @H_856_197@super E> action) { 1368 @H_856_197@int i, hi, mc; // hoist accesses and checks from loop 1369 ArrayList<E> lst; Object[] a; 1370 @H_856_197@if (action == @H_856_197@null) 1371 @H_856_197@throw @H_856_197@new NullPointerException(); 1372 @H_856_197@if ((lst = list) != @H_856_197@null && (a = lst.elementData) != @H_856_197@null) { 1373 @H_856_197@if ((hi = fencE) < 0) { 1374 mc = lst.modCount; 1375 hi = lst.size; 1376 } 1377 @H_856_197@else 1378 mc = expectedModCount; 1379 @H_856_197@if ((i = indeX) >= 0 && (index = hi) <= a.length) { 1380 @H_856_197@for (; i < hi; ++i) { 1381 @SuppressWarnings("unchecked") E e = (E) a[i]; 1382 action.accept(E); 1383 } 1384 @H_856_197@if (lst.modCount == mC) 1385 @H_856_197@return; 1386 } 1387 } 1388 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 1389 } 1390 1391 @H_856_197@public @H_856_197@long estimateSize() { 1392 @H_856_197@return (@H_856_197@long) (getFence() - indeX); 1393 } 1394 1395 @H_856_197@public @H_856_197@int characteristics() { 1396 @H_856_197@return Spliterator.ordered | Spliterator.SIZED | Spliterator.SUBSIZED; 1397 } 1398 } 1399 1400 @Override 1401 @H_856_197@public @H_856_197@Boolean removeIf(PreDicate<? @H_856_197@super E> filter) { 1402 Objects.requireNonNull(filter); 1403 // figure out which elements are to be removed 1404 // any exception thrown from the filter preDicate at this stage 1405 // will leave the collection unmodified 1406 @H_856_197@int removeCount = 0; 1407 @H_856_197@final BitSet removeSet = @H_856_197@new BitSet(sizE); 1408 @H_856_197@final @H_856_197@int expectedModCount = modCount; 1409 @H_856_197@final @H_856_197@int size = @H_856_197@this.size; 1410 @H_856_197@for (@H_856_197@int i=0; modCount == expectedModCount && i < size; i++) { 1411 @SuppressWarnings("unchecked") 1412 @H_856_197@final E element = (E) elementData[i]; 1413 @H_856_197@if (filter.test(element)) { 1414 removeSet.set(i); 1415 removeCount++; 1416 } 1417 } 1418 @H_856_197@if (modCount != expectedModCount) { 1419 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 1420 } 1421 1422 // shift surviving elements left over the spaces left by removed elements 1423 @H_856_197@final @H_856_197@Boolean anyToRemove = removeCount > 0; 1424 @H_856_197@if (anyToRemovE) { 1425 @H_856_197@final @H_856_197@int newSize = size - removeCount; 1426 @H_856_197@for (@H_856_197@int i=0, j=0; (i < sizE) && (j < newSizE); i++, j++) { 1427 i = removeSet.nextClearBit(i); 1428 elementData[j] = elementData[i]; 1429 } 1430 @H_856_197@for (@H_856_197@int k=newSize; k < size; k++) { 1431 elementData[k] = @H_856_197@null; // Let gc do its work 1432 } 1433 @H_856_197@this.size = newSize; 1434 @H_856_197@if (modCount != expectedModCount) { 1435 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 1436 } 1437 modCount++; 1438 } 1439 1440 @H_856_197@return anyToRemove; 1441 } 1442 1443 @Override 1444 @SuppressWarnings("unchecked") 1445 @H_856_197@public @H_856_197@void replaceAll(UnaryOperator<E> operator) { 1446 Objects.requireNonNull(operator); 1447 @H_856_197@final @H_856_197@int expectedModCount = modCount; 1448 @H_856_197@final @H_856_197@int size = @H_856_197@this.size; 1449 @H_856_197@for (@H_856_197@int i=0; modCount == expectedModCount && i < size; i++) { 1450 elementData[i] = operator.apply((E) elementData[i]); 1451 } 1452 @H_856_197@if (modCount != expectedModCount) { 1453 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 1454 } 1455 modCount++; 1456 } 1457 1458 @Override 1459 @SuppressWarnings("unchecked") 1460 @H_856_197@public @H_856_197@void sort(Comparator<? @H_856_197@super E> C) { 1461 @H_856_197@final @H_856_197@int expectedModCount = modCount; 1462 Arrays.sort((E[]) elementData, 0, size, c); 1463 @H_856_197@if (modCount != expectedModCount) { 1464 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 1465 } 1466 modCount++; 1467 } 1468 }View Code
添加元素时使用 ENsureCapacityInternal() 方法来保证容量足够,如果不够时,需要使用 grow() 方法进行扩容,新容量的大小为 oldCapacity + (oldCapacity >> 1)
,即 oldCapacity+oldCapacity/2。其中 oldCapacity >> 1 需要取整,所以新容量大约是旧容量的 1.5 倍左右。(oldCapacity 为偶数就是 1.5 倍,为奇数就是 1.5 倍-0.5)
扩容操作需要调用 Arrays.copyOf()
把原数组整个复制到新数组中,这个操作代价很高,因此最好在创建 ArrayList 对象时就指定大概的容量大小,减少扩容操作的次数。
1 @H_856_197@public @H_856_197@Boolean add(E E) { 2 ensureCapacityInternal(size + 1); // Increments modCount!! 3 elementData[size++] = e; 4 @H_856_197@return @H_856_197@true; 5 } 6 7 @H_856_197@private @H_856_197@void ensureCapacityInternal(@H_856_197@int minCapacity) { 8 @H_856_197@if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { 9 minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); 10 } 11 ensureExplicitCapacity(minCapacity); 12 } 13 14 @H_856_197@private @H_856_197@void ensureExplicitCapacity(@H_856_197@int minCapacity) { 15 modCount++; 16 // overflow-conscious code 17 @H_856_197@if (minCapacity - elementData.length > 0) 18 grow(minCapacity); 19 } 20 21 @H_856_197@private @H_856_197@void grow(@H_856_197@int minCapacity) { 22 // overflow-conscious code 23 @H_856_197@int oldCapacity = elementData.length; 24 @H_856_197@int newCapacity = oldCapacity + (oldCapacity >> 1); 25 @H_856_197@if (newCapacity - minCapacity < 0) 26 newCapacity = minCapacity; 27 @H_856_197@if (newCapacity - MAX_ARRAY_SIZE > 0) 28 newCapacity = hugeCapacity(minCapacity); 29 // minCapacity is usually close to size, so this is a win: 30 elementData = Arrays.copyOf(elementData, newCapacity); 31 }View Code
需要调用 System.arraycopy() 将 index+1 后面的元素都复制到 index 位置上,该操作的时间复杂度为 O(N),可以看到 ArrayList 删除元素的代价是非常高的。
@H_197_183@ @H_197_183@1 @H_856_197@public E remove(@H_856_197@int indeX) { 2 rangecheck(indeX); 3 modCount++; 4 E oldValue = elementData(indeX); 5 @H_856_197@int numMoved = size - index - 1; 6 @H_856_197@if (numMoved > 0) 7 System.arraycopy(elementData, index+1, elementData, index, numMoved); 8 elementData[--size] = @H_856_197@null; // clear to let GC do its work 9 @H_856_197@return oldValue; 10 }View Code
ArrayList 基于数组实现,并且具有动态扩容特性,因此保存元素的数组不一定都会被使用,那么就没必要全部进行序列化。
保存元素的数组 elementData 使用 transient 修饰,该关键字声明数组默认不会被序列化。
@H_197_183@@H_856_197@transient Object[] elementData; // non-private to simplify nested class access
ArrayList 实现了 writeObject() 和 readObject() 来控制只序列化数组中有元素填充那部分内容。
@H_197_183@ @H_197_183@1 @H_856_197@private @H_856_197@void readObject(java.io.objecTinputStream s) 2 @H_856_197@throws java.io.IOException, ClassnotFoundException { 3 elementData = EMPTY_ELEMENTDATA; 4 5 // Read in size, and any hidden stuff 6 s.defaultReadObject(); 7 8 // Read in capacity 9 s.readInt(); // ignored 10 11 @H_856_197@if (size > 0) { 12 // be like clone(), allocate array based upon size not capacity 13 ensureCapacityInternal(sizE); 14 15 Object[] a = elementData; 16 // Read in all elements in the proper order. 17 @H_856_197@for (@H_856_197@int i=0; i<size; i++) { 18 a[i] = s.readObject(); 19 } 20 } 21 }View Code @H_197_183@ @H_197_183@
1 @H_856_197@private @H_856_197@void writeObject(java.io.objectOutputStream s) 2 @H_856_197@throws java.io.IOException{ 3 // Write out element count, and any hidden stuff 4 @H_856_197@int expectedModCount = modCount; 5 s.defaultWriteObject(); 6 7 // Write out size as capacity for behavioural compatibility with clone() 8 s.writeInt(sizE); 9 10 // Write out all elements in the proper order. 11 @H_856_197@for (@H_856_197@int i=0; i<size; i++) { 12 s.writeObject(elementData[i]); 13 } 14 15 @H_856_197@if (modCount != expectedModCount) { 16 @H_856_197@throw @H_856_197@new ConcurrentModificationException(); 17 } 18 }View Code
序列化时需要使用 ObjectOutputStream 的 writeObject() 将对象转换为字节流并输出。而 writeObject() 方法在传入的对象存在 writeObject() 的时候会去反射调用该对象的 writeObject() 来实现序列化。反序列化使用的是 ObjecTinputStream 的 readObject() 方法,原理类似。
@H_197_183@1 ArrayList list = @H_856_197@new ArrayList(); 2 ObjectOutputStream oos = @H_856_197@new ObjectOutputStream(@H_856_197@new FiLeoutputStream(filE)); 3 oos.writeObject(list);
@H_414_21@modCount 用来记录 ArrayList 结构发生变化的次数。结构发生变化是指添加或者删除至少一个元素的所有操作,或者是调整内部数组的大小,仅仅只是设置元素的值不算结构发生变化。
在进行序列化或者迭代等操作时,需要比较操作前后 modCount 是否改变,如果改变了需要抛出 ConcurrentModificationException。代码参以上序列化中的 writeObject() 方法。
它的实现与 ArrayList 类似,但是使用了 synchronized 进行同步。
@H_197_183@ @H_197_183@1 @H_856_197@public @H_856_197@synchronized @H_856_197@Boolean add(E E) { 2 modCount++; 3 ensureCapacityHelper(elementCount + 1); 4 elementData[elementCount++] = e; 5 @H_856_197@return @H_856_197@true; 6 } 7 8 @H_856_197@public @H_856_197@synchronized E get(@H_856_197@int indeX) { 9 @H_856_197@if (index >= elementCount) 10 @H_856_197@throw @H_856_197@new ArrayIndexOutOfBoundsException(indeX); 11 12 @H_856_197@return elementData(indeX); 13 }View Code
Vector 的构造函数可以传入 capacityIncrement 参数,它的作用是在扩容时使容量 capacity 增长 capacityIncrement。如果这个参数的值小于等于 0,扩容时每次都令 capacity 为原来的两倍。
@H_197_183@ @H_197_183@1 @H_856_197@public Vector(@H_856_197@int initialCapacity, @H_856_197@int capacityIncrement) { 2 @H_856_197@super(); 3 @H_856_197@if (initialCapacity < 0) 4 @H_856_197@throw @H_856_197@new IllegalArgumentexception("Illegal Capacity: "+ 5 initialCapacity); 6 @H_856_197@this.elementData = @H_856_197@new Object[initialCapacity]; 7 @H_856_197@this.capacityIncrement = capacityIncrement; 8 }View Code @H_197_183@ @H_197_183@
1 @H_856_197@private @H_856_197@void grow(@H_856_197@int minCapacity) { 2 // overflow-conscious code 3 @H_856_197@int oldCapacity = elementData.length; 4 @H_856_197@int newCapacity = oldCapacity + ((capacityIncrement > 0) ? 5 capacityIncrement : oldCapacity); 6 @H_856_197@if (newCapacity - minCapacity < 0) 7 newCapacity = minCapacity; 8 @H_856_197@if (newCapacity - MAX_ARRAY_SIZE > 0) 9 newCapacity = hugeCapacity(minCapacity); 10 elementData = Arrays.copyOf(elementData, newCapacity); 11 }View Code
调用没有 capacityIncrement 的构造函数时,capacityIncrement 值被设置为 0,也就是说默认情况下 Vector 每次扩容时容量都会翻倍。
@H_197_183@ @H_197_183@1 @H_856_197@public Vector(@H_856_197@int initialCapacity) { 2 @H_856_197@this(initialCapacity, 0); 3 } 4 5 @H_856_197@public Vector() { 6 @H_856_197@this(10); 7 }View Code
可以使用 Collections.synchronizedList();
得到一个线程安全的 ArrayList。
1 List<String> list = @H_856_197@new ArrayList<>(); 2 List<String> synList = Collections.synchronizedList(list);
也可以使用 concurrent 并发包下的 CopyOnWriteArrayList 类。
@H_197_183@List<String> list = @H_856_197@new CopyOnWriteArrayList<>();
写操作在一个复制的数组上进行,读操作还是在原始数组中进行,读写分离,互不影响。
写操作需要加锁,防止并发写入时导致写入数据丢失。
写操作结束之后需要把原始数组指向新的复制数组。
@H_197_183@ @H_197_183@1 @H_856_197@public @H_856_197@Boolean add(E E) { 2 @H_856_197@final ReentrantLock lock = @H_856_197@this.lock; 3 lock.lock(); 4 @H_856_197@try { 5 Object[] elements = getArray(); 6 @H_856_197@int len = elements.length; 7 Object[] newElements = Arrays.copyOf(elements, len + 1); 8 newElements[len] = e; 9 setArray(newElements); 10 @H_856_197@return @H_856_197@true; 11 } @H_856_197@finally { 12 lock.unlock(); 13 } 14 } 15 16 @H_856_197@final @H_856_197@void setArray(Object[] a) { 17 array = a; 18 }View Code @H_197_183@
1 @SuppressWarnings("unchecked") 2 @H_856_197@private E get(Object[] a, @H_856_197@int indeX) { 3 @H_856_197@return (E) a[index]; 4 }
CopyOnWriteArrayList 在写操作的同时允许读操作,大大提高了读操作的性能,因此很适合读多写少的应用场景。
但是 CopyOnWriteArrayList 有其缺陷:
所以 CopyOnWriteArrayList 不适合内存敏感以及对实时性要求很高的场景。
基于双向链表实现,使用 Node 存储链表节点信息。
@H_197_183@ @H_197_183@1 @H_856_197@private @H_856_197@static @H_856_197@class Node<E> { 2 E item; 3 Node<E> next; 4 Node<E> prev; 5 }View Code
每个链表存储了 first 和 last 指针:
@H_197_183@1 @H_856_197@transient Node<E> first; 2 @H_856_197@transient Node<E> last;
ArrayList 基于动态数组实现,LinkedList 基于双向链表实现。ArrayList 和 LinkedList 的区别可以归结为数组和链表的区别:
https://www.cnblogs.com/taojietaoge/p/11359542.html
参考链接:https://www.cnblogs.com/taojietaoge/p/10301711.html
@H_197_183@
1 @H_856_197@static @H_856_197@final @H_856_197@class HashEntry<K,V> { 2 @H_856_197@final @H_856_197@int hash; 3 @H_856_197@final K key; 4 @H_856_197@volatile V value; 5 @H_856_197@volatile HashEntry<K,V> next; 6 }
ConcurrentHashMap 和 HashMap 实现上类似,最主要的差别是 ConcurrentHashMap 采用了分段锁(Segment),每个分段锁维护着几个桶(HashEntry),多个线程可以同时访问不同分段锁上的桶,从而使其并发度更高(并发度就是 Segment 的个数)。
Segment 继承自 ReentrantLock。
@H_197_183@ @H_197_183@1 @H_856_197@static @H_856_197@final @H_856_197@class Segment<K,V> @H_856_197@extends ReentrantLock @H_856_197@implements serializable { 2 3 @H_856_197@private @H_856_197@static @H_856_197@final @H_856_197@long serialVersionUID = 2249069246763182397L; 4 5 @H_856_197@static @H_856_197@final @H_856_197@int MAX_SCAN_RETRIES = 6 Runtime.getRuntime().availableProcessors() > 1 ? 64 : 1; 7 8 @H_856_197@transient @H_856_197@volatile HashEntry<K,V>[] table; 9 10 @H_856_197@transient @H_856_197@int count; 11 12 @H_856_197@transient @H_856_197@int modCount; 13 14 @H_856_197@transient @H_856_197@int threshold; 15 16 @H_856_197@final @H_856_197@float loadFactor; 17 }View Code @H_197_183@
@H_856_197@final Segment<K,V>[] segments;
默认的并发级别为 16,也就是说默认创建 16 个 Segment。
@H_197_183@@H_856_197@static @H_856_197@final @H_856_197@int DEFAULT_CONCURRENCY_LEVEL = 16;
每个 Segment 维护了一个 count 变量来统计该 Segment 中的键值对个数。
@H_197_183@ @H_197_183@1 /**
2 * the number of elements. Accessed only either within locks
3 * or among other volatile reads that maintain visibility.
4 */
5 @H_856_197@transient @H_856_197@int count;
View Code
在执行 size 操作时,需要遍历所有 Segment 然后把 count 累计起来。
ConcurrentHashMap 在执行 size 操作时先尝试不加锁,如果连续两次不加锁操作得到的结果一致,那么可以认为这个结果是正确的。
尝试次数使用 RETRIES_BEFORE_LOCK 定义,该值为 2,retries 初始值为 -1,因此尝试次数为 3。
如果尝试的次数超过 3 次,就需要对每个 Segment 加锁。
@H_197_183@ @H_197_183@1 /** 2 * number of unsynchronized retries in size and containsValue 3 * methods before resorTing to locking. This is used to avoid 4 * unbounded retries if tables undergo conTinuous modification 5 * which would make it impossible to obtain an accurate result. 6 */ 7 @H_856_197@static @H_856_197@final @H_856_197@int RETRIES_BEFORE_LOCK = 2; 8 9 @H_856_197@public @H_856_197@int size() { 10 // Try a few times to get accurate count. On failure due to 11 // conTinuous async changes in table, resort to locking. 12 @H_856_197@final Segment<K,V>[] segments = @H_856_197@this.segments; 13 @H_856_197@int size; 14 @H_856_197@Boolean overflow; // true if size overflows 32 bits 15 @H_856_197@long sum; // sum of modCounts 16 @H_856_197@long last = 0L; // previous sum 17 @H_856_197@int retries = -1; // first iteration isn't retry 18 @H_856_197@try { 19 @H_856_197@for (;;) { 20 // 超过尝试次数,则对每个 Segment 加锁 21 @H_856_197@if (retries++ == RETRIES_BEFORE_LOCK) { 22 @H_856_197@for (@H_856_197@int j = 0; j < segments.length; ++j) 23 ensureSegment(j).lock(); // force creation 24 } 25 sum = 0L; 26 size = 0; 27 overflow = @H_856_197@false; 28 @H_856_197@for (@H_856_197@int j = 0; j < segments.length; ++j) { 29 Segment<K,V> seg = segmentAt(segments, j); 30 @H_856_197@if (seg != @H_856_197@null) { 31 sum += seg.modCount; 32 @H_856_197@int c = seg.count; 33 @H_856_197@if (c < 0 || (size += C) < 0) 34 overflow = @H_856_197@true; 35 } 36 } 37 // 连续两次得到的结果一致,则认为这个结果是正确的 38 @H_856_197@if (sum == last) 39 @H_856_197@break; 40 last = sum; 41 } 42 } @H_856_197@finally { 43 @H_856_197@if (retries > RETRIES_BEFORE_LOCK) { 44 @H_856_197@for (@H_856_197@int j = 0; j < segments.length; ++j) 45 segmentAt(segments, j).unlock(); 46 } 47 } 48 @H_856_197@return overflow ? Integer.max_value : size; 49 }View Code
JDK 1.7 使用分段锁机制来实现并发更新操作,核心类为 Segment,它继承自重入锁 ReentrantLock,并发度与 Segment 数量相等。
JDK 1.8 使用了 CAS 操作来支持更高的并发度,在 CAS 操作失败时使用内置锁 synchronized。
并且 JDK 1.8 的实现也在链表过长时会转换为红黑树。
继承自 HashMap,因此具有和 HashMap 一样的快速查找特性。
@H_197_183@@H_856_197@public @H_856_197@class LinkedHashMap<K,V> @H_856_197@extends HashMap<K,V> @H_856_197@implements Map<K,V>
内部维护了一个双向链表,用来维护插入顺序或者 LRU 顺序。
@H_197_183@ @H_197_183@1 /** 2 * The head (eldest) of the doubly linked list. 3 */ 4 @H_856_197@transient LinkedHashMap.Entry<K,V> head; 5 6 /** 7 * The tail (youngest) of the doubly linked list. 8 */ 9 @H_856_197@transient LinkedHashMap.Entry<K,V> tail;View Code
accessOrder 决定了顺序,默认为 false,此时维护的是插入顺序。
@H_197_183@@H_856_197@final @H_856_197@Boolean accessOrder;
LinkedHashMap 最重要的是以下用于维护顺序的函数,它们会在 put、get 等方法中调用。
@H_197_183@1 @H_856_197@void afterNodeAccess(Node<K,V> p) { } 2 @H_856_197@void afterNodeInsertion(@H_856_197@Boolean evict) { }
当一个节点被访问时,如果 accessOrder 为 true,则会将该节点移到链表尾部。也就是说指定为 LRU 顺序之后,在每次访问一个节点时,会将这个节点移到链表尾部,保证链表尾部是最近访问的节点,那么链表首部就是最近最久未使用的节点。
@H_197_183@ @H_197_183@ @H_197_183@1 @H_856_197@void afterNodeAccess(Node<K,V> E) { // move node to last 2 LinkedHashMap.Entry<K,V> last; 3 @H_856_197@if (accessOrder && (last = tail) != E) { 4 LinkedHashMap.Entry<K,V> p = 5 (LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after; 6 p.after = @H_856_197@null; 7 @H_856_197@if (b == @H_856_197@null) 8 head = a; 9 @H_856_197@else 10 b.after = a; 11 @H_856_197@if (a != @H_856_197@null) 12 a.before = b; 13 @H_856_197@else 14 last = b; 15 @H_856_197@if (last == @H_856_197@null) 16 head = p; 17 @H_856_197@else { 18 p.before = last; 19 last.after = p; 20 } 21 tail = p; 22 ++@H_643_199@modCount; 23 } 24 }View Code
在 put 等操作之后执行,当 removeEldestEntry() 方法返回 true 时会移除最晚的节点,也就是链表首部节点 first。
evict 只有在构建 Map 的时候才为 false,在这里为 true。
@H_197_183@ @H_197_183@1 @H_856_197@void afterNodeInsertion(@H_856_197@Boolean evict) { // possibly remove eldest 2 LinkedHashMap.Entry<K,V> first; 3 @H_856_197@if (evict && (first = head) != @H_856_197@null && removeEldestEntry(first)) { 4 K key = first.key; 5 removeNode(hash(key), key, @H_856_197@null, @H_856_197@false, @H_856_197@true); 6 } 7 }View Code
removeEldestEntry() 默认为 false,如果需要让它为 true,需要继承 LinkedHashMap 并且覆盖这个方法的实现,这在实现 LRU 的缓存中特别有用,通过移除最近最久未使用的节点,从而保证缓存空间足够,并且缓存的数据都是热点数据。
@H_197_183@1 @H_856_197@protected @H_856_197@Boolean removeEldestEntry(Map.Entry<K,V> eldest) { 2 @H_856_197@return @H_856_197@false; 3 }
以下是使用 LinkedHashMap 实现的一个 LRU 缓存:
1 @H_856_197@class LRUCache<K, V> @H_856_197@extends LinkedHashMap<K, V> { 2 @H_856_197@private @H_856_197@static @H_856_197@final @H_856_197@int MAX_ENTRIES = 3; 3 4 @H_856_197@protected @H_856_197@Boolean removeEldestEntry(Map.Entry eldest) { 5 @H_856_197@return size() > MAX_ENTRIES; 6 } 7 8 LRUCache() { 9 @H_856_197@super(MAX_ENTRIES, 0.75f, @H_856_197@true); 10 } 11 }View Code @H_197_183@ @H_197_183@
1 @H_856_197@public @H_856_197@static @H_856_197@void main(String[] args) { 2 LRUCache<Integer, String> cache = @H_856_197@new LRUCache<>(); 3 cache.put(1, "a"); 4 cache.put(2, "b"); 5 cache.put(3, "c"); 6 cache.get(1); 7 cache.put(4, "d"); 8 System.out.println(cache.keySet()); // [3, 1, 4] 9 }View Code
WeakHashMap 的 Entry 继承自 WeakReference,被 WeakReference 关联的对象在下一次垃圾回收时会被回收。
WeakHashMap 主要用来实现缓存,通过使用 WeakHashMap 来引用缓存对象,由 JVM 对这部分缓存进行回收。
@H_197_183@@H_856_197@private @H_856_197@static @H_856_197@class Entry<K,V> @H_856_197@extends WeakReference<Object> @H_856_197@implements Map.Entry<K,V>
tomcat 中的 ConcurrentCache 使用了 WeakHashMap 来实现缓存功能。
ConcurrentCache 采取的是分代缓存:
1 @H_856_197@public @H_856_197@final @H_856_197@class ConcurrentCache<K, V> { 2 3 @H_856_197@private @H_856_197@final @H_856_197@int size; 4 5 @H_856_197@private @H_856_197@final Map<K, V> eden; 6 7 @H_856_197@private @H_856_197@final Map<K, V> longterm; 8 9 @H_856_197@public ConcurrentCache(@H_856_197@int sizE) { 10 @H_856_197@this.size = size; 11 @H_856_197@this.eden = @H_856_197@new ConcurrentHashMap<>(sizE); 12 @H_856_197@this.longterm = @H_856_197@new WeakHashMap<>(sizE); 13 } 14 15 @H_856_197@public V get(K k) { 16 V v = @H_856_197@this.eden.get(k); 17 @H_856_197@if (v == @H_856_197@null) { 18 v = @H_856_197@this.longterm.get(k); 19 @H_856_197@if (v != @H_856_197@null) 20 @H_856_197@this.eden.put(k, v); 21 } 22 @H_856_197@return v; 23 } 24 25 @H_856_197@public @H_856_197@void put(K k, V v) { 26 @H_856_197@if (@H_856_197@this.eden.size() >= sizE) { 27 @H_856_197@this.longterm.putAll(@H_856_197@this.eden); 28 @H_856_197@this.eden.clear(); 29 } 30 @H_856_197@this.eden.put(k, v); 31 } 32 }View Code
@H_197_183@
以上是大佬教程为你收集整理的备战-Java 容器全部内容,希望文章能够帮你解决备战-Java 容器所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。