Java Array to ListIn Java programming, arrays and lists are basic data structures, typically used to store collections of elements. While arrays provide fixed-size storage, lists provide dynamic resizing and other functionality. There are times when we may need to convert arrays to a list for proper conversion or to be compatible with libraries that require a list. In this section, we explore various methods for converting Java arrays to lists, including examples and ideas for each method. Converting Array to List in JavaJava array is a collection of multiple values of the same data type. An array can contain objects and primitive types. It depends on the definition of the array. If an array definition is of primitive type, the values of the array store in the contagious memory location. If an array contains objects elements, elements stored in the heap segment. In Java, a List is an interface that belongs to the Java Collections framework. It stores elements in the form of objects in an ordered manner and preserves the insertion order. It allows us to store duplicate values. The classes ArrayList, LinkedList, Vector and Stack implements the List interface. Java provides five methods to convert Array into a List are as follows:
Native MethodIt is the simplest method to convert Java Array into a List. In this method first, we create an empty List and add all the elements of the array into the List. Let's see an example. ArrayToListExample1.java Output: Using Arrays.asList() MethodIt is the method of the Java Arrays class that belongs to java.util package. When we use the asList() method with the Collection.toArray() method, it works as a bridge between array-based and collection-based APIs. Syntax: The method parses an array as a parameter by which the list will be backed. It returns a serializable fixed-size list view of the specified array. Let's see an example. ArrayToListExample2.java Output: Using Collections.addAll() MethodIt is the method of the Java Collections class. it belongs to java.util package. The class provides a method named addAll(). We can use the method to convert Array into a List. It adds all the elements to the specified collection. We can specify elements either individually or in the form of an array. It works the same as c.addAll(Arrays.asList(elements)). It is a faster implementation than another implementation. Syntax: It parses two parameters:
It returns true if the collection changed as a result of the call. It throws the following exceptions:
Let's see an example. ArrayToListExample3.java Output: Using Java 8 Stream APIJava 8 provides the Stream API to process the collections of objects. It is a sequence of methods that can be pipelined to produce the desired result. Remember that it does not change the original data structure. It provides the output based on the pipelined methods. We can attain Stream in a number of ways but in the following program, we have used Arrays.stream(Object[]) to attain the stream. Collectors.toList() Method: The method returns a Collector that collects the input elements into a newly created List in an encounter method. Syntax: Where T is the type of element that we have specified. The method does not provide guarantees on type, mutability, thread-safety, and serializability. Let's use the Stream API in a Java program and convert an array into a List. ArrayToListExample4.java Output: Using Guava Lists.newArrayList()It is the method of the Lists class that belong to com.google.common.collect package. The class provides a method newArrayList() that creates mutable empty ArrayList instance having the elements of the specified array. Syntax: Note: The newArrayList() method is available for Java 6 and earlier versions. In later versions it is deprecated. Instead of the above method, we use ArrayList constructor directly.ArrayToListExample5.java Output: ConsiderationsWhen converting arrays to lists, it's essential to consider the following points: Mutability: Arrays.asList() returns a fixed-size list, while the other methods create mutable lists. Choose the appropriate method based on whether we need a mutable or immutable list. Performance: Arrays.asList() provides a view of the original array, so it's efficient in terms of memory usage. However, if we need a mutable list, consider using ArrayList or streams. Null Elements: Be cautious when dealing with arrays that contain null elements, as some methods might throw NullPointerException. Primitive Arrays: For primitive arrays (like int[], double[]), we cannot directly use Arrays.asList(). In such cases, we will need to use wrapper classes or manual conversion. ConclusionModifying lists of Java arrays is a common programming task, and Java provides many ways to do this efficiently. Whether you need a fixed-size or variable list, there is a convenient option. Understanding the differences between these methods will help you choose the most appropriate method for your application, taking into account your specific needs. Next TopicJIT in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India