Reference Blog: HTTPS://WWW.JIANSHU.COM/P/9FE8632D0BC2
About Stream
- Java 8 introduces a new stream API. This stream differs from the I/O stream in that it is more like a collection class with iterable, but behaves differently from the collection class.
- Stream is an enhancement to the functionality of a collection object, focusing on a variety of very convenient, efficient aggregation operations for collection objects, or large-scale data manipulation.
- As long as you give what you need to do with the elements it contains, such as "filter out strings greater than 10", "get the first letter of each string," and so on, the Stream implicitly iterates internally, making the appropriate data transformations.
Why to use Stream
- The benefits of functional programming are particularly noticeable. This code is more expressive of the intent of the business logic than its implementation mechanism. Easy-to-read code is also easy to maintain, more reliable, and less prone to error.
- High
For a list operation, here is a description of the use of stream.
The code that gives the person is the basic get and set method.
PackageCom.demo; Public classPerson {PrivateString name; Private intAge ; PrivateString Province; PublicPerson (String name,intAge , String Province) { This. Name =name; This. Age =Age ; This. Province =Province; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicString getprovince () {returnProvince; } Public voidsetprovince (String province) { This. Province =Province; } }
Here is the code for the Main method:
PackageCom.demo;Importjava.util.ArrayList;Importjava.util.Arrays;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Set;Importjava.util.stream.Collectors;ImportJava.util.stream.Stream; Public classTest { Public Static voidMain (string[] args) {List<Person> list =NewArraylist<person>(); Person P1=NewPerson ("A", One, "AAAAA"); Person P2=NewPerson ("B", "BBBBB"); Person P3=NewPerson ("C", "CCCCC"); Person P4=NewPerson ("D", +, "ddddd"); Person P5=NewPerson ("E", "eeeee"); List=arrays.aslist (P1, p2, p3, P4, p5); Filtertest (list); Maptest (list); Flatmaptest (list); Reducetest (); Collecttest (list); } Private Static voidprintln (list<person>list) { for(person p:list) {System.out.println (P.getname ()+ "-" +p.getage () + "-" +p.getprovince ()); } } //filter Age > and name = d Private Static voidFiltertest (list<person>list) {List<Person> temp =NewArraylist<person>(); for(person p:list) {if(P.getage () > && "D". Equalsignorecase (P.getname ())) {Temp.add (P); }} println (temp); List<Person> collect =list. Stream (). Filter (P(P.getage () > && "D". Equalsignorecase (P.getname ()))) . Collect (Collectors.tolist ()); println (collect); List<Person> Collect1 =list. Stream (). Filter (P-{ if(P.getage () > && "D". Equalsignorecase (P.getname ())) { return true; } return false; }). Collect (Collectors.tolist ()); println (COLLECT1); } //Map Private Static voidMaptest (list<person>list) {List<String> temp =NewArraylist<>(); for(person p:list) {Temp.add (P.getname ()); } System.out.println ("Temp=" +temp.tostring ()); List<String> collect =list. Stream (). Map (P-p.getname ()). Collect (Collectors.tolist ()); System.out.println ("Collect=" +collect); List<String> Collect1 =list. Stream (). Map (Person::getname). Collect (Collectors.tol IST ()); System.out.println ("Collect1=" +Collect1); List<String> Collect2 =list. Stream (). Map (P-{ returnP.getname (); }). Collect (Collectors.tolist ()); System.out.println ("Collect2=" +collect2); } //FlatMap Private Static voidFlatmaptest (list<person>list) {List<String> collect =list. Stream (). FLATMAP ( personArrays.stream (Person.getname (). Split ("")) . Collect (Collectors.tolist ()); System.out.println ("Collect=" +collect); List<Stream<String>> Collect1 =list. Stream (). Map ( personArrays.stream (Person.getname (). Split ("")) . Collect (Collectors.tolist ()); System.out.println ("Collect1=" +Collect1); List<String> Collect2 =list. Stream (). Map ( person-Person.getname (). Split ("") . FlatMap (Arrays::stream). Collect (Collectors.tolist ()); System.out.println ("Collect2=" +collect2); List<String> collect3 =list. Stream (). Map ( person-Person.getname (). Split (""). FlatMap (str-arrays.aslist (str). Stream ()). Collect (Collectors.tolist ()); System.out.println ("Collect3=" +collect3); } //Reduce Public Static voidreducetest () {Integer reduce= Stream.of (1, 2, 3, 4). Reduce (Ten, (count, item){System.out.println ("Count:" +count); System.out.println ("Item:" +item); returnCount +item; } ); SYSTEM.OUT.PRINTLN (reduce); Integer Reduce1= Stream.of (1, 2, 3, 4). Reduce (1, (x, y), X +y); System.out.println (Reduce1); String Reduce2= Stream.of ("1", "2", "3"). Reduce ("1", (x, y), (x + "," +y)); System.out.println (REDUCE2); } /*** ToList*/ Public Static voidCollecttest (list<person>list) {List<String> collect =List.stream (). Map (Person::getname). Collect (Collectors.tolist ()); Set<String> Collect1 =List.stream (). Map (Person::getname). Collect (Collectors.toset ()); Map<string, integer> collect2 =List.stream (). Collect (Collectors.tomap (Person::getname, person::getage)); Map<string, string> collect3 =List.stream (). Collect (Collectors.tomap (P->p.getname (), value->{ returnValue+ "1"; })); for(Map.entry<string, string>Entry:collect3.entrySet ()) {System.out.println ("Key=" + entry.getkey () + ", value=" +Entry.getvalue ()); } //treeset<person> Collect4 = List.stream ()//. Collect (Collectors.tocollection (treeset::new)); //System.out.println (COLLECT4);Map<boolean, list<person>> collect5 =List.stream (). Collect (Collectors.groupingby (P"D". Equalsignorecase (P.getname ())); System.out.println (COLLECT5); String COLLECT6=List.stream (). Map (P-p.getname ()). Collect (Collectors.joining (",", "{", "}")); System.out.println (COLLECT6); List<String> collect7 = Stream.of ("1", "2", "3"). Collect (Collectors.reducing (NewArraylist<string> (), X, Arrays.aslist (x), (Y, z),{y.addall (z); returny; })); System.out.println (COLLECT7); }}
Here is the result of the run:
D-14-dddddd-14-dddddd-14-dddddtemp=[a, B, C, D, E]collect=[a, B, C, D, E]collect1=[a, B, C, D, E]collect2=[a, B, C, D, e]c Ollect=[a, B, C, D, e]collect1=[[email protected], [email protected], [email protected], [email protected], [email protect Ed]]collect2=[a, B, C, D, E]collect3=[a, B, C, D, E]count:10item:1count:11item:2count:13item:3count:16item : 420111,1,2,3key=a,[email protected]key=b,[email protected]key=c,[email protected]key=d,[email protected]key=e,[ Email protected]{false=[[email protected], [email protected], [email protected], [email protected]], True=[[email Protected]]}{a,b,c,d,e}[1, 2, 3]
Cond...
Java basic operation of the stream for list