x Java Java 8 JUnit JSON
  • XML
  • JDBC Spring Boot Microservices React Contact Us

    Convert Array to List and vice versa in Java            

    There are various ways of converting Array to List such using Arrays asList(), Collections addAll() etc. This tutorial explains How to convert an Array to List.

    How to convert Array to List

    1) Convert array to list in Java using Arrays asList()

    // Converts array to list in Java using Arrays asList()    
    import java.util.Arrays;
    import java.util.List;
    
    public class MyClass {
        public static void main(String[] args) {
            Integer[] values = { 1, 6, 5 };
            
            // convert array of Integer to List
            List<Integer> list = Arrays.asList(values);
            System.out.println(list);
           
            String[] names = { "Harry", "Ronny", "Jack" };
            
            // convert array of String to List
            List<String> nameList = Arrays.asList(names);
            System.out.println(nameList);        
        }
    }
        

    Output :

    [1, 6, 5]
    [Harry, Ronny, Jack]
    		

    2) Convert array to list in Java without using Arrays asList()

    You can convert array to list by iterating over array.

    // Converts array to list in Java by iterating over array       
    import java.util.ArrayList;
    import java.util.List;
    
    public class MyClass {
        public static void main(String[] args) {
            Integer[] nums = { 1, 6, 5 };
            
            List<Integer> list = new ArrayList<>();
          
            for (Integer num : nums) {
                list.add(num);
            }            
            
            System.out.println(list);
        }
    }
        

    Output :

    [1, 6, 5]
    	

    3) Convert array to list in Java using Collections addAll()

    //Converts array to list using Collections addAll()    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class MyClass {
        public static void main(String[] args) {
            Integer[] nums = { 1, 6, 5 };
            
            List<Integer> list = new ArrayList<>();
            list.add(2);
            Collections.addAll(list, nums); 
            System.out.println(list);
            
            Collections.addAll(list, 9, 10); 
            System.out.println(list);
        }
        
    }
        

    Output :

    [2, 1, 6, 5]
    [2, 1, 6, 5, 9, 10]
    	

    4) Convert array to list in Java using Collectors toList()

    //Converts array to list using Collectors toList()        
    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;
    
    public class MyClass {
        public static void main(String[] args) {
            Integer[] nums = { 1, 6, 5 };
            
            // convert array of Integer to List
            List<Integer> list = Arrays.stream(nums).collect(Collectors.toList());  
            System.out.println(list);
           
            String[] names = { "Harry", "Ronny", "Jack" };
            
            // convert array of String to List
            List<String> nameList = Arrays.stream(names).collect(Collectors.toList());
            System.out.println(nameList);        
        }
    }
        

    Output :

    [1, 6, 5]
    [Harry, Ronny, Jack]
    		

    How to convert a List to Array ?

    1) Convert list to array in Java using List toArray()

    //Convert list to array in Java using List toArray()    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Arrays;
    
    public class MyClass {
        public static void main(String[] args) {
            List<String> list = new ArrayList<String>();
            list.add("Apple");
            list.add("Mango");
            list.add("Grapes");
            String[] fruits = list.toArray(new String[list.size()]);
            System.out.println(Arrays.toString(fruits));
        }
    }
        

    Output :

    [Apple, Mango, Grapes]
    		

    2) Convert list to array in Java without using List toArray()

    You can convert list to array by iterating List.

    //Convert list to array in Java by iterating List    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Arrays;
    
    public class MyClass {
        public static void main(String[] args) {
            List<String> list = new ArrayList<String>();
            list.add("Charlie");
            list.add("Allen");
            list.add("Walden");
            String[] students = list.toArray(new String[list.size()]);
            int i=0;
            
            for (String student : list) {
                students[i++] = student;
            }
            
            System.out.println(Arrays.toString(students));
        }
    }
        

    Output :

    [Charlie, Allen, Walden]
    		

    3) Convert list to array in Java using Stream toArray()

    //Convert list to array in Java using Stream toArray()    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Arrays;
    
    public class MyClass {
        public static void main(String[] args) {
            List<String> list = new ArrayList<String>();
            list.add("Apple");
            list.add("Mango");
            list.add("Grapes");
            
            String[] fruits = list.stream().toArray((String[]::new));
            System.out.println(Arrays.toString(fruits));
        }
    }
        

    Output :

    [Apple, Mango, Grapes]
    		

    References :

    Oracle Docs Arrays asList()

    Oracle Docs Arrays addAll()

    Oracle Docs Collectors toList()

    Oracle Docs List toArray()

    Oracle Docs Stream toArray()

    Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *











    Share This