OKdevTV Docs
Home OKdevTV
ESC
java 1 min read 0.9 KB Raw Markdown
On this page (3)

Lambda expression

  • Since Java SE 8
  • clear and concise way to represent one method interface using an expression
  • easier to iterate through, filter, and extract data from a Collection
  • new concurrency features improve performance in multicore environments

Format

text
Argument List   Arrow Token   Body
(int x, int y)      ->        x + y

Example

java
public class RunnableTest {
  public static void main(String[] args) {

    // Anonymous Runnable
    Runnable r1 = new Runnable(){
      @Override
      public void run(){
        System.out.println("Hello world one!");
      }
    };

    // Lambda Runnable
    Runnable r2 = () -> System.out.println("Hello world two!");

    r1.run();
    r2.run();

   }
 }

ref