Demo:

package com.qhong;

public class Main {
public static void main(String[] args) throws Exception {
new Thread(new Runnable(){
@Override
public void run(){
System.out.println("Before Java8");
}
}).start(); new Thread(()->System.out.println("In Java8")).start();
}
}
Before Java8
In Java8

循环:

package com.qhong;

import java.util.Arrays;
import java.util.List; public class Main {
public static void main(String[] args) throws Exception {
List<String> list= Arrays.asList("a","b","c");
for(String str:list){
System.out.println(str);
}
System.out.println("----------------");
list.forEach(x->System.out.println(x));
System.out.println("----------------");
list.forEach(System.out::println);
}
}
a
b
c
----------------
a
b
c
----------------
a
b
c

Predicate用法:

package com.qhong;

import java.util.Arrays;
import java.util.List;
import java.util.function.*;
import java.lang.*; public class Main {
public static void main(String[] args) throws Exception {
List<String> languages = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp"); System.out.println("Languages which starts with J :");
filter(languages, (str)->str.startsWith("J")); System.out.println("Languages which ends with a ");
filter(languages, ( String str)->str.endsWith("a")); System.out.println("Print all languages :");
filter(languages, (str)->true); System.out.println("Print no language : ");
filter(languages, (str)->false); System.out.println("Print language whose length greater than 4:");
filter(languages, (str)->str.length() > 4);
} public static void filter(List<String> names, Predicate<String> condition) {
names.stream().filter((name) -> (condition.test(name)))
.forEach((name) -> {
System.out.println(name + " ");
});
}
}
Languages which starts with J :
Java
Languages which ends with a
Java
Scala
Print all languages :
Java
Scala
C++
Haskell
Lisp
Print no language :
Print language whose length greater than 4:
Scala
Haskell

code:

package com.qhong;

/**
* Created by qhong on 2017/3/14.
*/
public class Employee {
public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getFirstName() {
return firstName;
} public void setFirstName(String firstName) {
this.firstName = firstName;
} public String getLastName() {
return lastName;
} public void setLastName(String lastName) {
this.lastName = lastName;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} private Integer id;
private String firstName;
private String lastName;
private Integer age; public Employee(Integer id, String firstName, String lastName, Integer age){
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
} @Override
public String toString() {
return "\n["+this.id+","+this.firstName+","+this.lastName+","+this.age+"]";
}
}
package com.qhong;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List; public class Main {
public static void main(String[] args) throws Exception {
List<Employee> employees = getEmployees(); //Sort all employees by first name
employees.sort(Comparator.comparing(e -> e.getFirstName())); //OR you can use below
//employees.sort(Comparator.comparing(Employee::getFirstName)); //Let's print the sorted list
System.out.println(employees);
System.out.println("======================"); Comparator<Employee> comparator = Comparator.comparing(e -> e.getFirstName());
employees.sort(comparator.reversed());
System.out.println(employees);
System.out.println("======================");
//Sorting on multiple fields; Group by.
Comparator<Employee> groupByComparator = Comparator.comparing(Employee::getFirstName)
.thenComparing(Employee::getLastName);
//下面这种方式不行,不知道问题
// Comparator<Employee> groupByComparator=Comparator.comparing(e->e.getFirstName())
// .thenComparing(x->x.getLastName);
employees.sort(groupByComparator);
System.out.println(employees); System.out.println("======================"); //Parallel Sorting 并行排序
Employee[] employeesArray = employees.toArray(new Employee[employees.size()]);
System.out.println(employeesArray);
//Parallel sorting
Arrays.parallelSort(employeesArray, groupByComparator); System.out.println(employeesArray);
} private static List<Employee> getEmployees(){
List<Employee> employees = new ArrayList<>();
employees.add(new Employee(1,"Lokesh", "Gupta", 32));
employees.add(new Employee(2,"Aman", "Sharma", 28));
employees.add(new Employee(3,"Aakash", "Yaadav", 52));
employees.add(new Employee(4,"James", "Hedge", 72));
employees.add(new Employee(5,"David", "Kameron", 19));
employees.add(new Employee(6,"Yash", "Chopra", 25));
employees.add(new Employee(7,"Karan", "Johar", 59));
employees.add(new Employee(8,"Balaji", "Subbu", 88));
employees.add(new Employee(9,"Vishu", "Bissi", 33));
employees.add(new Employee(10,"Lokesh", "Ramachandran", 60));
return employees;
}
}

Output:

[
[3,Aakash,Yaadav,52],
[2,Aman,Sharma,28],
[8,Balaji,Subbu,88],
[5,David,Kameron,19],
[4,James,Hedge,72],
[7,Karan,Johar,59],
[1,Lokesh,Gupta,32],
[10,Lokesh,Ramachandran,60],
[9,Vishu,Bissi,33],
[6,Yash,Chopra,25]]
======================
[
[6,Yash,Chopra,25],
[9,Vishu,Bissi,33],
[1,Lokesh,Gupta,32],
[10,Lokesh,Ramachandran,60],
[7,Karan,Johar,59],
[4,James,Hedge,72],
[5,David,Kameron,19],
[8,Balaji,Subbu,88],
[2,Aman,Sharma,28],
[3,Aakash,Yaadav,52]]
======================
[
[3,Aakash,Yaadav,52],
[2,Aman,Sharma,28],
[8,Balaji,Subbu,88],
[5,David,Kameron,19],
[4,James,Hedge,72],
[7,Karan,Johar,59],
[1,Lokesh,Gupta,32],
[10,Lokesh,Ramachandran,60],
[9,Vishu,Bissi,33],
[6,Yash,Chopra,25]]
======================
[Lcom.qhong.Employee;@7530d0a
[Lcom.qhong.Employee;@7530d0a

http://www.importnew.com/16436.html

http://www.cnblogs.com/figure9/archive/2014/10/24/4048421.html

https://wizardforcel.gitbooks.io/java8-tutorials/content/Java%208%20lambda%20%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5.html

http://www.codeceo.com/article/learn-java-lambda.html

http://58coding.com/article/detail/24656434620795220

http://www.ezlippi.com/blog/2015/06/java-lambda-expression.html

http://zh.lucida.me/blog/java-8-lambdas-insideout-language-features/

http://howtodoinjava.com/java-8/using-comparator-becomes-easier-with-lambda-expressions-java-8/

Java8 Lambda的更多相关文章

  1. java8 Lambda表达式的新手上车指南(1)

    背景 java9的一再推迟发布,似乎让我们恍然想起离发布java8已经过去了三年之久,java8应该算的上java语言在历代版本中变化最大的一个版本了,最大的新特性应该算得上是增加了lambda表达式 ...

  2. 【Java学习笔记之三十一】详解Java8 lambda表达式

    Java 8 发布日期是2014年3月18日,这次开创性的发布在Java社区引发了不少讨论,并让大家感到激动.特性之一便是随同发布的lambda表达式,它将允许我们将行为传到函数里.在Java 8之前 ...

  3. java8 Lambda表达式的新手上车指南(1)--基础语法和函数式接口

    背景 java9的一再推迟发布,似乎让我们恍然想起离发布java8已经过去了三年之久,java8应该算的上java语言在历代版本中变化最大的一个版本了,最大的新特性应该算得上是增加了lambda表达式 ...

  4. java8 快速入门 lambda表达式 Java8 lambda表达式10个示例

    本文由 ImportNew - lemeilleur 翻译自 javarevisited.欢迎加入翻译小组.转载请见文末要求. Java 8 刚于几周前发布,日期是2014年3月18日,这次开创性的发 ...

  5. JAVA8 Lambda初体验

    import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.u ...

  6. Java8 lambda表达式10个示例

    Java 8 刚于几周前发布,日期是2014年3月18日,这次开创性的发布在Java社区引发了不少讨论,并让大家感到激动.特性之一便是随同发布的lambda表达式,它将允许我们将行为传到函数里.在Ja ...

  7. java8 Lambda表达式的10个例子(转)

    原文:http://jobar.iteye.com/blog/2023477 Java8中Lambda表达式的10个例子 例1 用Lambda表达式实现Runnable接口 Java代码 收藏代码// ...

  8. Java8 Lambda表达式详解手册及实例

    先贩卖一下焦虑,Java8发于2014年3月18日,距离现在已经快6年了,如果你对Java8的新特性还没有应用,甚至还一无所知,那你真得关注公众号"程序新视界",好好系列的学习一下 ...

  9. Java8 lambda表达式10个示例<转>

    例1.用lambda表达式实现Runnable 我开始使用Java 8时,首先做的就是使用lambda表达式替换匿名类,而实现Runnable接口是匿名类的最好示例.看一下Java 8之前的runna ...

  10. java8 lambda表达式应用

    1.用lambda表达式实现Runnable非常简单// Java 8之前: new Thread(new Runnable() { @Override public void run() { Sys ...

随机推荐

  1. LeetCode——Peeking Iterator

    Description: Given an Iterator class interface with methods: next() and hasNext(), design and implem ...

  2. 微信Tinker的一切都在这里,包括源码(一)

    版权声明:本文由张绍文原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/101 来源:腾云阁 https://www.qclo ...

  3. 标准web浏览器的组件

    浏览器基本上包括如下几个组件 1.HTML.XML.CSS.JavsScript解析器 2.Layout 3.文字和图形渲染 4.图像解码 5.GPU交互 6.网络访问 7.硬件加速

  4. @font-face 字体

    一.@font-face是CSS3中的一个模块,把自己定义的Web字体嵌入到你网页中 @font-face的语法规则 @font-face { font-family: <YourWebFont ...

  5. Linux系统下JDK安装配置(转载)

    转载出处:http://www.cnblogs.com/xuliangxing/p/7066913.html 本文主要介绍的是如何是Linux环境下安装JDK的,因为Linux环境下,很多时候也离不开 ...

  6. Python 之定时器

    #引入库 threading import threading #定义函数 def fun_timer(): print('hello timer')   #打印输出 global timer  #定 ...

  7. CentOS7安装步骤详解

    准备环境 1.虚拟机  VMware Workstation 2.Centos7-64位安装包  ( CentOS-6.7-x86_64-bin-DVD1.iso ) 开始安装   进入安装初始化界面 ...

  8. nginx 认证访问web

    htpasswd -c /usr/local/nginx/passwd.db huo nginx .conf文件添加↓:

  9. .NET中将中文符号转换成英文符号

    public static string ConvertToEn(string text) { const string s1 = ".:,?!.“”‘’"; const stri ...

  10. sosi-statistics

    set echo offset scan onset lines 150set pages 66set verify offset feedback offset termout offcolumn ...