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>();

Is there a better way to do it?

Apr 13, 2018 in Java by Daisy
• 8,040 points

2 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,600 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,840 points
0 votes
1 answer

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

Please check the below code to arrive ...READ MORE

answered Nov 21, 2018 in Java by Frankie
• 9,710 points
0 votes
1 answer

How can I convert an 'ArrayList<String> to 'String[]' in Java

List<String> list = new ArrayList<String>(); String[] array = ...READ MORE

answered Apr 20, 2018 in Java by Daisy
• 8,040 points
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,310 points
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
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,520 points
0 votes
1 answer

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

List<T>.toArray won't work because there's no conversion from Integer to int You ...READ MORE

answered Oct 30, 2018 in Java by Daisy
• 8,040 points
+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
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 -> ...READ MORE

answered Aug 13, 2018 in Java by samarth295
• 2,190 points
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,600 points

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.