3

In my application contains one Arraylist which i am adding value for every minute.

Initially I want to add one value for thousands time for that arraylist for that my code is

int count = 30000; Arraylist mylist = new Arraylist();for(int i=0;i<count;i++)
{
    mylist.add("4.5");
}

In the above code it is taking large time to complete the for loop and my app is getting slow . Is there any alternate way to add the value to array list fastly with one click?

Akshay
  • 1,161
  • 1
  • 12
  • 33

1 Answers1

6

You can use Collections's static <T> List<T> nCopies(int n, T o):

List<String> list = Collections.nCopies("4.5",count);

However, that would create an immutable List<String>. In order to obtain a mutable List, you'll have to pass that list to ArrayList's constructor, so I'm not sure the end result will be much faster than your current loop.

List<String> mylist = new Arraylist<>(Collections.nCopies("4.5",count));
Eran
  • 387,369
  • 54
  • 702
  • 768