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 " ["+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
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/