Java8 Lambda
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
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的更多相关文章
- java8 Lambda表达式的新手上车指南(1)
背景 java9的一再推迟发布,似乎让我们恍然想起离发布java8已经过去了三年之久,java8应该算的上java语言在历代版本中变化最大的一个版本了,最大的新特性应该算得上是增加了lambda表达式 ...
- 【Java学习笔记之三十一】详解Java8 lambda表达式
Java 8 发布日期是2014年3月18日,这次开创性的发布在Java社区引发了不少讨论,并让大家感到激动.特性之一便是随同发布的lambda表达式,它将允许我们将行为传到函数里.在Java 8之前 ...
- java8 Lambda表达式的新手上车指南(1)--基础语法和函数式接口
背景 java9的一再推迟发布,似乎让我们恍然想起离发布java8已经过去了三年之久,java8应该算的上java语言在历代版本中变化最大的一个版本了,最大的新特性应该算得上是增加了lambda表达式 ...
- java8 快速入门 lambda表达式 Java8 lambda表达式10个示例
本文由 ImportNew - lemeilleur 翻译自 javarevisited.欢迎加入翻译小组.转载请见文末要求. Java 8 刚于几周前发布,日期是2014年3月18日,这次开创性的发 ...
- JAVA8 Lambda初体验
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.u ...
- Java8 lambda表达式10个示例
Java 8 刚于几周前发布,日期是2014年3月18日,这次开创性的发布在Java社区引发了不少讨论,并让大家感到激动.特性之一便是随同发布的lambda表达式,它将允许我们将行为传到函数里.在Ja ...
- java8 Lambda表达式的10个例子(转)
原文:http://jobar.iteye.com/blog/2023477 Java8中Lambda表达式的10个例子 例1 用Lambda表达式实现Runnable接口 Java代码 收藏代码// ...
- Java8 Lambda表达式详解手册及实例
先贩卖一下焦虑,Java8发于2014年3月18日,距离现在已经快6年了,如果你对Java8的新特性还没有应用,甚至还一无所知,那你真得关注公众号"程序新视界",好好系列的学习一下 ...
- Java8 lambda表达式10个示例<转>
例1.用lambda表达式实现Runnable 我开始使用Java 8时,首先做的就是使用lambda表达式替换匿名类,而实现Runnable接口是匿名类的最好示例.看一下Java 8之前的runna ...
- java8 lambda表达式应用
1.用lambda表达式实现Runnable非常简单// Java 8之前: new Thread(new Runnable() { @Override public void run() { Sys ...
随机推荐
- linux主机下的Vmware Workstation配置NAT设置 端口映射-Ubuntu为例
最近折腾虚拟机,由于是在linux下进行的,而相关资料比较少,所以遇到了一些问题. 一个就是配置vmware workstation的NAT设置.因为一般来说,NAT可以共享主机的ip,从而能以主机身 ...
- Redis单机主从高可用性优化
版权声明:本文由陈龙原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/127 来源:腾云阁 https://www.qclou ...
- css选择器的性能
性能排序: 1.id选择器(#myid) 2.类选择器(.myclassname) 3.标签选择器(div,h1,p) 4.相邻选择器(h1+p) 5.子选择器(ul < li) 6.后代选择器 ...
- EUI Scroller实现自定义图片轮播 组件ScrollView
一 自定义组件如下 /** * 文 件 ScrollView.ts * 功 能: 滚动组件 * 内 容: 自定义组件,支持多张图片水平(垂直)切换滚动 * * Example: * 1. 从自定义组件 ...
- C++ XML 序列化器
http://www.cppblog.com/xlshcn/archive/2007/11/21/cppxmlserializer.html
- 【Android】Android实现自定义带文字和图片的Button
在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就是利用系统自带的Button来实现,这种方式代码量最 ...
- 树形DP求树的直径
hdu4607 Park Visit Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 洛谷P1373 小a和uim之大逃离 dp
正解:dp 解题报告: 传送门! 同样是看到列表发的题解就想着跟着做下dp的题目趴 然后发现还挺难的,,,反正我只大概想到怎么转移但是初始化什么的都不会TT 所以还是大概说下QAQ 首先可以想到设f[ ...
- Hive与ES整合
https://www.elastic.co/guide/en/elasticsearch/hadoop/current/hive.html 注:添加的第三方jar必须位于namenode下,否则依然 ...
- JAVA中传递的值还是引用的问题
public static void main(String[] args) { /*byte b[] = new byte[1024*1024*50]; System.out.println(b); ...