Collectors toList() method in Java with Examples Last Updated : 25 Jul, 2024 Comments Improve Suggest changes Like Article Like Report The toList() method of Collectors Class is a static (class) method. It returns a Collector Interface that gathers the input data onto a new list. This method never guarantees type, mutability, serializability, or thread-safety of the returned list but for more control toCollection(Supplier) method can be used. This is an un-ordered collector. Syntax:public static Collector<T, ?, R> toList() where: T: The type of the input elements.Interface Collector<T, A, R>: A mutable reduction operation that accumulates input elements into a mutable result container, optionally transforming the accumulated result into a final representation after all input elements have been processed. Reduction operations can be performed either sequentially or in parallel. T: The type of input elements to the reduction operation.A: The mutable accumulation type of the reduction operation.R: The result type of the reduction operation.toList():- Static method of Collectors class and returns a Collector interface object used to store a group of data onto a list. The Collectors class is under the java.util.streams package.Return Value: This method returns a Collector which collects all the input elements into a List, in encounter order Below are the examples to illustrate toList() method in Java: Example 1: Java // Java code to show the implementation of // Collectors toList() function import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; class GFG { // Driver code public static void main(String[] args) { // creating a Stream of strings Stream<String> s = Stream.of("Geeks", "for", "GeeksforGeeks", "Geeks Classes"); // using Collectors toList() function List<String> myList = s.collect(Collectors.toList()); // printing the elements System.out.println(myList); } } Output[Geeks, for, GeeksforGeeks, Geeks Classes] Example 2: Java // Java code to show the implementation of // Collectors toList() function import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; class GFG { // Driver code public static void main(String[] args) { // creating a Stream of strings Stream<String> s = Stream.of("1", "2", "3", "4"); // using Collectors toList() function List<String> myList = s.collect(Collectors.toList()); // printing the elements System.out.println(myList); } } Output[1, 2, 3, 4] Comment More infoAdvertise with us Next Article Collectors toList() method in Java with Examples P piyush25pv Follow Improve Article Tags : Java Technical Scripter Java-Collections Java - util package Java-Functions java-stream Java-Stream-Collectors Java-Collectors +4 More Practice Tags : JavaJava-Collections Similar Reads Collections singletonList() method in Java with Examples The singletonList() method of java.util.Collections class is used to return an immutable list containing only the specified object. The returned list is serializable. This list will always contain only one element thus the name singleton list. When we try to add/remove an element on the returned sin 2 min read Collectors toSet() in Java with Examples Collectors toSet() returns a Collector that accumulates the input elements into a new Set. There are no guarantees on the type, mutability, serializability, or thread-safety of the Set returned. This is an unordered Collector i.e, the collection operation does not commit to preserving the encounter 2 min read Collections synchronizedList() method in Java with Examples The synchronizedList() method of java.util.Collections class is used to return a synchronized (thread-safe) list backed by the specified list. In order to guarantee serial access, it is critical that all access to the backing list is accomplished through the returned list. Syntax: public static < 2 min read Collections unmodifiableList() method in Java with Examples The unmodifiableList() method of java.util.Collections class is used to return an unmodifiable view of the specified list. This method allows modules to provide users with "read-only" access to internal lists. Query operations on the returned list "read through" to the specified list, and attempts t 2 min read Collections list() method in Java with Examples The list() method of java.util.Collections class is used to return an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration. This method provides interoperability between legacy APIs that return enumerations and new APIs that requi 2 min read Like