java 8 lambda_Java 8 Lambda演练

java 8 lambda

在工作中,我进行了有关Java 8项目lambda的演示,当然还提供了一些简单的代码来说明一些要点。 Java 8的总体原因是:

  • 更简洁的代码(适用于只有一种方法和集合的类)。 “我们希望代码阅读者在到达lambda表达式的“实质”之前,必须尽可能少地使用语法。” – Brian Goetz(http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-4.html)
  • 能够传递功能,而不仅仅是数据
  • 更好地支持多核处理

所有示例都可以在从此处下载的以下Java 8版本上运行:

openjdk version "1.8.0-ea"
OpenJDK Runtime Environment (build 1.8.0-ea-lambda-nightly-h3876-20130403-b84-b00)
OpenJDK 64-Bit Server VM (build 25.0-b21, mixed mode)

最简单的情况:

public class ThreadA {public static void main(String[] args) {new Thread(new Runnable() {@Overridepublic void run() {System.err.println("Hello from anonymous class");}}).start();}}
public class ThreadB {public static void main(String[] args) {new Thread(() -> {System.err.println("Hello from lambda");}).start();}}

非正式地注意语法

()|x|(x,..,z) -> expr|stmt

箭头是新的运算符。 并注意第二段代码与第一段代码相比更为简洁。

集合:

首先让我介绍一个简单的领域和一些帮助者

public class Something {private double amount;public Something(double amount) {this.amount = amount;}public double getAmount() {return amount;}public String toString() {return "Amount: " + amount;}
}public class Helper {public static List<Something> someThings() {List<Something> things = new ArrayList<>();things.add(new Something(99.9));things.add(new Something(199.9));things.add(new Something(299.9));things.add(new Something(399.9));things.add(new Something(1199.9));return things;}}public interface Doer<T> {void doSomething(T t);}

让我们对Java 7样式进行一些过滤和排序:

public class CollectionA {public static void main(String... args) {List<Something> things = Helper.someThings();System.err.println("Filter");List<Something> filtered = filter(things);System.err.println(filtered);System.err.println("Sum");double sum = sum(filtered);System.err.println(sum);}public static List<Something> filter(List<Something> things) {List<Something> filtered = new ArrayList<>();for (Something s : things) {if (s.getAmount() > 100.00) {if (s.getAmount() < 1000.00) {filtered.add(s);}}}return filtered;}public static double sum(List<Something> things) {double d = 0.0;for (Something s : things) {d += s.getAmount();}return d;}}

现在是Java 8样式– 流式传输 :

import java.util.stream.Collectors;public class CollectionB {public static void main(String... args) {List<Something> things = Helper.someThings();System.err.println("Filter lambda");List<Something> filtered = things.stream().parallel().filter( t -> t.getAmount() > 100.00 && t.getAmount() < 1000.00).collect(Collectors.toList());System.err.println(filtered);System.err.println("Sum lambda");double sum = filtered.stream().mapToDouble(t -> t.getAmount()).sum();System.err.println(sum);}}

导入java.util.function。*接口和方法参考

public class CollectionC {public static void main(String... args) {List<Something> things = Helper.someThings();System.err.println("Do something");doSomething(things, new Doer<Something>() {@Overridepublic void doSomething(Something t) {System.err.println(t);}});}public static void doSomething(List<Something> things, Doer<Something> doer) {for (Something s : things) {doer.doSomething(s);}}}

将我们的Doer界面替换为标准的Consumer界面 (以前称为Block)

import java.util.function.Consumer;public class CollectionD {public static void main(String... args) {List<Something> things = Helper.someThings();System.err.println("Do something functional interfaces");consumeSomething(things, new Consumer<Something>() {@Overridepublic void accept(Something t) {System.err.println(t);}});System.err.println("Do something functional interfaces, using lambda");consumeSomething(things, (t) -> System.err.println(t));System.err.println("Do something functional interfaces, using lambda method reference (new operator ::) ");consumeSomething(things, System.err::println);System.err.println("Do something functional interfaces, using stream");things.stream().forEach(new Consumer<Something>() {@Overridepublic void accept(Something t) {System.err.println(t);}});System.err.println("Do something functional interfaces, using stream and method reference");things.stream().forEach(System.err::println);}public static void doSomething(List<Something> things, Doer<Something> doer) {for (Something s : things) {doer.doSomething(s);}}public static void consumeSomething(List<Something> things, Consumer<Something> consumer) {for (Something s : things) {consumer.accept(s);}}}

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.stream.Collectors;public class Various {public static void main(String... args) {List<Something> things = Helper.someThings();//MapSystem.err.println(things.stream().map((Something t) -> t.getAmount()).collect(Collectors.toList()));//Reducedouble d = things.stream().reduce(new Something(0.0), (Something t, Something u) -> new Something(t.getAmount() + u.getAmount())).getAmount();System.err.println(d);//Reduce againSystem.err.println(things.stream().reduce((Something t, Something u) -> new Something(t.getAmount() + u.getAmount())).get());//Map/reduceSystem.err.println(things.stream().map((Something t) -> t.getAmount()).reduce(0.0, (x, y) -> x + y));//LazyOptional<Something> findFirst = things.stream().filter(t -> t.getAmount() > 1000).findFirst();System.err.println(findFirst.get());//Lazy no valueOptional<Something> findFirstNotThere = things.stream().filter(t -> t.getAmount() > 2000).findFirst();try {System.err.println(findFirstNotThere.get());} catch (NoSuchElementException e) {System.err.println("Optional was not null, but its value was");}//Optional one step deeperthings.stream().filter(t -> t.getAmount() > 1000).findFirst().ifPresent(t -> System.err.println("Here I am"));}}

参考: Kim Saabye Pedersen的博客博客中的JCG合作伙伴 Kim Saabye Pedersen的Java 8 lambda演练 。

翻译自: https://www.javacodegeeks.com/2013/06/java-8-lambda-walkthrough.html

java 8 lambda

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/348371.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Php的定界符有哪些了,php中定界符

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼PHP中定界符解析定界符写法如下&#xff1a;echo <<这里可以随便放些什么字符&#xff01;HTML; 实现效果&#xff1a;1.PHP定界符的作用就是按照原样&#xff0c;包括换行格式什么的&#xff0c;输出在其内部的东西&#xf…

在JDK 11中启动单文件源代码程序

JEP 330 –启动单文件源代码程序是即将发布的JDK 11&#xff08;18.9&#xff09;发行版中令人兴奋的功能之一。 此功能允许直接使用java解释器执行Java源代码。 源代码在内存中编译&#xff0c;然后由解释器执行。 限制是必须在同一文件中定义所有类。 对于刚开始学习Java并想…

oracle元字符,正则表达式元字符

[more]1、正则表达式中的元字符元字符 意思 例子说明要匹配的字符是一个特殊字符、常量或者后者引用。(后引用重复上一次的匹配) n 匹配换行符匹配( 匹配 () 匹配 )^ 匹配字符串的开头位置 如果A是字符串的第一个字符&#xff0c;^A 匹配 A$ 匹配字符串的末尾位置 如果B是字符串…

Packt和Java Code Geeks提供的$ 5 Java编程书籍!

您好极客&#xff01; 今天&#xff0c;我们为您带来一些激动人心的消息&#xff01; Java Code Geeks和Packt联手为您提供广泛的书籍库每周折扣。 对于开发人员来说&#xff0c;Java仍然是最强大的选择之一&#xff0c;它是定义企业和移动设备的语言。 本周&#xff0c;我们…

oracle12c 端口查看,Oracle12c修改端口号

Oracle12c数据库更改端口号修改端口号的整体步骤1.1、查看当前监听的状态1.2、停止监听1.3、修改监听文件的端口号1.4、修改初始化参数local_listener1.5、重启监听器1.6、修改完毕&#xff0c;使用新端口登录测试实践步骤&#xff1a;1、查看当前监听的状态[oraclelocalhost ~…

源码时代php中级项目,0526PHP班中级项目评比圆满落幕

为了充分发掘同学们开发项目的成功经验&#xff0c;全面提升学员的综合素质&#xff0c;锻炼学员的解说与问题处理能力&#xff0c;源代码教育(源码时代)重庆校区进行了PHP就业班的中级项目评比。项目评比分为演讲、质询、点评及投票评分几个环节&#xff0c;每个环节都精彩纷呈…

SpringHibernate4

1.概述 本文将重点介绍如何使用Spring设置Hibernate 4 –我们将研究如何使用Java和XML配置来使用Hibernate 4配置Spring 3。 当然&#xff0c;该过程的某些部分对于Hibernate 3文章是通用的 。 2. Maven 要将Spring Persistence依赖项添加到项目pom.xml中 &#xff0c;请参阅专…

qt linux 添加库文件路径,linux下qt使用第三方库的那些事

开发库查看工具&#xff1a;$sudo apt-get install pkg-config很多时候我们并不知道自己电脑有没有这个库&#xff0c;所以我们可以使用这个工具来查看自己有哪些工具&#xff0c;或者哪些工具没有。同时&#xff0c;qmake是对这个工具配置支持的&#xff0c;所以我们很多时候很…

xp系统上安装linux系统教程,XP系统如何安装fedora linux双系统?WinXP安装fedora linux双系统的方法...

有位朋友因为想在linux中熟悉下hadoop的配置开发环境&#xff0c;所以就开始于WinXP系统中安装fedora linux双系统&#xff0c;可是操作了很久都没成功。这该如何怎么办呢&#xff1f;接下来&#xff0c;小编就给大家介绍WinXP安装fedora linux双系统的具体方法。1.下载Fedora-…

solr 的maven_使用Maven运行Solr

solr 的maven使用Maven运行Solr Solr是一个开源搜索服务器&#xff0c;它是使用Lucene Core的索引和搜索功能构建的&#xff0c;它可以用于使用几乎任何编程语言来实现可扩展的搜索引擎。 尽管Solr具有许多优点&#xff0c;但建立一个开发环境并不是其中之一。 这篇博客文章描…

linux指定内核位置,ARM linux内核启动时几个关键地址

1. 内核启动地址ZTEXTADDR解压代码运行的开始地址。没有物理地址和虚拟地址之分&#xff0c;因为此时MMU处于关闭状态。这个地址不一定时RAM的地址&#xff0c;可以是支持读写寻址的flash等存储中介。Start address of decompressor. heres no point in talking about vi…

pae扩展内存 linux,Linux内核-内存管理-PAE(物理地址扩展)

Intel 通过在处理器上把管脚数从 32 增加到 36&#xff0c;以提高处理器的寻址能力&#xff0c;使其达到 2^3664GB&#xff0c;然而线性地址的位数仍然是 32 位&#xff0c;为此&#xff0c;需引入一种新的分页机制。从pentium pro 处理器开始&#xff0c;intel引入一种叫做 PA…

java嵌入式db_Java DB嵌入式模式

java嵌入式dbJava DB是基于Java编程语言和SQL的关系数据库管理系统。 这是Apache软件基金会的开源Derby项目的Oracle版本。 Java SE 7 SDK中包含Java DB。 Java DB有两个部署选项&#xff1a; Embedded和Network Server 。 这篇文章是关于嵌入式部署或模式的。 1.嵌入式 在嵌…

内存属于linux文件吗,linux下的/dev/shm是什么? 内存 文件系统

linux下的/dev/shm是什么&#xff1f;/dev/shm/是linux下一个目录&#xff0c;/dev/shm目录不在磁盘上&#xff0c;而是在内存里&#xff0c;因此使用linux /dev/shm/的效率非常高&#xff0c;直接写进内存。我们可以通过以下两个脚本来验证linux /dev/shm的性能&#xff1a;[r…

群晖备份linux分区,数据丢失的后悔药,群晖NAS备份方案详解

“秒速开机”——据说90%的人都是因为这句话而知道的SSD固态硬盘。相比于机械硬盘&#xff0c;SSD固态硬盘开机快、关机快、打开软件快、载入数据快、拷贝快、删除也快——既快乐、又爽快&#xff0c;更是大块人心!然而&#xff0c;SSD固态硬盘已经可以完全取代机械硬盘了吗&am…

php cdi_异步CDI事件

php cdi几天前&#xff0c;在我们的常规代码审查中&#xff0c;我的一位同事提出了一个问题&#xff0c;即如果可能&#xff0c;一次同时调用CDI观察者&#xff08;这样的方法带有参数Observes &#xff09;将发生多次&#xff1f;用于不同的事件实例。 换句话说&#xff0c;在…

tg3269c网卡驱动linux,TP-Link3269C网卡驱动官方版

TG-3269C驱动是一款能够安装于由普联发布的无线网卡驱动&#xff0c;通过此安装驱动我们手机和其他无线设备才能连接上无线网卡并进行上网&#xff0c;同时如果你的网卡经常出现断开和重连、网络不稳定等情况可以通过重新安装驱动&#xff0c;查看是否是硬件的问题&#xff0c;…

使用Spring Security,Thymeleaf和Okta保护Java应用程序的安全

永不再构建身份验证 –喜欢构建用户管理&#xff1f; 使用Okta&#xff0c;您可以在几分钟内为您的应用程序添加社交登录&#xff0c;多因素身份验证和OpenID Connect支持。 立即创建一个免费的开发者帐户。 在构建Java应用程序时&#xff0c;用户管理是至关重要的考虑因素。 …

红旗linux添加usb无线网卡,在Ubuntu 8.10中安装无线网卡RTL8187SE驱动

本人的笔记本是微星的Wind U90&#xff0c;自带的无线网卡是RTL8187SE。这款无线网卡在一般的Linux下是没有驱动的&#xff0c;微星的官方也仅仅提供在OpenSUSE下的驱动。为了在我的Ubuntu下使用这个网卡&#xff0c;只能自己动手了。还好&#xff0c;有了互联网上各位大侠和微…

java linq_LINQ和Java

java linqLINQ已经非常成功&#xff0c;但在.NET生态系统中也引起了争议。 许多人正在Java世界中寻找可比的解决方案。 为了更好地理解什么是可比的解决方案&#xff0c;让我们看一下LINQ解决的主要问题&#xff1a; 查询语言通常是具有许多关键字的声明性编程语言。 它们提供…