Set, list, and array (set and array) Conversion in Java

Source: Internet
Author: User
 Public static object [] list2array (list <Object> olist ){
Object [] oarray = olist. toarray (new object [] {});
// When todo is used, another write method is required. The generic array is not supported.
Return oarray;
}

Public static object [] set2array (set <Object> oset ){
Object [] oarray = oset. toarray (new object [] {});
// When todo is used, another write method is required. The generic array is not supported.
Return oarray;
}

Public static <t extends Object> List <t> set2list (set <t> oset ){
List <t> tlist = new arraylist <t> (oset );
// When todo is used, another write structure is required to generate the corresponding subclass of list as needed.
Return tlist;
}

Public static <t extends Object> List <t> array2list (T [] tarray ){
List <t> tlist = arrays. aslist (tarray );
// Some operations that affect the number of sets, such as add (), remove (), clear (), and so on, are not returned by the pure aslist () returned by todo,
// Because arrays $ arraylist is the same as Java. util. arraylist, they all inherit abstractlist,
// But arrays $ arraylist does not have the override methods, while Java. util. arraylist implements.
// Todo we recommend that you use the subclass of list for return, instead of arrays $ arraylist. As needed. Note the following line:
// List <t> tlist = new arraylist <t> (arrays. aslist (tarray ));
Return tlist;
}

Public static <t extends Object> set <t> list2set (list <t> tlist ){
Set <t> tset = new hashset <t> (tlist );
// The specific implementation of todo is converted into sub-classes of different sets.
Return tset;
}

Public static <t extends Object> set <t> array2set (T [] tarray ){
Set <t> tset = new hashset <t> (arrays. aslist (tarray ));
// Todo does not have a one-step method. Based on the specific function, select the appropriate set subclass for conversion.
Return tset;
}

 

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.