The Stream flow
Introduction to the
In Java 8, thanks to the functional programming enabled by lambda, a new Stream concept was introduced to address the existing drawbacks of collections.
benefits
Let’s start with an example:
Filter out strings starting with ‘zhang’ from names for subset 1, filter out strings of length 3 from subset 1, and iterate through the output.
// The traditional collection method
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Zhang Sanfeng");
names.add("Zhang Dabao");
names.add("Zhang");
names.add(demarger);
names.add("Xiao feng.");
names.add("Li Dabao");
List<String> list1 = new ArrayList<>(); // A string beginning with 'zhang'
for (String name:names){
if (name.startsWith("Zhang")){
list1.add(name);
}
}
List<String> list2 = new ArrayList<>(); // A string of length 3 in list1
for (String name:list1){
if (name.length()==3){ list2.add(name); }}for(String name:list2){ System.out.println(name); }}Copy the code
// stream Indicates the stream mode
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Zhang Sanfeng");
names.add("Zhang Dabao");
names.add("Zhang");
names.add(demarger);
names.add("Xiao feng.");
names.add("Li Dabao");
names.stream().filter(name->name.startsWith("Zhang"))
.filter(name->name.length()==3).forEach(System.out::println);
}
Copy the code
You can see that using stream is much simpler than using traditional collections.
There are two ways to get a stream
- All of the
Collection
The set can be accessed throughstream()
Method to get the stream. Stream
Class static methodsof()
Usually used to convert an array tostream
Flow.
public static void main(String[] args) {
// The collection becomes a stream
List<String> list = new ArrayList<>();
Stream<String> listStream = list.stream();
Set<Integer> set = new HashSet<>();
Stream<Integer> setStream = set.stream();
// The array becomes a stream
String[] names = new String[]{"zs"."ls"."ww"};
Stream<String> namesStream = Stream.of(names);
Stream<Integer> integerStream = Stream.of(1.2.3.4.5);
}
Copy the code
Methods classification
There are two types of methods in a stream:
- Delayed method, return value is still
Stream
type - Finalizing method, the return value is no longer
Stream
Type, for example:count()
和forEach()
.
Common method –fliter()
Filter () replaces a stream with another substream, which takes a parameter of type Predicate (one of the system’s built-in function interfaces).
Stream<T> filter(Predicate<? super T> predicate);
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Zhang Sanfeng");
names.add("Zhang Dabao");
names.add("Zhang");
names.add(demarger);
names.add("Xiao feng.");
names.add("Li Dabao");
// Filter the names collection, only people with the last name zhang, and print it
names.stream().filter(name->name.startsWith("Zhang")).
forEach(System.out::println);
}
Copy the code
Stream feature – Can only be used once
A Stream is a piped Stream and can only be used once. When a Stream terminates, it is closed and an error is reported when its method is called again.
public static void main(String[] args) {
// The array becomes a stream
String[] names = new String[]{"zsf"."ls"."ww"};
Stream<String> namesStream = Stream.of(names);
namesStream.filter(name->name.length()==3).
forEach(System.out::println);
// The stream has been closed, called again
namesStream.forEach(System.out::println);
}
Copy the code
Common method –map()
If you need to map elements from a stream to another stream, you can use the map() method, which is typically used to convert data of one type into data of another type.
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
Copy the code
public static void main(String[] args) {
// The array becomes a stream
String[] strings = new String[]{"1"."2"."3"};
Stream<String> stream1 = Stream.of(strings);
// Use the map method to convert an Integer of type string to an Integer of type Integer, and then iterate over it
Stream<Integer> stream2 = stream1.map(k->Integer.parseInt(k));
stream2.forEach(System.out::println);
}
Copy the code
Common methods –limit()
Limit () can intercept only the first n elements in a stream
public static void main(String[] args) {
// The array becomes a stream
String[] strings = new String[]{"1"."2"."3"."4"};
// Just cut the first three elements, output
Stream.of(strings).limit(3).forEach(System.out::println);
}
Copy the code
Common method — Skip ()
Skip the first n elements in the stream and return a stream of length 0 if n is greater than the number of elements in the stream.
public static void main(String[] args) {
// The array becomes a stream
String[] strings = new String[]{"1"."2"."3"."4"};
// Skip the first two elements, output
Stream.of(strings).skip(2).forEach(System.out::println);
}
Copy the code
Common method –concat()
The static methods of the Stream class are used to merge two streams.
public static void main(String[] args) {
// The array becomes a stream
String[] strings1 = new String[]{"1"."2"."3"."4"};
String[] strings2 = new String[]{"3"."4"."5"."6"};
Stream<String> stream1 = Stream.of(strings1);
Stream<String> stream2 = Stream.of(strings2);
Stream<String> stream3 = Stream.concat(stream1,stream2);
stream3.forEach(System.out::println); / / output 1,2,3,4,3,4,5,6
}
Copy the code