Java Collection (4): Unsupported Operations and Unsupportedoperationexception

Source: Internet
Author: User
Tags addall unsupported

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.