March 27, 2024

Java Stream - Convert a Stream to List

In this tutorial you’ll see how to convert a Stream to list using collector method and utility methods like toList() and toCollection() of Collectors class in Java Stream API.

1. A simple example to collect Stream elements into an ArrayList.

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamToList {

  public static void main(String[] args) {
    Stream<String> streamElem = Stream.of("A", "B", "C", "D");
    List<String> listFromStream = streamElem.collect(Collectors.toList());
    System.out.println(listFromStream.getClass().getName());
    System.out.println("Elements in the list- " + listFromStream);
  }
}
Output
java.util.ArrayList
Elements in the list- [A, B, C, D]

As you can see type of the List returned is ArrayList.

2. If you want to convert the Stream into a LinkedList then you can use Collectors.toCollection() method.

public class StreamToList {

  public static void main(String[] args) {
    Stream<String> streamElem = Stream.of("A", "B", "C", "D");
    List<String> listFromStream = streamElem.collect(Collectors.toCollection(LinkedList :: new));
    System.out.println(listFromStream.getClass().getName());
    System.out.println("Elements in the list- " + listFromStream);
  }
}
Output
java.util.LinkedList
Elements in the list- [A, B, C, D]

As you can see now the type of the List is LinkedList.

3. Converting Stream to List while filtering some of the elements by providing a condition. For example if you want to store only those elements in the List which are greater than 10 then you can filter other elements.

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamToList {
  public static void main(String[] args) {
    List<Integer> numList = Arrays.asList(6, 10, 5, 6, 7, 8, 12, 22);
    List<Integer> listFromStream = numList.stream().filter(e -> e > 10).collect(Collectors.toList());
    System.out.println("Elements in the list- " + listFromStream);
  }
}
Output
Elements in the list- [12, 22]

4. An example with custom object to store only those Students in the List who have scored more than 90.

Student class used is as follows-
public class Student {
  private int rollNo;
  private String name;
  private String stream;
  private int marks;
  Student(int rollNo, String name, String stream, int marks){
    this.rollNo = rollNo;
    this.name = name;
    this.stream = stream;
    this.marks = marks;
  }
  public int getRollNo() {
    return rollNo;
  }
  public String getName() {
    return name;
  }

  public String getStream() {
    return stream;
  }

  public int getMarks() {
    return marks;
  }

  @Override
  public String toString() {
    return "Roll Number: " +  getRollNo() 
        + " Name: " + getName() + " Marks: " + getMarks();
  }
}
public class StreamToList {
  public static void main(String[] args) {
    List<Student> studentList = Arrays.asList(new Student(1, "Mercy", "Science", 75),
              new Student(2, "Ram", "Science", 99),
              new Student(3, "Priscilla", "Art", 68),
              new Student(4, "Jacques", "Maths", 97),
              new Student(5, "Peter", "Science", 75));
    List<Student> listFromStream = studentList.stream().filter(s -> s.getMarks() > 90).collect(Collectors.toList());
    System.out.println("Elements in the list- " + listFromStream);
  }
}
Output
Elements in the list- [Roll Number: 2 Name: Ram Marks: 99, Roll Number: 4 Name: Jacques Marks: 97]

5. If you want to get only the student names in the list then you can use map method along with collect.

public class StreamToList {
  public static void main(String[] args) {
    List<Student> studentList = Arrays.asList(new Student(1, "Mercy", "Science", 75),
              new Student(2, "Ram", "Science", 99),
              new Student(3, "Priscilla", "Art", 68),
              new Student(4, "Jacques", "Maths", 97),
              new Student(5, "Peter", "Science", 75));
    List<String> listFromStream = studentList.stream().map(s -> s.getName()).collect(Collectors.toList());
    System.out.println("Elements in the list- " + listFromStream);
  }
}
Output
Elements in the list- [Mercy, Ram, Priscilla, Jacques, Peter]

That's all for the topic Java Stream - Convert a Stream to List. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment