In Java programming, arrays and lists are two very commonly used data structures. Arrays are a fixed-size data structure that can efficiently store data of the same type. The list is a more flexible structure that can be dynamically resized, and is more suitable for use when frequent data modification is required. Converting arrays to lists is a common task, here we will introduce two simple implementations.
1. Using Java Collections Framework()
Java provides a very convenient tool class, among them
asList
Methods can very simply convert arrays into lists. The advantage of this method is that it is simple and easy to understand. Next, let's see how to use it.
1.1 Sample Code
The following is a useSample code for method converting arrays into lists:
import ; import ; public class ArrayToListExample { public static void main(String[] args) { // Create an array String[] array = {"apple", "banana", "cherry", "date"}; //Usage method to convert array to list List<String> list = (array); // Output list content ("Converted List: " + list); } }
1.2 Output result
Run the above code and the output is:
Converted list: [apple, banana, cherry, date]
1.3 Description
In this example, we first define an array of strings that contain the names of some fruits. Then, we useMethods quickly convert arrays into lists. Note that the returned list is a fixed-size list, which will cause
UnsupportedOperationException
abnormal.
1.4 Things to note
-
Type safety:
The returned list is a fixed-size list with the same element type as the input array.
-
Adding or deleting elements is not supported: Since the returned list is based on an array, elements cannot be added or deleted. If you need a variable size list, you can use it further
ArrayList
。
1.5 Extended usage
If you need a more flexible list, you canThe returned list is passed to
ArrayList
Constructor to create a variable size list. Examples are as follows:
import ; import ; import ; public class ArrayToListExample2 { public static void main(String[] args) { // Create an array String[] array = {"apple", "banana", "cherry", "date"}; // Convert array to variable size ArrayList List<String> list = new ArrayList<>((array)); // Add new elements ("elderberry"); ("banana"); // Output the modified list content ("Modified List: " + list); } }
1.6 Output result
The output of running the above code will be:
Modified list: [apple, cherry, date, elderberry]
2. Using Java 8 Stream
Starting with Java 8, Java introduced a Stream API that allows the processing of collection data in a declarative manner. With streams, we can easily convert arrays into lists. Here is an example of how to convert an array to a list using the Stream API:
2.1 Sample Code
import ; import ; import ; public class ArrayToListStreamExample { public static void main(String[] args) { // Create an array String[] array = {"apple", "banana", "cherry", "date"}; // Convert arrays to lists using Stream API List<String> list = (array).collect(()); // Output list content ("Converted List: " + list); } }
2.2 Output result
Run the above code and the output is:
Converted list: [apple, banana, cherry, date]
2.3 Description
In this example, we useMethod creates a stream and then use
.collect(())
Collect elements from the stream into a list. This method andSimilar, but the streaming API is more flexible when handling complex operations.
2.4 Things to note
- flexibility: The method of using streams is more flexible, and other operations can be performed while converting arrays into lists, such as filtering, mapping, etc.
-
performance: For small-scale data, streaming API performance and
The difference is not obvious, but for cases where the data volume is large, it may be slightly disadvantageous due to the additional operating costs.
2.5 Sample extension
We can further use the stream method to process the data while converting, such as converting all strings to uppercase:
import ; import ; import ; public class ArrayToListStreamExample2 { public static void main(String[] args) { // Create an array String[] array = {"apple", "banana", "cherry", "date"}; // Use Stream API to convert arrays to lists and to uppercase List<String> list = (array) .map(String::toUpperCase) .collect(()); // Output the converted list content ("Converted capital list: " + list); } }
2.6 Output result
Run the above code and the output is:
Converted capitalization list: [APPLE, BANANA, CHERRY, DATE]
in conclusion
In Java, there are many ways to convert arrays to lists, but useand Java 8 streaming API are the two most commonly used implementation methods. The former is simple and clear in implementation, while the latter is more flexible and suitable for use in data processing chains. The most appropriate way to choose the basic skills that developers need to master.
Understanding and mastering these technologies is very important to improve Java programming capabilities, especially in handling sets and inter-set conversions. In large projects, choosing the right data structure and transformation method can significantly improve the execution efficiency and readability of the code. Through the introduction of this article, I believe that everyone has a deeper understanding and mastery of the conversion between Java arrays and lists. Hope these contents will be helpful to you in actual programming!
This is the end of this article about two simple implementation methods of converting arrays into lists in Java. For more related contents of Java array conversion lists, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!