Java Stream to List

Convert Stream to List in java

In this post, we will see how to convert Stream to List in java.

There are multiple ways to convert Stream to List in java.

Using Collectors.toList()

You can pass Collectors.toList() to Stream.collect() method to convert Stream to List in java. Stream’s collect method performs mutable reduction operation on elements of Stream and Collectors.toList() provides a collector which accumulates elements of Stream into the list.

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

Here is an quick example:

Output:

[India, China, France, Germany]

Using Collectors.toCollection()

You can pass Collectors.toCollection() to Stream.collect() method to convert Stream to List in java. This is similar to previous approach, but it gives you more control over type of collection you want.

Here is an example to create LinkedList from Stream.

[India, China, France, Germany]

Using foreach

You can iterate over Stream using foreach() method and add elements to the List.

Here is an example to create LinkedList from Stream.

This approach is not recommended in case you are using Parallel Stream as elements of the Streams may not be processed in original order. You can use forEachOrdered() method to preserve the order in the List.
Output:

[India, China, France, Germany]

Filter Stream and convert to List

If you want to filter elements from Stream, you can use Stream’s filter method.

Output:

[India, Indonesia]

Filter method accepts predicate functional interface to filter a Stream.

Convert infinite Stream to List

You need to limit infinite Stream and then convert it to list.

Here is an exmple:

Output:

[100, 101, 102, 103, 104]

Here we have used iterate() method to generate an infinite Stream and limited it using limit() method and then converted it to list.

That’s all about Java Stream to List.


import_contacts

You may also like:

Related Posts

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    In this post, we will see how to convert Date to LocalDate in java. Sometimes, we may need to convert Date to new Java 8 APIs and vice versa. There are multiple ways to convert Date to LocalDate in java. Read also: Convert LocalDate to Date in java 💻 Awesome Tech Resources: Looking for ⚒️ […]

  • Convert LocalDate to Date in java
    11 January

    Java LocalDate to Date

    In this post, we will see how to convert LocalDate to Date. Java 8 has introduced a lot of new APIs for Date and time. There can be many ways to convert Java LocalDateTime to date. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for tech events? Go […]

  • Convert LocalDateTime to Timestamp in java
    18 November

    Convert LocalDateTime to Timestamp in Java

    In this post, we will how to convert LocalDateTime to Timestamp . Before learning how to convert localdatetime to timestamp, let us learn about LocalDateTime and Timestamp, and understand the importance of this conversion. LocalDateTime LocalDateTime was introcuded in Java 8. LocalDateTime can be imported time package: import java.time.LocalDateTime; LocalDateTime is an immutable object used […]

  • 28 August

    A In-Depth guide to Java 8 Stream API

    In this post, we will see an in-depth overview of Java 8 streams with a lot of examples and exercises. Introduction You may think that Stream must be similar to InputStream or OutputStream, but that’s not the case. A Stream represents a sequence of elements supporting sequential and parallel aggregate operations. Stream does not store […]

  • Java Stream List to Map
    26 April

    Java Stream List to Map

    In this post, we will see how to convert List to Map using Stream in java 8. Collectors’s toMap() can be used with Stream to convert List to Map in java. Consider a class Named Movie which have 3 fields – id, name and genre [crayon-607a43d6c46d7846320739/] Create a list of movies and convert with to […]

  • Java Stream Sorted
    26 April

    Java Stream sorted example

    In this post, we will see how to sort a list with Stream.sorted() method. Java 8 has introduced Stream.sort() method to sort list of elements conveniently. It helps us to write short and concise functional style code rather than boilerplate code. java.util.Stream has two overloaded versions of sorted() method. 💻 Awesome Tech Resources: Looking for […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.