Pages

Wednesday, November 24, 2021

Java 8 Stream Collect to List

1. Overview

In this tutorial, We'll learn how to convert the stream of items into the list.

Below are the examples to collect the stream to list using Collectors.collect() and Collectors.toCollection() method.


Java 8 Stream Collect to List



2. Java 8 Strem Collect to List


To convett stream of strings into list by using stream.collect() and along with Collectors.collect() method.

Example 1:
package com.javaprogramto.java8.collectors.tolist;

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

public class StreamCollectToListExample1 {

	public static void main(String[] args) {
		Stream<String> vowels = Stream.of("A", "E", "I", "O", "U");

		List<String> list = vowels.collect(Collectors.toList());

		System.out.println("stream collect to list output : " + list);
	}
}

Output:
stream collect to list output : [A, E, I, O, U]

3. Java 8 Stream Collect to LinkedList


Below example to collect the stream of objects into the LinkedList using Collectors.toCollection() method.

Example 2:
package com.javaprogramto.java8.collectors.tolist;

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

public class StreamCollectToListExample2 {

	public static void main(String[] args) {
		Stream<String> vowels2 = Stream.of("A", "E", "I", "O", "U");

		List<String> list = vowels2.collect(Collectors.toCollection(LinkedList::new));

		if(list instanceof LinkedList) {
			System.out.println("Returned list is a instacne of LinkedList");
		}
		System.out.println("stream collect to LinkedList output : " + list);

	}
}

Output:
Returned list is a instacne of LinkedList
stream collect to LinkedList output : [A, E, I, O, U]

4. Stream filtering and collect into list


Use filter() method to filter values of stream and collet into List.

Example 3:
package com.javaprogramto.java8.collectors.tolist;

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

public class StreamCollectToListExample3 {

	public static void main(String[] args) {
		Stream<String> vowels2 = Stream.of("A", "E", "I", "O", "U");

		List<String> list = vowels2.filter(str -> !str.equals("E") && !str.equals("O")).collect(Collectors.toList());

		if(list instanceof LinkedList) {
			System.out.println("Returned list is a instacne of LinkedList");
		} else if(list instanceof ArrayList) {
			System.out.println("Returned list is a instacne of ArrayList");
		}  
		System.out.println("stream collect to ArrayList output : " + list);

	}
}

Output:
Returned list is a instacne of ArrayList
stream collect to ArrayList output : [A, I, U]


5. Java 8 Stream + map() + filter() + to list


Convert each string into its ASCII value with map() method and take odd numbers from it as a list.

Example 4:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamCollectToListExample4 {

	public static void main(String[] args) {
		Stream<String> vowels2 = Stream.of("A", "E", "I", "O", "U");

		List<Integer> list = vowels2.map(str -> (int) str.charAt(0)).filter(number -> number % 2 != 0)
				.collect(Collectors.toCollection(LinkedList::new));

		if (list instanceof LinkedList) {
			System.out.println("Returned list is a instacne of LinkedList");
		} else if (list instanceof ArrayList) {
			System.out.println("Returned list is a instacne of ArrayList");
		}
		System.out.println("stream collect to ArrayList output : " + list);

	}
}

 Output:
Returned list is a instacne of LinkedList
stream collect to ArrayList output : [65, 69, 73, 79, 85]

6. Collect objects from infinite stream to List


Use limit() method to get the first n objects from the infinite stream.

Example 5:
package com.javaprogramto.java8.collectors.tolist;

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

public class StreamCollectToListExample5 {

	public static void main(String[] args) {
		
		IntStream infiniteStream  = IntStream.iterate(1000,  i -> i + 1000);
		
		List<Integer> numbersList = infiniteStream.limit(5).boxed().collect(Collectors.toList());
		
		System.out.println("infinite stream collect to ArrayList output : " + numbersList);

	}
}

Output:
infinite stream collect to ArrayList output : [1000, 2000, 3000, 4000, 5000]

7. Conclusion


In this article, we've seen how many ways stream can be collected into the List or LinkedList in java 8 using Collectors.toList() and Collectors.toCollection() method.



No comments:

Post a Comment

Please do not add any spam links in the comments section.