Home > Back-end >  How can I make Arrays.asList() range 1 to 1000000?
How can I make Arrays.asList() range 1 to 1000000?

Time:04-16

I want to make my Arrays.asList() range to 1-1000000. I'm not familiar with lambda expressions and stream API. How do I make it? I'm still new on JAVA.

public static void main(String[] args) {
    
        List<Integer> x = Arrays.asList(1,2,3,4.....1000000);

        System.out.println(x.stream().filter(e -> e % 2 == 0).reduce(0, Integer::sum));
    }

CodePudding user response:

You can achieve this with:

int[] array = IntStream.rangeClosed(1, 1000000).toArray();

...and then covert to List:

List<Integer> x = Arrays.asList(array);

CodePudding user response:

Simple oneliner

List<Integer> x = IntStream.rangeClosed(1, 1000000).boxed().collect(Collectors.toList());
  • Related