Returns a 0-length array or collection instead of null--effective Java reading notes

Source: Internet
Author: User

/** * Returns a 0-length array or collection instead of NULL * * @author Liu Xiangfeng * */ Public  class  Shop {    PrivateList<object> objectList;//0 Length array constants are passed to the ToArray method to indicate the expected return type    Private Final Staticobject[] Empty_object_array =Newobject[0]; Public  Shop(list<object> objectList) {Super(); This. objectList = objectList; }/** * * @return An array containing all of the objects in the shop */     PublicObject[]getobjects() {//If the collection is empty, he will use a 0-length input array--otherwise, this practice never assigns an array of 0 lengths        returnObjectlist.toarray (Empty_object_array); }/** * * @return A copy of collection */     PublicList<object>getstringlist() {if(Objectlist.isempty ()) {returnCollections.emptylist (); }Else{return NewArraylist<object> (objectList); }    }/** * Test method * * @param args */     Public Static void Main(string[] args) {list<object> newobjectlist = collections.emptylist ();//Newobjectlist = new arraylist<object> ();Object object =NewObject ();//Newobjectlist.add (object);Shop shop =NewShop (newobjectlist);//Do not need to determine if the value of shop.getobjects () is empty        if(Arrays.aslist (Shop.getobjects ()). Contains (object)) {System.out.println ("Jolly good, just the thing"); }    }}

Returns a 0-length array or collection instead of null--effective Java reading notes

Related Article

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.