Initialize a list in a single line with a specified value Last Updated : 02 Jan, 2020 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 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.nCopies() method Store this collection as a List. Below is the implementation of the above approach: Java // Java program to initialize a list in a single // line with a specified value // using Collection Framework only import java.io.*; import java.util.*; import java.util.stream.*; 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; // Generate a Collection with a single value N List<T> list = Collections.nCopies(size, N); 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 Initialize a list in a single line with a specified value C code_r Follow Improve Article Tags : Java Java-List-Programs Practice Tags : Java Similar Reads How to initialize a list in a single line in Java 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. 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 2 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 10 Ways to Create a Stream in Java The Stream API, introduced in Java 8, it is used to process collections of objects. Stream is a sequence of objects, that supports many different methods which can be pipe lined to produce the desired result. The features of Java stream are â A stream is not a data structure alternatively it takes i 9 min read Initializing a List in Java The Java.util.List is a child interface of Collection. It is an ordered collection of objects where duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by the ArrayList, LinkedList, Vector, and 6 min read Java Program to Access the Part of List as List A List is an ordered sequence of elements stored together to form a collection. A list can contain duplicate as well as null entries. A list allows us to perform index-based operations, that is additions, deletions, manipulations, and positional access. Java provides an in-built interface <<ja 4 min read Like