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

Posted Storm_L

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java集合:未获支持的操作及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 }

 

以上是关于Java集合:未获支持的操作及UnsupportedOperationException的主要内容,如果未能解决你的问题,请参考以下文章

Java容器

Trie树题目模板及java代码

吴秀波演唱曲目被指未获授权 《跨界歌王》疑侵权

索引及集合管理 - MongoDB从入门到删库

Redis支持的五种数据类型及相应操作:String(字符串),Hash(哈希),List(列表),Set(集合)及zset(sorted set:有序集合)

Java中Jedis操作Redis与Spring的整合