In this tutorial you’ll see how to convert a Java Stream to List using collect method and Collectors.toList() and Collectors.toCollection() utility methods.
1. In this example a Stream of strings is converted to a List of strings.
import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class StreamToList { public static void main(String[] args) { // Creating stream Stream<String> cityStream = Stream.of("Chicago", "Mumbai", "New Delhi", "Paris"); // Converting Stream to List List<String> cityList = cityStream.collect(Collectors.toList()); // Checking the type of the created List System.out.println(cityList.getClass().getName()); System.out.println("List Elements- " + cityList); } }
Output
java.util.ArrayList List Elements- [Chicago, Mumbai, New Delhi, Paris]
Though the Javadocs say “There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned” but usually ArrayList is returned as checked in the example by printing type of the List.
2. If you want any other type of List i.e. LinkedList while converting Stream to List then you can use Collectors.toCollection(Supplier<C> collectionFactory) method and explicitly specify the type of collection where stream elements are to be stored.
public class StreamToList { public static void main(String[] args) { // Creating stream Stream<String> cityStream = Stream.of("Chicago", "Mumbai", "New Delhi", "Paris"); // Converting Stream to List List<String> cityList = cityStream.collect(Collectors.toCollection(LinkedList :: new)); // Checking the type of the created List System.out.println(cityList.getClass().getName()); System.out.println("List Elements- " + cityList); } }
Output
java.util.LinkedList List Elements- [Chicago, Mumbai, New Delhi, Paris]
3. In this example we’ll see how to get a sub-list from a list of objects by using the original list as stream source and using Collectors class toList() method to collect required elements in another list.
Employee class
public class Employee { private String empId; private int age; private String name; private char gender; private int salary; Employee(String empId, int age, String name, char gender, int salary){ this.empId = empId; this.age = age; this.name = name; this.gender = gender; this.salary = salary; } public String getEmpId() { return empId; } public int getAge() { return age; } public String getName() { return name; } public char getGender() { return gender; } public int getSalary() { return salary; } @Override public String toString() { return "Emp Id: " + getEmpId() + " Name: " + getName() + " Age: " + getAge(); } }
If you want to get a List of employees having salary greater than 10000 then you can use filter() method along with collector() to collect the filtered elements to the list.
public class StreamToList { public static void main(String[] args) { List<Employee> empList = Arrays.asList(new Employee("E001", 40, "Ram", 'M', 5000), new Employee("E002", 35, "Shelly", 'F', 7000), new Employee("E003", 24, "Mark", 'M', 9000), new Employee("E004", 37, "Ritu", 'F', 11000), new Employee("E005", 32, "Anuj", 'M', 12000), new Employee("E006", 28, "Amy", 'F', 14000)); List<Employee> tempList = empList.stream() .filter(e -> e.getSalary() > 10000) .collect(Collectors.toList()); System.out.println("List Elements- " + tempList); } }
Output
List Elements- [Emp Id: E004 Name: Ritu Age: 37, Emp Id: E005 Name: Anuj Age: 32, Emp Id: E006 Name: Amy Age: 28]
4. In this example we’ll try to get the name of female employees in a separate list. That will mean using map() also along with filter() and collect() methods.
public class StreamToList { public static void main(String[] args) { List<Employee> empList = Arrays.asList(new Employee("E001", 40, "Ram", 'M', 5000), new Employee("E002", 35, "Shelly", 'F', 7000), new Employee("E003", 24, "Mark", 'M', 9000), new Employee("E004", 37, "Ritu", 'F', 11000), new Employee("E005", 32, "Anuj", 'M', 12000), new Employee("E006", 28, "Amy", 'F', 14000)); List<String> tempList = empList.stream() .filter(e -> e.getGender() == 'F') .map(e -> e.getName()) .collect(Collectors.toList()); System.out.println("List Elements- " + tempList); } }
Output
List Elements- [Shelly, Ritu, Amy]
That's all for this topic Java Stream - Convert Stream to List. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Advanced Tutorial Page
Related Topics
You may also like-
No comments:
Post a Comment