List,set in Java, an explanation of conversions between arrays

Source: Internet
Author: User
Tags addall set set

Java List,set, the conversion between arrays, mainly using Apache Jakarta Commons collections, the specific method is as follows:
Import Org.apache.commons.collections.CollectionUtils;

String[] Strarray = {"AAA", "BBB", "CCC"};
List strlist = new ArrayList ();
Set strset = new HashSet ();
Collectionutils.addall (Strlist, Strarray);
Collectionutils.addall (Strset, Strarray);
The implementation of the Collectionutils.addall () method is simple, but it only loops through the collection Add () method.

If you just want to convert the array to list, you can use the Java.util.Arrays class in the JDK:

Import Java.util.Arrays;

String[] Strarray = {"AAA", "BBB", "CCC"};
List strlist = arrays.aslist (Strarray);
However, the list returned by the Arrays.aslist () method cannot be an Add object, because the implementation of the method is a arraylist of the new using the size of the array referenced by the parameter.

★collection to Array
Using the collection ToArray () method directly, the method has two overloaded versions:

Object[] ToArray ();
T[] ToArray (t[] a);

★map Turn Collection
Use the values () method of the map directly.

★list and set conversions
List List = new ArrayList (new Hashset ());//Fixed-size list
List List = arrays.aslist (array);//Growable
List List = new LinkedList (arrays.aslist (array));//Duplicate elements is discarded
Set set = New HashSet (arrays.aslist (array));

List,set in Java, an explanation of conversions between arrays

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.