

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do you create a list with values in Java?
We can utilize Arrays.asList() method to get a List of specified elements in a single statement.
Syntax
public static <T> List<T> asList(T... a)
Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)
Type Parameter
T − The runtime type of the array.
Parameters
a − The array by which the list will be backed.
Returns
A list view of the specified array
In case we use Arrays.asList() then we cannot add/remove elements from the list. So we use this list as an input to the ArrayList constructor to make sure that list is modifiable.
Example
The following example shows how to create a List with multiple items in a single statement.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { // Create a list object List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3)); // print the list System.out.println(list); list.add(4); System.out.println(list); } }
Output
This will produce the following result −
[1, 2, 3] [1, 2, 3, 4]
- Related Questions & Answers
- How do you create a list in Java?
- How do you create a list from a set in Java?
- How do you create an empty list in Java?
- How do you copy a list in Java?
- How do you make a list iterator in Java?
- How do you turn a list into a Set in Java?
- How do you convert list to array in Java?
- How do you add an element to a list in Java?
- How do you check a list contains an item in Java?
- How do you make a shallow copy of a list in Java?
- Create a to-do list with JavaScript
- How do you create a clickable Tkinter Label?
- Java Program to create a new list with values from existing list with Function Mapper
- Java Program to create a new list with values from existing list with Lambda Expressions
- How do you check if an element is present in a list in Java?
Advertisements