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.

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

  • Unable to obtain LocalDateTime from TemporalAccessor
    20 February

    [Fixed] Unable to obtain LocalDateTime from TemporalAccessor

    Table of ContentsUnable to obtain LocalDateTime from TemporalAccessor : ReasonUnable to obtain LocalDateTime from TemporalAccessor : FixLocalDate’s parse() method with atStartOfDay()Use LocalDate instead of LocalDateTime In this article, we will see how to fix Unable to obtain LocalDateTime from TemporalAccessor in Java 8. Unable to obtain LocalDateTime from TemporalAccessor : Reason You will generally get […]

  • Java LocalDate to Instant
    17 February

    Convert LocalDate to Instant in Java

    Table of ContentsJava LocalDate to InstantUsing toInstant() wth ZoneIdUsing toInstant() with ZoneOffset In this article, we will see how to convert LocalDate to Instant in Java. Java LocalDate to Instant Instant class provides an instantaneous point in time. When you want to convert LocalDate to Instant, you need to provide time zone. Using toInstant() wth […]

  • Java Instant to LocalDate
    17 February

    Convert Instant to LocalDate in Java

    Table of ContentsUsing ofInstant method [ Java 9+]Using ZoneDateTime’s toLocalDate() [Java 8] In this article, we will see how to convert Instant to LocalDate in java. Using ofInstant method [ Java 9+] Java 9 has introduced static method ofInstant() method in LocalDate class. It takes Instant and ZoneId as input and returns LocalDate object. [crayon-627337e9c67a0339215740/] […]

  • Convert String to LocalDateTime in Java
    17 February

    Convert String to LocalDateTime in Java

    Table of ContentsJava String to LocalDateTimeConvert String to LocalDateTime with custom format In this article, we will see how to convert String to LocalDateTime in Java. LocalDateTime class was introduced in Java 8. LocalDateTime represents local date and time without timezone information. It is represented in ISO 8601 format (yyyy-MM-ddTHH:mm:ss) by default. Java String to […]

  • Java LocalDateTIme to String
    16 February

    Format LocalDateTime to String in Java

    Table of ContentsJava LocalDateTime To StringConvert LocalDateTime to Time Zone ISO8601 StringParse String to LocalDateTime In this article, we will see how to format LocalDateTime to String in java. Java LocalDateTime To String To format LocalDateTime to String, we can create DateTimeFormatter and pass it to LocalDateTime’s format() method. [crayon-627337e9c6c3e051192198/] Here are steps: Get LocalDateTime […]

  • Find duplicate elements in the Stream
    17 October

    Java 8 – Find duplicate elements in Stream

    Table of ContentsIntroductionUsing distinct()Using Collections.frequency()Using Collectors.toSet()Using Collectors.toMap()Using Collectors.groupingBy()Conclusion Introduction When working with a collection of elements in Java, it is very common to have duplicate elements, and Java provides different APIs that we can use to solve the problem. Java 8 Stream provides the functionality to perform aggregate operations on a collection, and one of […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.