LinkedList method toArray() in Java
A LinkedList method toArray() is a Collection interface method that used to convert an array of LinkedList elements that contains all of the elements in this list in proper sequence. It returns an array containing all of the elements in this list in proper sequence.
Syntax
 1 public Object[] toArray()
return : It returns an array containing all of the elements in this list in proper sequence.
LinkedList method toArray()
 1 package iogyan.example;
 2 
 3 import java.util.Arrays;
 4 import java.util.LinkedList;
 5 import java.util.List;
 6 
 7 public class Main {
 8 
 9     public static void main(String[] args) {
 10 
 11         // create immutable list object
 12         List<Integer> elements = List.of(10, 20, 30, 40);
 13 
 14         // create LinkedList by specifying a list
 15         LinkedList<Integer> list = new LinkedList<>(elements);
 16         System.out.println("A LinkedList elements : " + list);
 17 
 18         // create array from list
 19         Object[] array = list.toArray();
 20         System.out.println("An array of list elements : ");
 21         System.out.println(Arrays.toString(array));
 22     }
 23 }
In the above example, a LinkedList of type integer created by passing a list collection. A toArray() method called that returns an array containing all of the elements in this list in same sequence. A resulting array converted to string using Arrays static method toString().
Output
 1 A LinkedList elements : [10, 20, 30, 40]
 2 An array of list elements : 
 3 [10, 20, 30, 40]

LinkedList method toArray() with String

LinkedList method toArray() with String
 1 package iogyan.example;
 2 
 3 import java.util.Arrays;
 4 import java.util.LinkedList;
 5 import java.util.List;
 6 
 7 public class StringMain {
 8 
 9     public static void main(String[] args) {
 10 
 11         // create immutable list object
 12         List<String> elements = List.of("Welcome", "to", "IOGyan");
 13 
 14         // create LinkedList by specifying a list
 15         LinkedList<String> list = new LinkedList<>(elements);
 16         System.out.println("A LinkedList elements : " + list);
 17 
 18         // create array from list
 19         Object[] array = list.toArray();
 20         System.out.println("An array of list elements : ");
 21         System.out.println(Arrays.toString(array));
 22     }
 23 }
In the above example, a LinkedList of type String created by passing a list collection. A toArray() method called that returns an array containing all of the elements in this list in same sequence. A resulting array converted to string using Arrays static method toString().
Output
 1 A LinkedList elements : [Welcome, to, IOGyan]
 2 An array of list elements : 
 3 [Welcome, to, IOGyan]

LinkedList method toArray() with custom object

LinkedList method toArray() with custom object
 1 package iogyan.example;
 2 
 3 import iogyan.model.Employee;
 4 
 5 import java.util.*;
 6 
 7 public class EmployeeMain {
 8 
 9     public static void main(String[] args) {
 10 
 11         Employee emp1 = new Employee(1, "Emp1","Tech", 1000);
 12         Employee emp2 = new Employee(2, "Emp2","Admin", 5000);
 13         Employee emp3 = new Employee(3, "Emp3","Admin", 12000);
 14 
 15         // create immutable list object
 16         List<Employee> elements = List.of(emp1, emp2, emp3);
 17 
 18         // create LinkedList by specifying a list
 19         LinkedList<Employee> list = new LinkedList<>(elements);
 20         System.out.println("A LinkedList elements : ");
 21         System.out.println(list);
 22 
 23         // create array from list
 24         Object[] array = list.toArray();
 25         System.out.println("An array of list elements : ");
 26         Arrays.stream(array).forEach(System.out::println);
 27     }
 28 }
In the above example, a LinkedList of type String created by passing a list collection. A toArray() method called that returns an array containing all of the elements in this list in same sequence. A resulting array converted to Stream using Arrays static method stream() and print.
Output
 1 A LinkedList elements : 
 2 [Emp1 Tech 1000.0, Emp2 Admin 5000.0, Emp3 Admin 12000.0]
 3 An array of list elements : 
 4 Emp1 Tech 1000.0
 5 Emp2 Admin 5000.0
 6 Emp3 Admin 12000.0

LinkedList method toArray() with array parameter

A LinkedList method toArray() with array parameter used to copy a list elements into the specified array. If a list elements less than array length, a remaining element will be null and if a list element less than array, it creates new array of length list size and copy elements.
Syntax
 1 public <T> T[] toArray(T[] a)
a : It is an array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
return : It returns an array containing an elements of this list.

LinkedList method toArray() with array parameter

LinkedList method toArray() with array parameter
 1 package iogyan.example;
 2 
 3 import java.util.Arrays;
 4 import java.util.LinkedList;
 5 import java.util.List;
 6 
 7 public class Main {
 8 
 9     public static void main(String[] args) {
 10 
 11         // create immutable list object
 12         List<Integer> elements = List.of(10, 20, 30, 40);
 13 
 14         // create LinkedList by specifying a list
 15         LinkedList<Integer> list = new LinkedList<>(elements);
 16         System.out.println("A LinkedList elements : " + list);
 17 
 18         // create an array with less element
 19         Integer[] arr = new Integer[4];
 20         Integer[] res = list.toArray(arr);
 21         System.out.println("An array of list elements : ");
 22         System.out.println(Arrays.toString(res));
 23     }
 24 }
In the above example, an array of integer is created with length 4, a list method toArray() called by passing an array object and copy each element into the specified array and returns the same array.
Output
 1 A LinkedList elements : [10, 20, 30, 40]
 2 An array of list elements : 
 3 [10, 20, 30, 40]

LinkedList method toArray() with array as less element

LinkedList method toArray() with array as less element
 1 package iogyan.example;
 2 
 3 import java.util.Arrays;
 4 import java.util.LinkedList;
 5 import java.util.List;
 6 
 7 public class Main {
 8 
 9     public static void main(String[] args) {
 10 
 11         // create immutable list object
 12         List<Integer> elements = List.of(10, 20, 30, 40);
 13 
 14         // create LinkedList by specifying a list
 15         LinkedList<Integer> list = new LinkedList<>(elements);
 16         System.out.println("A LinkedList elements : " + list);
 17 
 18         // create an array with less element
 19         Integer[] arr = new Integer[2];
 20         Integer[] res = list.toArray(arr);
 21         System.out.println("An array of list elements : ");
 22         System.out.println(Arrays.toString(res));
 23     }
 24 }
In the above example, an array of integer is created with length 2, a list method toArray() called by passing an array object that creates new array and copy each element into the newly created array and returns the same array.
Output
 1 A LinkedList elements : [10, 20, 30, 40]
 2 An array of list elements : 
 3 [10, 20, 30, 40]

Employee class

Employee class
 1 package iogyan.model;
 2 
 3 public class Employee {
 4     private final int id;
 5     private final String name;
 6     private final String dept;
 7     private final double salary;
 8 
 9     public Employee(int id, String name, String dept, double salary) {
 10         this.id = id;
 11         this.name = name;
 12         this.dept = dept;
 13         this.salary = salary;
 14     }
 15 
 16     public int getId() {
 17         return this.id;
 18     }
 19 
 20     public String getName() {
 21         return this.name;
 22     }
 23 
 24     public String getDept() {
 25         return this.dept;
 26     }
 27 
 28     public double getSalary() {
 29         return this.salary;
 30     }
 31 
 32     @Override
 33     public int hashCode() {
 34         return super.hashCode();
 35     }
 36 
 37     @Override
 38     public boolean equals(Object obj) {
 39         return this.id == ((Employee)obj).id;
 40     }
 41 
 42     @Override
 43     public String toString() {
 44         return this.getName() + " " + this.getDept() + " " + this.getSalary();
 45     }
 46 }
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us