Java集合(4):未获支持的操作及UnsupportedOperationException

执行各种添加和移除的方法在Collection中都是可选操作的,这意味着实现类并不需要为这些方法提供实现。当我们调用这些方法时,将不会执行有意义的行为,而是通常抛出UnsupportedOperationException。

下面例子我们可以看出Collection的各种实现类中:

(1) java.util.ArrayList实现了所有Collection接口。

(2) java.util.Arrays.ArrayList不支持对对象的add,remove,clear,但是支持set修改元素值。这是因为Arrays.asList返回固定尺寸的List

(3) java.util.Collections.UnmodifiableList产生的是read-only的List,所以不能做任何修改

 

 1 import java.util.ArrayList;
 2 import java.util.Arrays;
 3 import java.util.Collection;
 4 import java.util.Collections;
 5 import java.util.List;
 6 
 7 class Unsupported {
 8     static void test(String msg, List<String> list) {
 9         System.out.println("--- " + msg + " ---");
10         Collection<String> c = list;
11         Collection<String> subList = list.subList(1, 8);
12         Collection<String> c2 = new ArrayList<String>(subList);
13         try {
14             c.retainAll(c2);
15             System.out.println("retainAll(): SUCCESS!");
16         } catch (Exception e) {
17             System.out.println("retainAll(): " + e);
18         }
19         try {
20             c.removeAll(c2);
21             System.out.println("removeAll(): SUCCESS!");
22         } catch (Exception e) {
23             System.out.println("removeAll(): " + e);
24         }
25         try {
26             c.clear();
27             System.out.println("clear(): SUCCESS!");
28         } catch (Exception e) {
29             System.out.println("clear(): " + e);
30         }
31         try {
32             c.add("X");
33             System.out.println("add(): SUCCESS!");
34         } catch (Exception e) {
35             System.out.println("add(): " + e);
36         }
37         try {
38             c.addAll(c2);
39             System.out.println("addAll(): SUCCESS!");
40         } catch (Exception e) {
41             System.out.println("addAll(): " + e);
42         }
43         try {
44             c.remove("C");
45             System.out.println("remove(): SUCCESS!");
46         } catch (Exception e) {
47             System.out.println("remove(): " + e);
48         }
49         try {
50             list.set(0, "X");
51             System.out.println("List.set(): SUCCESS!");
52         } catch (Exception e) {
53             System.out.println("List.set(): " + e);
54         }
55     }
56 }
57 
58 public class Test6 {
59     public static void main(String[] args) {
60         List<String> list = Arrays.asList("A B C D E F G H I J K L".split(" "));
61         Unsupported.test("Modifiable Copy", new ArrayList<String>(list));
62         Unsupported.test("Arrays.asList()", list);
63         Unsupported.test("unmodifiableList()", Collections.unmodifiableList(new ArrayList<String>(list)));
64         // Output:
65         // --- Modifiable Copy ---
66         // retainAll(): SUCCESS!
67         // removeAll(): SUCCESS!
68         // clear(): SUCCESS!
69         // add(): SUCCESS!
70         // addAll(): SUCCESS!
71         // remove(): SUCCESS!
72         // List.set(): SUCCESS!
73         // --- Arrays.asList() ---
74         // retainAll(): java.lang.UnsupportedOperationException
75         // removeAll(): java.lang.UnsupportedOperationException
76         // clear(): java.lang.UnsupportedOperationException
77         // add(): java.lang.UnsupportedOperationException
78         // addAll(): java.lang.UnsupportedOperationException
79         // remove(): java.lang.UnsupportedOperationException
80         // List.set(): SUCCESS!
81         // --- unmodifiableList() ---
82         // retainAll(): java.lang.UnsupportedOperationException
83         // removeAll(): java.lang.UnsupportedOperationException
84         // clear(): java.lang.UnsupportedOperationException
85         // add(): java.lang.UnsupportedOperationException
86         // addAll(): java.lang.UnsupportedOperationException
87         // remove(): java.lang.UnsupportedOperationException
88         // List.set(): java.lang.UnsupportedOperationException
89     }
90 }

 

posted @ 2018-02-09 10:14  Storm_L  阅读(2817)  评论(0编辑  收藏  举报