Java gets Stream stream from collections and arrays
During the Java programming process,Stream
Streams are an important tool for processing data structures such as collections and arrays, and they provide efficient and concise operation methods.
This article will introduce in detail how to obtain data structures such as collections and arrays.Stream
flow.
1. Get Stream from the collection
Java'sCollection
The interface provides us with direct accessStream
Method:
method
-
stream()
: Get a sequential stream. -
parallelStream()
: Get a parallel stream.
Sample code
import .*; import .*; public class Main { public static void main(String[] args) { // Create a collection List<String> list = ("A", "B", "C"); // Get sequential stream Stream<String> stream = (); (::println); // Get parallel streams Stream<String> parallelStream = (); (::println); } }
Output result:
A
B
C
2. Get Stream from the array
Arrays
The class provides static methodsstream()
Used to generate streams from arrays.
method
-
(T[] array)
: Convert the entire array into a sequential stream. -
(T[] array, int startInclusive, int endExclusive)
: Convert part of the range of the array to a stream.
Sample code
import .*; import .*; public class Main { public static void main(String[] args) { // Create an array String[] array = { "A", "B", "C", "D" }; // Convert the entire array to a stream Stream<String> stream = (array); (::println); // Convert partial range of array to stream Stream<String> partialStream = (array, 1, 3); (::println); // Output B, C } }
Output result:
A
B
C
D
B
C
3. Use Get Stream
Stream
The interface provides static methods()
, you can generate streams directly from elements or arrays.
Sample code
import .*; public class Main { public static void main(String[] args) { // Use to generate streams from elements Stream<String> stream = ("A", "B", "C"); (::println); // Use to generate streams from array String[] array = { "X", "Y", "Z" }; Stream<String> arrayStream = (array); (::println); } }
Output result:
A
B
C
X
Y
Z
4. Get Stream from the primitive type array
For basic type arrays (e.g.int[]
、double[]
), a specialized stream method can be used to generate a stream.
method
(int... values)
(double... values)
(int[] array)
Sample code
import .*; public class Main { public static void main(String[] args) { // use IntStream intStream = (1, 2, 3, 4); (::println); // use int[] intArray = { 5, 6, 7, 8 }; IntStream arrayStream = (intArray); (::println); } }
Output result:
1
2
3
4
5
6
7
8
5. Get Stream from Map
Map
Not belongingCollection
Subclass of the interface, so it cannot be called directlystream()
method.
We can passentrySet()
、keySet()
orvalues()
Indirectly obtaining the stream.
Sample code
import .*; import .*; public class Main { public static void main(String[] args) { // Create Map Map<String, Integer> map = new HashMap<>(); ("A", 1); ("B", 2); ("C", 3); // Get key stream Stream<String> keyStream = ().stream(); (::println); // Get the value stream Stream<Integer> valueStream = ().stream(); (::println); // Get key-value convection Stream<<String, Integer>> entryStream = ().stream(); (entry -> (() + ": " + ())); } }
Output result:
A
B
C
1
2
3
A: 1
B: 2
C: 3
Summarize
Data Type | method |
---|---|
gather | stream() or parallelStream() |
Array | () or () |
Basic type array | () or () |
Map | Use entrySet(), keySet(), values() |
The above is personal experience. I hope you can give you a reference and I hope you can support me more.