Official account: Java Xiaokaxiu, website: Javaxks.com
Author: shen stranger, link: blog.csdn.net/zwwhnly/art…
1. Precautions for using Arrays.asList
1.1 Possible pits to step on
Let’s take a look at arrays.aslist:
List<Integer> statusList = Arrays.asList(1, 2);
System.out.println(statusList);
System.out.println(statusList.contains(1));
System.out.println(statusList.contains(3));
Copy the code
The output result is as follows:
Then, add element 3 to the statusList as follows:
statusList.add(3);
System.out.println(statusList.contains(3));
Copy the code
Output the desired results, should be true, but the actual is thrown the Java. Lang. UnsupportedOperationException anomaly:
Can not help but wonder, just add an element, why throw such an exception, it is not scientific ah.
1.2 Cause Analysis
With that in mind, let’s take a look at the source code for the asList static methods provided by the Arrays class:
ArrayList (java.util); java.util (java.util); java.util (java.util); java.util (java.util); java.util (java.util);
But the ArrayList here is an inner class of the Arrays class:
It also inherited class AbstractList, rewriting a lot of methods, for example, we use the contains method above, but not rewrite the add method, so we call the add method to sell Java lang. UnsupportedOperationException anomalies.
About this point, in the “Alibaba Java Development Manual” Taishan edition, also mentioned:
Use tools Arrays. AsList () to convert the array collection, cannot use the modify the related methods of collection, it’s the add/remove/clear method will throw an UnsupportedOperationException anomalies.
So be careful when using Arrays. AsList.
1.3 summarize
The arrays.aslist method can be used for simple things like quickly declaring a collection to determine if a value is in the allowed range:
But after the statement don’t call the add method such as modify the collection, otherwise you will quote Java. Lang. UnsupportedOperationException anomalies.
2. Considerations for using subList with ArrayList
Let’s start with a simple use of subList:
List<String> bookList = new ArrayList<>(); Booklist.add (" Savior far away "); BookList. Add (" betrayal "); Booklist.add (" Sky dust "); BookList. Add (" life "); Booklist.add (" Ordinary World "); List<String> luyaoBookList = bookList.subList(3, 5); System.out.println(bookList); System.out.println(luyaoBookList);Copy the code
The running results are as follows:
As you can see, subList returns a collection of elements indexed from fromIndex (included) to toIndex (not included) in the bookList.
It’s easy to use, and it’s easy to understand, but there are a few things to be aware of that can cause errors or exceptions:
- Modifying the value of the original collection element affects the subcollection
- Modifying the structure of the original collection causes
ConcurrentModificationException
abnormal - Modifying the value of a subcollection element affects the original collection
- Modifying the structure of a subset affects the original collection
The above points are described like this in the “Alibaba Java Development Manual” Taishan edition:
2.1 Modifying the value of the original set will affect the sub-set
For example, if we modify the value of an element in the original collection bookList (unstructured) :
List<String> bookList = new ArrayList<>(); Booklist.add (" Savior far away "); BookList. Add (" betrayal "); Booklist.add (" Sky dust "); BookList. Add (" life "); Booklist.add (" Ordinary World "); List<String> luyaoBookList = bookList.subList(3, 5); System.out.println(bookList); System.out.println(luyaoBookList); Booklist. set(3," luyao-life "); // Set (3," Luyao-life "); System.out.println(bookList); System.out.println(luyaoBookList);Copy the code
The running result is as follows:
As you can see, we only changed the value of the original collection, bookList, but affected the subcollection, luyaoBookList.
2.2 Modifying the structure of the original set will causeConcurrentModificationException
abnormal
For example, we add an element (structural modification) to the original collection bookList:
List<String> bookList = new ArrayList<>(); Booklist.add (" Savior far away "); BookList. Add (" betrayal "); Booklist.add (" Sky dust "); BookList. Add (" life "); Booklist.add (" Ordinary World "); List<String> luyaoBookList = bookList.subList(3, 5); System.out.println(bookList); System.out.println(luyaoBookList); // Add booklist.add (" morning starts at noon ") to the original collection; System.out.println(bookList); System.out.println(luyaoBookList);Copy the code
The running result is as follows:
As you can see, when we add elements to the original set (structural modifications), after the traversal subset fit ConcurrentModificationException exception occurred.
Note: The above exception did not occur when the element was added, but when the subset was iterated after the element was added.
On this point, in the “Alibaba Java Development Manual” taishan edition is described as follows:
2.3 Modifying the value of the sub-set will affect the original set
For example, we modify the value of an element in the subset luyaoBookList (unstructured) :
List<String> bookList = new ArrayList<>(); Booklist.add (" Savior far away "); BookList. Add (" betrayal "); Booklist.add (" Sky dust "); BookList. Add (" life "); Booklist.add (" Ordinary World "); List<String> luyaoBookList = bookList.subList(3, 5); System.out.println(bookList); System.out.println(luyaoBookList); // Change the value of the subset luyaobooklist. set(1," Luyao - Ordinary World "); System.out.println(bookList); System.out.println(luyaoBookList);Copy the code
The running result is as follows:
As you can see, we only changed the value of the subcollection luyaoBookList, but we affected the original collection bookList.
2.4 Modifying the structure of sub-sets will affect the original set
For example, we add an element (structural modification) to the subset luyaoBookList:
List<String> bookList = new ArrayList<>(); Booklist.add (" Savior far away "); BookList. Add (" betrayal "); Booklist.add (" Sky dust "); BookList. Add (" life "); Booklist.add (" Ordinary World "); List<String> luyaoBookList = bookList.subList(3, 5); System.out.println(bookList); System.out.println(luyaoBookList); // Add the element luyaobooklist. add(" morning starts at noon ") to the subcollection; System.out.println(bookList); System.out.println(luyaoBookList);Copy the code
The running result is as follows:
As you can see, when we added elements (structural changes) to the subcollection, we affected the original collection bookList.
2.5 Cause Analysis
First, let’s take a look at the comments for the subList method to see what it does:
Returns a view of the portion of this list between the specified {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
Translation:
Returns a view of the list section between the specified {@code fromIndex}(included) and {@code toIndex}(excluded).
Then, let’s take a look at its source code:
public List<E> subList(int fromIndex, int toIndex) {
subListRangeCheck(fromIndex, toIndex, size);
return new SubList(this, 0, fromIndex, toIndex);
}
Copy the code
As you can see, it calls the SubList constructor, the source of which is shown below:
As you can see, the SubList class is an inner class of the ArrayList. The constructor does not create a new ArrayList, so changing the values of elements in the original collection or subcollection will affect each other.
2.6 summarize
ArrayList subList method, return is a child in the original set (view), the structural changes the value of the a set of arbitrary element will influence each other, and structural modification of the original collection, will quote ConcurrentModificationException anomalies, Structural changes to subsets affect the original collection, so use them with care to avoid errors or exceptions.
Reference 3.
Use the subList method in ArrayList with caution
“Alibaba Java development manual” Taishan edition