Add multiple items to already initialized arraylist in java

JavaArraylistCollections

Java Problem Overview


I'm googling it and can't seem to find the syntax. My arraylist might be populated differently based on a user setting, so I've initialized it

ArrayList<Integer> arList = new ArrayList<Integer>();

And now I'd like to add hundred of integers without doing it one by one with arList.add(55);

Java Solutions


Solution 1 - Java

If you have another list that contains all the items you would like to add you can do arList.addAll(otherList). Alternatively, if you will always add the same elements to the list you could create a new list that is initialized to contain all your values and use the addAll() method, with something like

Integer[] otherList = new Integer[] {1, 2, 3, 4, 5};
arList.addAll(Arrays.asList(otherList));

or, if you don't want to create that unnecessary array:

arList.addAll(Arrays.asList(1, 2, 3, 4, 5));

Otherwise you will have to have some sort of loop that adds the values to the list individually.

Solution 2 - Java

What is the "source" of those integer? If it is something that you need to hard code in your source code, you may do

arList.addAll(Arrays.asList(1,1,2,3,5,8,13,21));

Solution 3 - Java

Collections.addAll is a varargs method which allows us to add any number of items to a collection in a single statement:

List<Integer> list = new ArrayList<>();
Collections.addAll(list, 1, 2, 3, 4, 5);

It can also be used to add array elements to a collection:

Integer[] arr = ...;
Collections.addAll(list, arr);

Solution 4 - Java

If you are looking to avoid multiple code lines to save space, maybe this syntax could be useful:

		java.util.ArrayList lisFieldNames = new ArrayList() {
			{
				add("value1"); 
				add("value2");
			}
		};

Removing new lines, you can show it compressed as:

		java.util.ArrayList lisFieldNames = new ArrayList() {
			{
				add("value1"); add("value2"); (...);
			}
		};

Solution 5 - Java

Java 9+ now allows this:

List<Integer> arList = List.of(1,2,3,4,5);

The list will be immutable though.

Solution 6 - Java

If you needed to add a lot of integers it'd proabbly be easiest to use a for loop. For example, adding 28 days to a daysInFebruary array.

ArrayList<Integer> daysInFebruary = new ArrayList<>();

for(int i = 1; i <= 28; i++) {
    daysInFebruary.add(i);
}

Solution 7 - Java

In kotlin ways;

val arList = ArrayList<String>()
arList.addAll(listOf(1,2,3,4,5))

Solution 8 - Java

I believe the answer above is incorrect, the proper way to initialize with multiple values would be this...

int[] otherList ={1,2,3,4,5};

so the full answer with the proper initialization would look like this

int[] otherList ={1,2,3,4,5};
arList.addAll(Arrays.asList(otherList));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbatoutofhellView Question on Stackoverflow
Solution 1 - JavascaevityView Answer on Stackoverflow
Solution 2 - JavaAdrian ShumView Answer on Stackoverflow
Solution 3 - JavaRadiodefView Answer on Stackoverflow
Solution 4 - JavaFran G AparicioView Answer on Stackoverflow
Solution 5 - Javauser197674View Answer on Stackoverflow
Solution 6 - Javarhino9View Answer on Stackoverflow
Solution 7 - JavaAbdulkerim YıldırımView Answer on Stackoverflow
Solution 8 - Javauser2821654View Answer on Stackoverflow