JAVA8-Functional programming

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.