List,set in Java, the conversion between arrays is detailed _java

Source: Internet
Author: User

Java List,set, the conversion between arrays, mainly using the 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 uses 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 Arrays.aslist () method returns a list that cannot add an object because the implementation of the method is a arraylist of the size of the array that uses the parameter reference.

★collection Array
Direct use of the collection ToArray () method, which 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 are discarded
Set set = New HashSet (arrays.aslist (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.