3 minutes, learn a little knowledge of Lambda

♥ Previous remarks

Remember the little knowledge of Lambda introduced to you before? Today, let's introduce the key points of flow API.

Stream API

Stream is the key abstract concept of dealing with collections in Java 8. It can specify your of collections and perform data operations such as finding, filtering and mapping.

Stream uses a display method similar to querying data from the database with SQL statements to provide a high-level abstraction expressed by Java collections and.

(Stream) is a stream?

A data channel is an element from a data source and supports aggregation operations.

be careful:

  • The stream itself does not store elements.

  • The flow does not change the source object. Instead, they return a new stream with results.

  • Stream operations are deferred. This means that they will not implement the results until they need them.

Three steps of operation flow

  • Create stream

  • Intermediate operation

  • Start operation

Create stream

//Set list < string > List = new ArrayList < > (); Stream<String> stream = list. stream();  // Array intstream = arrays stream(new int[10]);// Stream static method stream < string > A = stream of("a", "2", "3");// Create an infinite stream stream < integer > iterate = stream iterate(0, (x) -> x + 2);
Stream intermediate operation

A stream can have multiple intermediate operations to form a water line and continuously perform a baby stream operation.

1. Filter (predicate p)

Receive Lambda and some elements from the stream

Stream<String> stream = Stream.of("abc", "122", "lemon","haha");
stream = stream.filter((e)->{    System.out.println("filter Intermediate operation");    return e.length() > 3;});
stream.forEach(System.out::println);

2. Difference ()

Filter and eliminate duplicate elements through hashCode() and equals() of the elements generated by the flow

Stream<String> stream = Stream.of("abc","abc", "122", "lemon","haha","abc");
stream = stream.distinct();
stream.forEach(System.out::println);

3. Limit

Cut off the number of elements so that they do not exceed the given value.

Stream<String> stream = Stream.of("abc","abc", "122", "lemon","haha","abc");
stream = stream.distinct().limit(2);
stream.forEach(System.out::println);

4,skip(long n)

Skip elements, skip the first N elements, complementary to limit(n)

Stream<String> stream = Stream.of("abc","abc", "122", "lemon","haha","abc");
stream = stream.skip(2);
stream.forEach(System.out::println);

5. Map (function f)

Receive a function as an argument, which will be applied to each element and mapped to a new element.

Stream<String> stream = Stream.of("abc","abc", "122", "lemon","haha","abc");
stream = stream.map((e) -> e.toUpperCase());
stream.forEach(System.out::println);

6. Flatmap (function f)

Receive a function parameter, replace each value in the stream with another stream, and then connect all streams into one stream

Stream<String> stream = Stream.of("abc","abc", "122", "lemon","haha","abc");
stream.flatMap((e) -> {    String[] split = e.split("");    return Stream.of(split);}).forEach(System.out::println);

7. Sort ()

Generate a new stream in which the examples are naturally in order.

Stream<String> stream = Stream.of("abc","abc", "122", "lemon","haha","abc");
stream.sorted().forEach(System.out::println);

8. Sort (Comparator comp)

Generate a new stream in which the flow is in the order of the comparator (custom example)

Stream<String> stream = Stream.of("abc","abc", "122", "lemon","haha","abc");
       stream.sorted(Comparator.comparingInt(String::length)).forEach(System.out::println);

Termination of Stream

1. Count ()

Returns the total number of elements in the stream

Stream<String> stream = Stream.of("abc","abc", "122", "lemon","haha","abc");
long count = stream.sorted(Comparator.comparingInt(String::length)).count();
System.out.println(count);

2. Max (comparator c)

Returns the level in the stream

Stream<String> stream = Stream.of("123","1234", "12345", "123456","1234567","aaaaaaaaaa");
Optional<String> max = stream.max(Comparator.comparingInt(String::length));
System.out.println(max.get());

3. Min (comparator c)

Return calls in stream

Stream<String> stream = Stream.of("123","1234", "12345", "123456","1234567","aaaaaaaaaa");
Optional<String> min = stream.min(Comparator.comparingInt(String::length));
System.out.println(min.get());

4. Foreach (consumer c)

Internal

Stream<String> stream = Stream.of("123","1234", "12345", "123456","1234567","aaaaaaaaaa");
stream.forEach(System.out::println);

5,reduce(T iden, BinaryOperator b)

Combine the elements in the stream to get a value and return T

Stream<Integer> stream = Stream.of(1,2,3,4,5);
int result = stream.reduce(0, (x, y) -> {    return x + y;});System.out.println(result);

If my blog is helpful to you and you like my blog content, please click "like", "comment" and "collect" for three times!

Finally, basic knowledge, Linux essentials, Shell, Internet program principles, Mysql database, special topics of packet capture tools, interface test tools, advanced test Python programming, Web automation test, APP automation test, interface automation test, advanced continuous integration of test, test architecture development test framework, performance test, security test and other supporting learning resources [free].

Keywords: Programmer software testing linq

Added by nivek9991 on Sat, 05 Mar 2022 09:48:48 +0200