-2
public static void main(String[] args) {

    List<Long> list = new ArrayList<>();

    for (long i = 1; i < 100000000; i++) {
        list.add(i);
    }



}

this loop taking too long to insert values into the list is there any features in JAVA 8, using that the same task will perform in a minimal time.

Oliver Gondža
  • 3,386
  • 4
  • 29
  • 49
user7439667
  • 144
  • 8
  • 1
    the bigger question would be what do you want to do with that `List` of (100000000 -1) elements? Just iterating on them is feasible using `LongStream.iterate` as well. – Naman Apr 05 '20 at 07:02
  • That is really a big number, try multi threading and split the loop in two or more threads :) but it won't be in order – Dickens A S Apr 05 '20 at 07:05
  • Please use the linkedlist https://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist-in-java – Sri Apr 05 '20 at 07:06
  • This can be improved significantly by using `long[] list = new long[100000000];`, declaring `i` as `int` in the loop, and `list[i] = i;` (unless you need a list, which is unlikely) – ernest_k Apr 05 '20 at 07:14
  • @DickensAS Yes I did it with multithreading using join. But looking to do the same using Java 8. Thanks for your comment – user7439667 Apr 05 '20 at 07:14
  • Why do you want a list of the numbers from 0 to 99999999? What is the use-case? – kaya3 Apr 05 '20 at 07:15
  • Please look into Multithreading. – Zain Ul Abideen Apr 05 '20 at 07:15
  • @Naman Thanks for suggesting LongStream.iterate. Running code using this. Lets lee how much time it takes. If anything else you want to suggest will be appreciated.:) – user7439667 Apr 05 '20 at 07:18
  • @kaya3 running this code to check if any features in JAVA 8 parallel stream to perform this instead of multithreading. – user7439667 Apr 05 '20 at 07:21
  • The way to build a list using streams will depend on what you want to put in the list. The main benefit of building a list in parallel will be if computing the items themselves takes the bulk of the time. In your example, the bottleneck is just the time it takes to add the item to the list. – kaya3 Apr 05 '20 at 07:29
  • Does this answer your question? [What's the fastest way to initialize a large list of integers?](https://stackoverflow.com/questions/24845767/whats-the-fastest-way-to-initialize-a-large-list-of-integers) – Ole V.V. Apr 05 '20 at 08:03
  • Are you aware that you are creating a list of hundred millions of items? And that each item will be an wrapper object? – Jean-Baptiste Yunès Apr 05 '20 at 08:49

1 Answers1

0

If it’s not mandatory that the list be an ArrayList:

    Long[] array = new Long[100_000_000];
    Arrays.parallelSetAll(array, Long::valueOf);
    List<Long> list = Arrays.asList(array);

I have not done any time measurements. The list will be modifiable in the sense that you can set values, but it has fixed size, there is no adding or removing.

If you do need a variable-size list, you may try:

    List<Long> list = new ArrayList(Arrays.asList(array));

It somewhat defies the idea of exploiting parallel execution, though, and I am no longer sure about how the speed would compare to what you have already got.

Edit: If the list doesn’t need to be modifiable at all, make your own “list” implementation that doesn’t store any actual data and calculates the value each time an element is asked for. Then initialization will take no time. You can subclass AbstractList for this purpose.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161