Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Stream screening, slicing and Mapping in Java8

Shulou Source: shulou.com Published: 2022-06-02 12:01:35 10月01日 Update

This article will explain in detail how to use Stream screening, slicing and mapping in Java8. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Three steps to using Stream

Create Stream

Intermediate operation

Terminate operation (terminal operation)

@ Testpublic void test1 () {/ / create Stream / / 1. You can get the serial flow through the stream () provided by the Collection series collection or parallelStream () get the parallel flow List list = new ArrayList (); Stream stream1 = list.stream (); / / 2. Get the array stream Employee [] emps = new Employee [10]; Stream stream2 = Arrays.stream (emps); / / 3 through the static method stream () in Arrays. Through the static method of () Stream stream3 = Stream.of ("aa", "bb", "cc") in the Stream class; / / 4. Create infinite flow / / create infinite flow by iteration Stream stream4 = Stream.iterate (0, (x)-> x + 2); / / Intermediate operation and termination operation stream4.limit (10) .forEach (System.out::println) / / create infinite flow Stream.generate (()-> Math.random ()) .limit (5) .forEach (System.out::println) by generating random numbers;} inner iteration and outer iteration

Internal iterations: iterations are done by Stream API itself

External iteration: completed by a program written by yourself

/ / Internal iteration: iterative operation is completed by Stream API @ Testpublic void test1 () {/ / Intermediate Operation: no operation is performed Stream employeeStream = employees.stream () .filter ((e)-> {System.out.println ("Intermediate Operation of Stream API") Return e.getAge () > 35;}); / / terminate operation: execute everything at once, that is, "lazy evaluation" employeeStream.forEach (System.out::println);} / / external iteration @ Testpublic void test2 () {Iterator iterator = employees.iterator () While (iterator.hasNext ()) {System.out.println (iterator.next ());} filtering and slicing usage

Filter--- receives Lambda and excludes certain elements from the stream

Limit--- truncates the flow so that its elements do not exceed a given number

Skip (n)-skips elements and returns a stream that throws away the first n elements. If there are less than n elements in the stream, an empty stream is returned, which is complementary to limit (n)

Distinct--- filtering to remove duplicate elements through hashCode () and equals () of the elements generated by the stream

List employees = Arrays.asList (new Employee (Zhang San, 18, 9999.99), new Employee (Li Si, 38, 5555.99), new Employee (Wang Wu, 50, 6666.66), new Employee (Zhao Liu, 16, 3333.33), new Employee (Tian Qi, 8, 7777.77), new Employee (Tian Qi, 8, 7777.77) New Employee (Tianqi, 8, 7777.77) @ Testpublic void test3 () {employees.stream () .filter ((e)-> {System.out.println ("short circuit"); return e.getSalary () > 5000;}) .limit (2) .forEach (System.out::println) } @ Testpublic void test4 () {employees.stream () .filter ((e)-> e.getSalary () > 5000) .skip (2) .forEach (System.out::println) } @ Testpublic void test5 () {employees.stream () .filter ((e)-> e.getSalary () > 5000) .skip (2) .forEach (System.out::println);} usage of mapping

Map--- receives Lambda, converts elements into other forms, or extracts information. Take a function as an argument, which is applied to each element and mapped to a new element.

FlatMap--- takes a function as an argument, replaces each value in the stream with another stream, and then connects all streams into one stream

Public static Stream filterCharacter (String str) {List list = new ArrayList (); for (Character ch: str.toCharArray ()) {list.add (ch);} return list.stream ();} @ Testpublic void test6 () {List list = Arrays.asList ("aaa", "bbb", "ccc", "ddd", "eee") List.stream () .map ((str)-> str.toUpperCase ()) .forEach (System.out::println); System.out.println ("- -"); employees.stream () .map (Employee::getName) .forEach (System.out::println) System.out.println ("- -"); Stream stream = list.stream () .map (TestStreamAPI2::filterCharacter); / / {{a, a}, {b, b}} stream.forEach ((sm)-> {sm.forEach (System.out::println);}) System.out.println ("- -"); Stream sm = list.stream () .flatMap (TestStreamAPI2::filterCharacter); / / {a, b, b, b} sm.forEach (System.out::println) } this is the end of the article on "Stream screening, slicing and mapping in Java8". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Elements iterations parameters Tianqi articles content functions methods more static generation good practical one-time three information can be passed form inertia array Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Technology Docker MySQL Shulou Tech Info