How to convert Stream to List.
Example 1
This is an example of converting a Stream to an array and an array to a List.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class ConvertStreamToList {
public static void main(String[] args) {
Stream<String> stream = Stream.of("Java8", "and", "Stream", "examples");
String[] array = stream.toArray(String[]::new);
List<String> list = Arrays.asList(array);
for (String s : list) {
System.out.println(s);
}
}
}
Output:
Java8
and
Stream
examples
Example 2
This is an example of converting to a List using collect(Collectors.toList())
. It doesn`t convert to Array in the middle.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ConvertStreamToList1 {
public static void main(String[] args) {
Stream<String> stream = Stream.of("Java8", "and", "Stream", "examples");
List<String> list = stream.collect(Collectors.toList());
for (String s : list) {
System.out.println(s);
}
}
}
Output:
Java8
and
Stream
examples
Example 3
This is an example of converting IntStream to Stream and Stream to Array and List in the order.
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class ConvertStreamToList2 {
public static void main(String[] args) {
IntStream intStream = IntStream.range(1, 5);
Stream<Integer> stream = intStream.boxed();
Integer[] result = stream.toArray(Integer[]::new);
List<Integer> list = Arrays.asList(result);
System.out.println(list);
}
}
Output:
[1, 2, 3, 4]
Example 4
This is an example of converting to a List using collect(Collectors.toList())
.
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class ConvertStreamToList3 {
public static void main(String[] args) {
IntStream intStream = IntStream.range(1, 5);
Stream<Integer> stream = intStream.boxed();
List<Integer> list = stream.collect(Collectors.toList());
System.out.println(list);
}
}
Output:
[1, 2, 3, 4]
Related Posts
- Java - Remove items from List while iterating
- Java - How to find key by value in HashMap
- Java - Update the value of a key in HashMap
- Java - How to put quotes in a string
- Java - How to put a comma (,) after every 3 digits
- BiConsumer example in Java 8
- Java 8 - Consumer example
- Java 8 - BinaryOperator example
- Java 8 - BiPredicate Example
- Java 8 - Predicate example
- Java 8 - Convert Stream to List
- Java 8 - BiFunction example
- Java 8 - Function example
- Java - Convert List to Map
- Exception testing in JUnit
- Hamcrest Collections Matcher
- Hamcrest equalTo () Matcher
- AAA pattern of unit test (Arrange/Act/Assert)
- Hamcrest Text Matcher
- Hamcrest Custom Matcher
- Why Junit uses Hamcrest
- Java - ForkJoinPool
- Java - How to use Futures
- Java - Simple HashTable implementation
- Java - Create a file in a specific path
- Java - Mockito의 @Mock, @Spy, @Captor, @InjectMocks
- Java - How to write test code using Mockito
- Java - Synchronized block
- Java - How to decompile a ".class" file into a Java file (jd-cli decompiler)
- Java - How to generate a random number
- Java - Calculate powers, Math.pow()
- Java - Calculate the square root, Math.sqrt()
- Java - How to compare String (==, equals, compare)
- Java - Calculate String Length
- Java - case conversion & comparison insensitive (toUpperCase, toLowerCase, equalsIgnoreCase)