1. What is functional programming?
At the heart of functional programming is the use of immutable values and functions when thinking about problems, and functions that process a value and map it to another value.
2. Lambda expression Learning
Important: A lambda expression is an anonymous method that passes behavior like data, and the common structure is as follows:binaryoperator<integer> add = (x, y)->x+y.
3. Flow: Common flow operations
3.1, Collect (ToList ())
// Early Evaluation Method long count = Artists.stream (). Filter (A->a.isfrom ("China"long count2 = Artists.stream () . Filter (B->b.getname (). Equals ("TTG"long count3 = artists.stream () . Filter (A- >{ System.out.println (A.getname ()); return a.isfrom ("China"); }) . Count ();
List<string> collected = Stream.of ("A", "B", "C"= jsonarray.tojsonstring (collected); boolean isequal = Collected.equals (Arrays.aslist ("A", "B", "C"));
3.2. Map
// 2.map list<string> str = stream.of ("A", "B", "C") , s.touppercase ()) . Collect ( Collectors.tolist ()); = jsonarray.tojsonstring (str); boolean issame = Realstr.equals (Arrays.aslist ("A", "B", "C"));
3.3. Filter
// 3.filter List<object> str2 = Stream.of ("abc", "1abc", "ABC1") , Isdight (S.charat (0))) . Collect (Collectors.tolist ()); = Jsonarray.tojsonstring (STR2);
3.4, FlatMap
// The Flatmap:flatmap method can replace a value with a stream and then concatenate multiple streams into a stream List<string> STR3 = Stream.of (Arrays.aslist ("A", "B", "C"), Arrays.aslist ("D", "E")) Stringlist.stream ()) . Collect (Collectors.tolist ()); = Jsonarray.tojsonstring (STR3);
3.5, Max and Min
// Max min list<track> tracks = Arrays.aslist ( "Bakai", 524), new track ("Violets For Your Furs ", 378), new track (" Time Is ", 451= tracks.stream (). Min ( Comparator.comparing (track >track.getlength ())) . get ();
3.6. Integration + General mode
//Reduce//1. Accumulation intCOUNT4 = Stream.of). Reduce (0, (acc,res)->acc+res); //IntegrationAlbum Album =NULL; Set<String> Orgin =Album.getmusicians (). Filter (ArtistArtist.getname (). StartsWith ("the"). Map (artist-artist.getnationality ()). Collect (Collectors.toset ()); System.out.println (COUNT4); intTotalmembers = 0; for(Artist artist:artists) {Stream<Artist> Members =artist.getmembers (); Totalmembers+=Members.count (); } }
JAVA8-Functional programming