How to convert List<Integer> to int[] in Java

2 Answers

0 votes
import java.util.List;
import java.util.Arrays; 
 
public class MyClass 
{
    public static void main(String[] args) {
 
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);

        int[] array = list.stream().mapToInt(i -> i).toArray();
        
        System.out.println(Arrays.toString(array));
    }
}
  

  
   
/*
run:
   
[1, 2, 3, 4, 5, 6]
   
*/

 



answered Nov 11, 2023 by avibootz
0 votes
import java.util.List;
import java.util.Arrays; 
 
public class MyClass 
{
    public static void main(String[] args) {
 
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);

        int[] array = list.stream().mapToInt(Integer::intValue).toArray();
        
        System.out.println(Arrays.toString(array));
    }
}
  

  
   
/*
run:
   
[1, 2, 3, 4, 5, 6]
   
*/

 



answered Nov 11, 2023 by avibootz

Related questions

1 answer 135 views
2 answers 214 views
214 views asked Jan 2, 2022 by avibootz
1 answer 165 views
1 answer 158 views
1 answer 200 views
1 answer 910 views
1 answer 96 views
...