Collectors toList() method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

[Geeks, for, GeeksforGeeks, Geeks Classes]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

[1, 2, 3, 4]


My Personal Notes arrow_drop_up

Im a final year MCA student at Panjab University, Chandigarh, one of the most prestigious university of India I am skilled in various aspects related to Web Development and AI I have worked as a freelancer at upwork and thus have knowledge on various aspects related to NLP, image processing and web

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.