没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
java 8 lambda Concise syntax – More succinct and clear than anonymous inner classes • Deficiencies with anonymous inner classes – Bulky, confusion re “this” and naming in general, no nonfinal vars, hard to optimize • Convenient for new streams library – shapes.forEach(s -> s.setColor(Color.RED)); • Similar constructs used in other languages – Callbacks, closures, map/reduce idiom • Step toward true functional programming
资源推荐
资源详情
资源评论





















© 2014 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/
Java, JSF 2, PrimeFaces, HTML5, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Lambda Expressions
in Java 8: Part 1 – Basics
Originals of Slides and Source Code for Examples:
http://www.coreservlets.com/java-8-tutorial/
© 2014 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/
Java, JSF 2, PrimeFaces, HTML5, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
For live Java-related training,
see http://courses.coreservlets.com/
or email [email protected].
Taught by lead author of Core Servlets & JSP, co-author of
Core JSF (4
th
Ed), & this tutorial. Available at public venues, or
customized versions can be held on-site at your organization.
• Courses developed and taught by Marty Hall
– JSF 2.2, PrimeFaces, servlets/JSP, Ajax, jQuery, Android development, Java 7 or 8 programming, custom mix of topics
– Courses available in any state or country. Maryland/DC area companies can also choose afternoon/evening courses.
• Courses developed and taught by coreservlets.com experts (edited by Marty)
– Spring, Hibernate/JPA, GWT, Hadoop, HTML5, RESTful Web Services
Contact [email protected] for details

Topics in This Section
• Intro
– Motivation
– Quick summary of big idea
• New option: lambdas
– Interpretation
– Most basic form
– Type inferencing
– Expression for body
– Omitting parens
– Comparing lambda approaches to alternatives
– Using effectively final variables
– @FunctionalInterface
– Method references
– The java.util.function package
4
© 2014 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/
Java, JSF 2, PrimeFaces, HTML5, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Motivation and
Overview

Many Languages Let You Pass
Functions Around
• Dynamically (and usually weakly) typed
– JavaScript, Lisp, Scheme, etc.
• Strongly typed
– Ruby, Scala, Clojure, ML, etc.
• Functional approach proven concise,
useful, and parallelizable
– JavaScript sorting
• var testStrings = ["one", "two", "three", "four"];
• testStrings.sort(function(s1, s2) {
return(s1.length - s2.length);});
• testStrings.sort(function(s1, s2) {
return(s1.charCodeAt(s1.length - 1) –
s2.charCodeAt(s2.length - 1));});
Why Lambdas in Java Now?
• Concise syntax
– More succinct and clear than anonymous inner classes
• Deficiencies with anonymous inner classes
– Bulky, confusion re “this” and naming in general, no non-
final vars, hard to optimize
• Convenient for new streams library
– shapes.forEach(s -> s.setColor(Color.RED));
• Similar constructs used in other languages
– Callbacks, closures, map/reduce idiom
• Step toward true functional programming
– Real functions and mutable local vars perhaps in future
7

Main Advantage of Lambdas:
Concise and Expressive
• Old
– button.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
blah();
}
});
• New
– button.addActionListener(e -> blah());
8
Vigorous writing is concise... This requires not that the writer make all sentences short, or avoid all details
and treat subjects only in outline, but that every word should tell. -- Strunk and White, The Elements of Style
Corollary Advantages:
Support New Way of Thinking
• Encourage functional programming
– When functional programming approach is used, many
classes of problems are easier to solve and result in code
that is clearer to read and simpler to maintain.
• Support streams
– Streams are wrappers around collections that use
lambdas, support map/reduce, use lazy evaluation, and
can be made parallel automatically by compiler.
• Cannot be made concurrent automatically
– for(Shape s: shapes) { s.setColor(Color.RED); }
• Can be automatically turned into concurrent code
– shapes.forEach(s -> s.setColor(Color.RED));
» Streams are covered in a separate tutorial section
9

© 2014 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/
Java, JSF 2, PrimeFaces, HTML5, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Lambdas:
Most Basic Form
Big Idea
• You write what looks like a function
• Arrays.sort(testStrings, (s1, s2) -> s1.length() - s2.length());
• taskList.execute(() -> downloadSomeFile());
• someButton.addActionListener(event -> handleButtonClick());
• double d = MathUtils.integrate(x -> x*x, 0, 100, 1000);
• You get an instance of a class that implements
the interface that was expected in that place
– The expected type must be an interface that has exactly one
(abstract) method
• Called “Functional Interface” or “Single Abstract Method (SAM)
Interface”
– The designation of a single ABSTRACT method is not redundant,
because in Java 8 interfaces can have concrete methods, called
“default methods”. Java 8 interfaces can also have static methods.
Both default methods and static methods are covered in later section.
11
剩余34页未读,继续阅读
资源评论

- 奔腾岁月2015-09-07不错,真是雪中送炭!

qq_16707875
- 粉丝: 0
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- COMSOL声子晶体建模:带隙与传输损耗计算方法及应用
- SQL-Server-2008数据库设计与管理03单元3创建与管理数据表(ppt文档).ppt
- 区域农产品电子商务运营策划方案.doc
- 2023年SEO电脑维修网站整体优化方案.doc
- 综合项目管理知识标准体系中的九大知识领域.doc
- 配拣型仓库控制系统“仓库任务操作系统”介绍-PPT课件.pptx
- 网络综合布线投标书模板.doc
- 机械故障诊断学钟秉林第章神经网络诊断原理.ppt
- 通信行业大数据项目市场需求.docx
- 嵌入式系统rteosμcosii的移植.pptx
- 哈希算法介绍(7页).doc
- 物联网与工业自动化的关系ppt课件.ppt
- 内蒙古准格尔旗高中数学第一章算法初步1.1.2程序框图与算法的基本逻辑结构例题课件新人教B版必修3.ppt
- 学生计算机学习心得体会900字5篇.docx
- 2023年软件评测师上午试题分析与解答.doc
- 建设项目管理作业参考答案.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
