In the previous section we talked about the common methods of collection, as well as the previous chapters where we introduced the AddAll method of collection, which we introduced to its point of attention.
Note that, in the common method, the basic is optional operation.
What is an optional operation?
Unfortunately, the method of operation is not set for all implementations, but only for a certain class of implementation.
For example:
Package Com.ray.ch15;import Java.util.arrays;import Java.util.collection;import Java.util.list;public class Test { public static void Test (list<string> List, String msg) {System.out.println ("---------" + msg + "--------"); list<string> sublist = list.sublist (1, 4); Collection<string> Collection = list;try {collection.retainall (sublist);} catch (Exception e) { System.out.println ("Retainall:" + E);}} public static void Main (string[] args) {list<string> list = Arrays.aslist ("1", "2", "3", "4", "5", "6"), Test (list, " Start1 ");}}
Output:
---------Start1--------
RetainAll:java.lang.UnsupportedOperationException
Why does it throw an exception?
Because Arrays.aslist is the lowest-level data structure of this method is an array, the array in Java is not modifiable, when retainall this method to modify the data structure inside the array, it is not allowed.
If we output a bit more exception information above, we will locate the Abstractlist remove method, and when it is called, it will throw an unsupported exception.
Public E Remove (int index) {throw new unsupportedoperationexception (); }
Summary: This chapter focuses on the optional operation of the common methods of collection.
This chapter is here, thank you.
-----------------------------------
Directory
A common method of understanding java-15.2 collection from the Beginning (2)-Attention point