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,050 points
321 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 parth295
• 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,190 points

Related Questions In Java

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,880 points
83 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 in Java by Sirajul
• 41,000 points
244 views
0 votes
2 answers
0 votes
1 answer

How can I Sort an ArrayList in Java

You can sort the ArrayList in 2 ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,350 points
83 views
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,190 points
56 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,540 points
34 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 in Java by Sirajul
• 41,000 points
1,195 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
727 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 13, 2018 in Java by samarth295
• 2,190 points
181 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 parth295
• 4,640 points
85 views