How to initialize a list in a single line in Java with a specified value? Last Updated : 16 Dec, 2019 Comments Improve Suggest changes Like Article Like Report Given a value N, the task is to create a List having this value N in a single line in Java. Examples: Input: N = 5 Output: [5] Input: N = GeeksForGeeks Output: [GeeksForGeeks] Approach: Get the value N Create an array with this value N Create a List with this array as an argument in the constructor Below is the implementation of the above approach: Java // Java program to initialize a list // in a single line with a specified value import java.io.*; import java.util.*; class GFG { // Function to create a List // with the specified value public static <T> List<T> createList(T N) { // Currently only one value is taken int size = 1; // Create an array of size 1 T arr[] = (T[]) new Object[1]; // Add the specified value in the array arr[0] = N; // System.out.println(Arrays.toString(arr)); List<T> list = Arrays.asList(arr); // return the created list return list; } // Driver code public static void main(String[] args) { int N = 1024; System.out.println("List with element " + N + ": " + createList(N)); String str = "GeeksForGeeks"; System.out.println("List with element " + str + ": " + createList(str)); } } Output: List with element 1024: [1024] List with element GeeksForGeeks: [GeeksForGeeks] Comment More infoAdvertise with us Next Article How to initialize a list in a single line in Java with a specified value? C code_r Follow Improve Article Tags : Java Java-List-Programs Practice Tags : Java Similar Reads Initialize a list in a single line with a specified value Given a value N, the task is to create a List having this value N in a single line in Java using Collection Framework only. Examples: Input: N = 5 Output: [5] Input: N = GeeksForGeeks Output: [GeeksForGeeks] Approach: Get the value N Generate a Collection with a single value N using Collections.nCop 1 min read Initialize a list in a single line with a specified value using Java Stream Given a value N, the task is to create a List having this value N in a single line in Java using Stream. Examples: Input: N = 5 Output: [5] Input: N = GeeksForGeeks Output: [GeeksForGeeks] Approach: Get the value N Generate the Stream using generate() method Set the size of the List to be created as 2 min read How to Initialize an Array in Java? An array in Java is a linear data structure that is used to store multiple values of the same data type. In an array, each element has a unique index value, which makes it easy to access individual elements. We first need to declare the size of an array because the size of the array is fixed in Java 5 min read Initialize a static Map in Java using Double Brace Initialization In this article, a static map is created and initialised in Java using Double Brace Initialization. Static Map in Java A static map is a map which is defined as static. It means that the map becomes a class member and can be easily used using class. Double Brace Initialization In Double Brace Initia 2 min read Convert String into comma separated List in Java Given a String, the task is to convert it into comma separated List. Examples: Input: String = "Geeks For Geeks" Output: List = [Geeks, For, Geeks] Input: String = "G e e k s" Output: List = [G, e, e, k, s] Approach: This can be achieved by converting the String into String Array, and then creating 2 min read How to Add Element at First and Last Position of LinkedList in Java? LinkedList is a part of Collection framework present inside java.util package. This class is an implementation of LinkedList data structure which is a linear data structure where the elements are not stored in a contiguous manner and every element is a separate object with a data field and address f 2 min read LinkedList listIterator() Method in Java In Java, the listIterator() method of the LinkedList class returns a ListIterator that allows us to iterate over the elements of the list.Example: Java// Java Program to Demonstrate the // use of listIterator() in LinkedList import java.util.LinkedList; import java.util.ListIterator; public class Ge 3 min read Convert a String to a List of Characters in Java In Java, to convert a string into a list of characters, we can use several methods depending on the requirements. In this article, we will learn how to convert a string to a list of characters in Java.Example:In this example, we will use the toCharArray() method to convert a String into a character 3 min read Initialize an ArrayList in Java ArrayList is a part of the collection framework and is present in java.util package. It provides us dynamic arrays in Java. Though it may be slower than standard arrays, but can be helpful in programs where lots of manipulation in the array is needed.ArrayList inherits the AbstractList class and imp 4 min read List add(int index, E element) method in Java The add(int index, E ele) method of List interface in Java is used to insert the specified element at the given index in the current list. Implementation:Java// Java code to illustrate add(int index, E elements) import java.util.*; public class ArrayListDemo { public static void main(String[] args) 2 min read Like