-----LAMBDA for new features of JAVA8

Source: Internet
Author: User

    1. Introduction to Lambda
      Lambda is added to the JDK as a new feature, explaining Java's support for other languages and absorbing advanced methods in other languages. LAMBDA expressions provide a reference to a type, a reference to a method, and a default method in Java.

The explanation for Lambda is not translated in the wiki Wikipedia to preserve the original flavor (see everyone's understanding, there are different views welcome to explore)
Lambda (programming), a function (or a subroutine) defined, and possibly called, without being bound to an identi Fier
lambda expression in lambda calculus, a formal system in mathematical logic and computer science for expressing C Omputation by the variable binding and substitution
From: http://medianetwork.oracle.com/video/player/3373491280001
2. Example
Import java.util.ArrayList;
Import java.util.Collections;
Import Java.util.Comparator;
Import java.util.List;

public class Lambdaexpresstiontouris {

public static void Main (string[] args) {list<string> ocuppys1 = new arraylist<string> ();     Ocuppys1.add ("Bussinessman");     Ocuppys1.add ("Doctor");     Ocuppys1.add ("Teacher");     Ocuppys1.add ("Programer");     Ocuppys1.add ("Stuedent");     list<string> ocuppys2 = new arraylist<string> ();     Ocuppys2.add ("Bussinessman");     Ocuppys2.add ("Doctor");     Ocuppys2.add ("Teacher");     Ocuppys2.add ("Programer");     Ocuppys2.add ("Stuedent");     Lambdaexpresstiontouris Letouris = new Lambdaexpresstiontouris ();     SYSTEM.OUT.PRINTLN ("Use Java 7 sort:");     System.out.println ("Before:" +ocuppys1);     Letouris.sortbyjava7 (OCUPPYS1);     System.out.println ("Before:" +ocuppys1);     SYSTEM.OUT.PRINTLN ("Use Java 8 sort:");     System.out.println ("Before:" +ocuppys2);     Letouris.sortbyjava8 (OCUPPYS2); System.out.println ("Before:" +ocuppys2);} Original sorting method using Java 7 private void SortByJava7 (list<string> liststr) {collections.sort (liststr, New Comparator<sTring> () {public int compare (string s1, string s2) {return s1.compareto (S2); }    });} Sort using a Java 8 lambda expression private void sortByJava8 (list<string> liststr) {Collections.sort (Liststr, (S1,S2)-S 1.compareTo (S2));}

}

    1. Summarize
      1,lambda is a function.
      2,JAVA8 defines a function interface that is used by the new API
      3, Default function method
      4, use it to implement a reference to the method
      5, use its sort function to implement sorting

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

-----LAMBDA for new features of JAVA8

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.