One line initialization of an ArrayList object in Java

0 votes

It is very tedious and lengthy to create an ArrayList and add elements to it. 

for example:

ArrayList<String> student = new ArrayList<String>();
places.add("john");
places.add("james");
places.add("jill");

Is there a better way to do it?

Apr 13, 2018 in Java by Daisy
• 8,140 points
4,746 views

2 answers to this question.

0 votes

Java 9 has some new Factory methods for collections:

For List object - List.of() method 
List<String> places=new ArrayList<>(List.of( "john","james","jill");

These factory methods of Java 9 has many advantages:

  1. Space Efficiency
  2. Immutability
  3. Thread Safe
answered Apr 13, 2018 by Parth
• 4,640 points
0 votes

In Java 8 or earlier:

List<String> string = Arrays.asList("foo", "bar", "baz");

This will give you a List backed by the array, so it cannot change length.
But you can call List.set, so it's still mutable.


In Java 9:

List<String> string = List.of("foo", "bar", "baz");

This will give you an immutable List, so it cannot be changed.
Which is what you want in most cases where you're prepopulating it.

answered Jul 26, 2018 by samarth295
• 2,220 points

Related Questions In Java

0 votes
1 answer

Initialization of an ArrayList in one line.

Actually, probably the "best" way to initialize ...READ MORE

answered Dec 21, 2020 in Java by Gitika
• 65,770 points
6,082 views
0 votes
2 answers

Determining Class of an Object in Java

You can use: Object instance = new SomeClass(); instance.getClass().getName(); ...READ MORE

answered Nov 26, 2018 in Java by Sushmita
• 6,920 points
1,974 views
0 votes
2 answers

In Java, what is the best way to determine the size of an object?

I happened to find a java class "jdk.nashorn.internal.ir.debug.ObjectSizeCalculator", ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,230 points
13,056 views
0 votes
2 answers
0 votes
2 answers

How to convert an array to arraylist?

In Java 9 you can use: List<String> list= List.of("Hello", "World", ...READ MORE

answered Aug 1, 2018 in Java by samarth295
• 2,220 points
1,043 views
0 votes
1 answer

How does contains() method work in evaluating objects?

Generally you should also override hashCode() each time you ...READ MORE

answered May 25, 2018 in Java by Rishabh
• 3,620 points
1,013 views
0 votes
2 answers

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

In addition to Commons Lang, you can ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
16,039 views
0 votes
1 answer

Convert ArrayList<String> to String[] array

Use like this. List<String> stockList = new ArrayList<String>(); stockList.add("stock1"); stockList.add("stock2"); String[] ...READ MORE

answered Dec 22, 2020 in Java by Gitika
• 65,770 points
1,278 views
0 votes
2 answers

How to sort an ArrayList of custom object by property in Java?

You can Sort using java 8 yourList.sort(Comparator.comparing(Classname::getName)); or  yourList.stream().forEach(a -> ...READ MORE

answered Aug 14, 2018 in Java by samarth295
• 2,220 points
3,029 views
0 votes
3 answers

How to sort an array in java?

import java.util.Arrays; public class Sort { ...READ MORE

answered Aug 24, 2018 in Java by Parth
• 4,640 points
1,193 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP