Java array conversion to List
I never imagined that there was so much knowledge about "converting Java arrays to lists", and it was a little cool. The conversion of "Java array to List" is actually a very basic knowledge, and many people have discussed it in their blogs. It is inevitable that it is unnecessary to write such a discussion article! As I thought of "not accumulating silicon, but not thousands of miles", I forced myself to do some in-depth practices on this issue again.
1. Create ArrayList from array
I was surprised to see how enthusiastic the discussion about Create ArrayList from array in stackoverflow was:
New ArrayList (Arrays. asList (array) List list = Arrays. asList (array); Use Guava -- Lists. newArrayList (aStringArray); Collections. addAll (arraylist, array); for (Element e: array) -- list. add (e );
The above five solutions can be extracted from it. For Guava, I do not know much about this, so I will let it go for the time being. We will detail the remaining four cases for practice.
①. New ArrayList (Arrays. asList (array ))
In fact, I think the first solution is simple and easy to use.
String[] array1 = { new String("1"), new String("2"), new String("3") };arrayList = new ArrayList
(Arrays.asList(array1));for (String s : arrayList) { System.out.print(s + "、");}try { arrayList.add("111"); for (String s : arrayList) { System.out.print(s + "、"); }} catch (Exception e) { System.out.print(e);}try { array[1] = "qw"; for (String s : arrayList) { System.out.print(s + "、"); }} catch (Exception e) { System.out.print(e);}System.out.println();System.out.println("----------new ArrayList
(Arrays.asList(array1))---------------");
The output content is as follows:
1, 2, 3, 1, 2, 3, 111, 1, 2, 3, 111,
---- New ArrayList (Arrays. asList (array1 ))-----
However, after reading the discussion on stackoverflow, I think this solution has the biggest drawback!
You may create and fill two lists!
Well, this process will generate two lists, which are really broken! But please note that there is a "new ArrayList" between this method and the second method, so why should we try again? Please refer to the introduction of the second method!
② List list = Arrays. asList (array)
From the java API, we can see that
AsList
Public static List asList (T... A) returns a list of fixed sizes supported by the specified array. (Changes to the returned list are directly written to the array .) Together with Collection. toArray (), this method serves as a bridge between array-based APIs and collection-based APIs. The returned list is serializable and implements RandomAccess.
This method also provides a convenient way to create a fixed-length list, which is initialized to contain multiple elements:
List
stooges = Arrays.asList("Larry", "Moe", "Curly");
Parameters:
A-list arrays are supported.
Return Value:
List View of the specified array.
This method has two drawbacks:
The list length is fixed, that is, the add element cannot be performed. Changes to the returned list will be written directly to the array.
Let's look at the example:
String[] array = { new String("1"), new String("2"), new String("3") };List
arrayList = Arrays.asList(array);for (String s : arrayList) { System.out.print(s + "、");}try { arrayList.add("111"); for (String s : arrayList) { System.out.print(s + "、"); }} catch (Exception e) { System.out.print(e);}try { arrayList.set(1, "qw"); for (String s : array) { System.out.print(s + "、"); }} catch (Exception e) { System.out.print(e);}System.out.println();System.out.println("----------Arrays.asList---------------");
The output result is as follows:
1, 2, 3, java. lang. UnsupportedOperationException1, qw, 3,
---- Arrays. asList -----
The results show that:
This will work fine. But some caveats:
The list returned from asList has fixed size. so, if you want to be able to add or remove elements from the returned list in your code, you'll need to wrap it in a new ArrayList. otherwise you'll get an UnsupportedOperationException. the list returned from asList () is backed by the original array. if you modify the original array, the list will be modified as well. this may be surprising.
Therefore, there are many restrictions on the "array conversion to List" method, which limits the use of the converted List!
Then return to the first method "new ArrayList (Arrays. in asList (array), the use of new ArrayList obviously avoids the above two restrictions, but creates two lists at the same time, so although the above two methods are concise enough in terms of the amount of code, but there are also many drawbacks, that is to say, these two methods are somewhat "sequelae ".
④ Collections. addAll (arraylist, array );
Skip Guava. Let's take a look at "Collections. addAll (arraylist, array )".
String[] array2 = { new String("1"), new String("2"), new String("3") };arrayList = new ArrayList
();Collections.addAll(arrayList, array2);for (String s : arrayList) { System.out.print(s + "、");}try { arrayList.add("111"); for (String s : arrayList) { System.out.print(s + "、"); }} catch (Exception e) { System.out.print(e);}try { array[1] = "qw"; for (String s : arrayList) { System.out.print(s + "、"); }} catch (Exception e) { System.out.print(e);}System.out.println();System.out.println("-------------Collections.addAll(arrayList, array2)------------");
For the fourth method, I think it is good, the code is concise enough, and there is no additional "sequelae", so we recommend this practice!
In addition, you can see the following explanation through the api:
Public static boolean addAll (Collection <? Super T> c, T... Elements)
Add all specified elements to the specified collection. You can specify the elements to be added or an array. The behavior of this convenient method is the same as that of c. addAll (Arrays. asList (elements), but in most implementations, this method may run much faster.
When you specify elements separately, This method provides a convenient way to add a few elements to an existing collection:
Collections. addAll (flavors, "Peaches 'n' Plutonium", "Rocky Racoon ");
This method corresponds to another method.
String[] array3 = { new String("1"), new String("2"), new String("3") };ArrayList
arrayList3 = new ArrayList
();arrayList3.addAll(Arrays.asList(array3));
This method is not as good as Collections. addAll!
⑤ For (Element e: array) -- list. add (e );
In fact, we often ignore the most primitive writing method, that is, the so-called
arrayList3 = new ArrayList
();for (String s : array3) { arrayList3.add(s);}
In my opinion, this writing method is concise enough, with no sequelae and running speed. I think it is definitely not slow!
After summing up the above "converting Java array to List" method, I really feel that the original basic knowledge is also so deep. Please learn it now!