How to iterate an object object if it could be a collection or an array

Source: Internet
Author: User

Requirements: A method passed in a parameter that is of type object (assuming the object is "items", using the object type is also used to increase the method reusability using polymorphism), but it is known that the object may be a collection, including collection and map, or an array, This includes an array of object types and an array of basic data types, so how to iterate over the elements in this object and add them to a collection (we don't care what type of value is added to the iteration).

Analysis: Since the Object object (which is assumed to be items) may be a collection of collection, a map collection, an array of object types, or an array of primitive data types, however, a collection or array cannot escape these four types. For an iteration of an array of object types and an array of primitive types, see a blog post.

So before we iterate, we'll start by judging what type of object it is, and if it's a collection collection or a map collection, how do we iterate? Especially the map collection.

FIX: All collections or arrays are converted to the collection collection, and if it is a subclass object of the collection collection, then there is no need to make any changes, if it is a map collection, then call the EntrySet method of the map collection to get the Set collection object ( The set set is also a subclass of the collection collection, and if it is an array object, whether an array of object types or an array of basic data types, use the collection class array to iterate through each element and encapsulate it in a collection collection object, which is already reflected in the previous blog post.

Code:

1  PublicCollection setitems (Object items) {2     if(ItemsinstanceofCollection) {3         return(Collection) items;4     }5         6     if(ItemsinstanceofMap) {7Map map =(MAP) items;8         returnMap.entryset ();//Set9     }Ten          One     if(Items.getclass (). IsArray ()) { ACollection coll =NewArrayList (); -         intLength =array.getlength (items); -          for(inti=0;i<length;i++) { theObject value =array.get (items, i); - Coll.add (value); -         } -         returnColl; +     } -          +     return NULL; A}

Application 1:

1 New ArrayList (); 2 list.add ("AAA"); 3 list.add ("BBB"); 4 List.add ("CCC"); 5 Collection Collection = setitems (list); 6   System.out.println (collection);

Results: [AAA, BBB, CCC]

Application 2:

1 New HashMap (); 2 map.put ("AA", "AAAA"); 3 map.put ("BB", "bbbb"); 4 Collection Collection = setitems (map); 5 System.out.println (collection);

Results: [AA=AAAA, BB=BBBB]

Note Because in the Setitems method, the EntrySet method is called for the map collection, so we iterate over the set set.

Application 3:

1     int [] arr = {1,2,3,4,5}; 2     System.out.println (Setitems (arr));

Results: [1, 2, 3, 4, 5]

Application 4:

1     String[] arr = {"Long", "Live", "SD"}; 2     System.out.println (Setitems (arr));

Result: [Long, Live, SD]

How to iterate an object object if it could be a collection or an array

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.