Convert list to LinkedList java
You can convert a LinkedList to an array in Java by using the toArray() method of the java.util.LinkedList class. The toArray() method accepts an array of relevant type to store contents of LinkedList. It stores the elements in the array in the same order they are currently inside the LinkedList. By using the toArray() method you can convert any type of LinkedList e.g. Integer, String or Float to any type of Array, only catch is this you cannot convert a LinkedList to an array of primitives i.e. a LinkedList of Integer cannot be converted into an array of ints by using toArray() method, but you can convert it to an array of Integer objects, that's perfectly Ok. Btw, the Java Collection framework is vast as it contains so many classes for different purposes. The best way to master the Collection framework is to pick up a good book and follow it from start to end like Java Generics and Collection, which provides comprehensive coverage of all important classes of Java Collection framework like ArrayList, Vector, HashMap, HashSet, etc. Show Alternatively, you can also follow a good core Java course like The Complete Java MasterClass, which also coverers changes made in Java 8 like lambda expression and streams, which has completely changed how you use Collection classes in Java. The bottom line is that a good knowledge of the Java Collection framework is essential for any Java programmer. In fact, these classes are the bread and butter of Java programming and you will often find them using in your day-to-day programming tasks. Since the toArray() method is used to convert LinkedList to an array in Java, it's important to learn more about it. In fact, you can use this method to convert any type of list to an array in Java as I have previously discussed while we are converting ArrayList to an array in Java. Let's revise some of the important points about this useful method:1) This method returns an array containing all of the elements in the given linked list in the same sequence i.e. it keeps the order intact. This is possible because LinkedList implements java.util.List interface which is an ordered collection and guarantees insertion order of elements. 2) The toArray() method expects the caller to provide an array of a specified type, but it's also smart enough to make some adjustments into length.3) If the given array is not big enough to store all elements of the LinkedList a new array is created of the same runtime type and size of the LinkedList. 4) If the given array is bigger than the linked list then spare buckets are set to null. You can use them to determine the true length of the array if you know that list cannot contain null elements.If you are interested in learning more about this method or in general, the Java Collection framework, I strongly suggest you join the Java Fundamentals: Collections course on Pluralsight. One of the most comprehensive courses on this topic.
As I have said, you cannot convert LinkedList of wrapper objects to an array of primitive objects e.g. LinkedList of Double cannot be converted to an array of double primitives, for that you need to loop through the list and manually insert each element into an array to take advantage of autoboxing. Btw, after Java 8, you can also use the map() method to convert a list of Integer objects into an array of Integer objects. Btw, If you are yet to start Java 8 then I suggest you take a look a these free Java 8 courses to start with. It's very important for a Java developer to get familiar with Java 8 changes. Anyway, here is our sample program to convert a LinkedList to an array of the same type in Java: import java.util.Arrays; import java.util.LinkedList; public class LinkedListToArray { public static void main(String args[]){ // creating and initializing a LinkedList of String LinkedList<String> listOfBooks = new LinkedList<>(); listOfBooks.add("Effective Java"); listOfBooks.add("Clean Code"); listOfBooks.add("Clean Coder"); // printing the contents of LinkedList before conversion System.out.println("LinkedList: " + listOfBooks); // Converting the LinkedList to array String[] arrayOfBooks = listOfBooks.toArray(new String[listOfBooks.size()]); // printing contents of array after conversion System.out.println("String array: " + Arrays.toString(arrayOfBooks)); // Second example - Creating LinkedList of Integers LinkedListThat's all about how to convert a LinkedList to the array in Java. Just remember that you can use the toArray() method for this conversion. It also maintains the order of elements and can create a new array if a given array is not big enough to store all elements. Related LinkedList and Java Collection tutorials, articles
Before you learn about the example, make sure you first visit the following tutorials,
Example 1: Convert the LinkedList into Arrayimport java.util.LinkedList; class Main { public static void main(String[] args) { LinkedListOutput In the above example, we have created a linked list named languages. Notice the line, languages.toArray(arr);Here, the toArray() method converts the linked list languages into an array. And stores it in the string array arr. Note: If we don't pass any argument to the toArray() method, the method returns an array of the Object type. Example 2: Convert Array to LinkedListimport java.util.Arrays; import java.util.LinkedList; class Main { public static void main(String[] args) { // create an array String[] array = {"Java", "Python", "C"}; System.out.println("Array: " + Arrays.toString(array)); // convert array to a linked list LinkedListOutput Array: [Java, Python, C] LinkedList: [Java, Python, C]In the above example, we have created an array of String type. Notice the expression, Arrays.asList(array)Here, the asList() method of the Arrays class converts the specified array into the linked list. |