The various methods of adding and removing are optional in collection, which means that implementation classes do not need to provide implementations for these methods. When we call these methods, we do not perform meaningful behavior, but we usually throw unsupportedoperationexception.
In the following example we can see the various implementation classes of collection:
(1) The java.util.ArrayList implements all collection interfaces.
(2) java.util.Arrays.ArrayList does not support add,remove,clear of objects, but it supports set to modify element values. This is because Arrays.aslist returns a fixed-size list.
(3) java.util.Collections.UnmodifiableList produces a list of read-only, so you can't make any changes .
1 Importjava.util.ArrayList;2 Importjava.util.Arrays;3 Importjava.util.Collection;4 Importjava.util.Collections;5 Importjava.util.List;6 7 classUnsupported {8 Static voidTest (String msg, list<string>list) {9SYSTEM.OUT.PRINTLN ("---" + msg + "---");Tencollection<string> C =list; Onecollection<string> sublist = list.sublist (1, 8); Acollection<string> C2 =NewArraylist<string>(sublist); - Try { - C.retainall (C2); theSystem.out.println ("Retainall (): success!"); -}Catch(Exception e) { -System.out.println ("Retainall ():" +e); - } + Try { - C.removeall (C2); +System.out.println ("RemoveAll (): success!"); A}Catch(Exception e) { atSystem.out.println ("RemoveAll ():" +e); - } - Try { - c.clear (); -System.out.println ("Clear (): success!"); -}Catch(Exception e) { inSystem.out.println ("Clear ():" +e); - } to Try { +C.add ("X"); -System.out.println ("Add (): success!"); the}Catch(Exception e) { *System.out.println ("Add ():" +e); $ }Panax Notoginseng Try { - C.addall (C2); theSystem.out.println ("AddAll (): success!"); +}Catch(Exception e) { ASystem.out.println ("AddAll ():" +e); the } + Try { -C.remove ("C"); $System.out.println ("Remove (): success!"); $}Catch(Exception e) { -System.out.println ("Remove ():" +e); - } the Try { -List.set (0, "X");WuyiSystem.out.println ("List.set (): success!"); the}Catch(Exception e) { -System.out.println ("List.set ():" +e); Wu } - } About } $ - Public classTest6 { - Public Static voidMain (string[] args) { -list<string> list = arrays.aslist ("A B C D E F G H I J K L". Split ("")); AUnsupported.test ("Modifiable Copy",NewArraylist<string>(list)); +Unsupported.test ("Arrays.aslist ()", list); theUnsupported.test ("Unmodifiablelist ()", Collections.unmodifiablelist (NewArraylist<string>(list)); - //Output: $ //---modifiable Copy--- the //Retainall (): success! the //RemoveAll (): success! the //Clear (): success! the //Add (): success! - //AddAll (): success! in //Remove (): success! the //List.set (): success! the //---arrays.aslist ()--- About //Retainall (): Java.lang.UnsupportedOperationException the //RemoveAll (): Java.lang.UnsupportedOperationException the //Clear (): Java.lang.UnsupportedOperationException the //Add (): Java.lang.UnsupportedOperationException + //AddAll (): Java.lang.UnsupportedOperationException - //Remove (): Java.lang.UnsupportedOperationException the //List.set (): success!Bayi //---unmodifiablelist ()--- the //Retainall (): Java.lang.UnsupportedOperationException the //RemoveAll (): Java.lang.UnsupportedOperationException - //Clear (): Java.lang.UnsupportedOperationException - //Add (): Java.lang.UnsupportedOperationException the //AddAll (): Java.lang.UnsupportedOperationException the //Remove (): Java.lang.UnsupportedOperationException the //List.set (): Java.lang.UnsupportedOperationException the } -}
Java Collection (4): Unsupported Operations and Unsupportedoperationexception