- 浏览: 1089689 次
文章分类
- 全部博客 (379)
- S2SH (16)
- stuts2 (0)
- java语言 (81)
- JSP (17)
- <html>元素 (11)
- javaweb (4)
- web容器 (3)
- ext (23)
- javaScript (48)
- ant (1)
- liferay (1)
- sql (9)
- css (42)
- 浏览器设置 (3)
- office_world (1)
- eclipse (4)
- 其它 (28)
- 操作系统 (5)
- android (6)
- Struts2 (11)
- RegEx (3)
- mysql (5)
- BigDATA (1)
- Node.js (1)
- Algorithm (10)
- Apache Spark (1)
- 数据库 (5)
- linux (2)
- git (1)
- Adobe (3)
- java语言,WebSocket (1)
- Maven (3)
- SHELL (1)
- XML (2)
- 数学 (2)
- Python (2)
- Java_mysql (1)
- ReactJS (6)
- 养生 (4)
- Docker (1)
- Protocols (3)
- java8 (2)
- 书籍 (1)
- Gradle (2)
- AngularJS (5)
- SpringMVC (2)
- SOAP (1)
- BootstrapCSS (1)
- HTTP协议 (1)
- OAuth2 (1)
最新评论
-
Lixh1986:
Java并发编程:自己动手写一把可重入锁https://blo ...
Java之多线程之Lock与Condition -
Lixh1986:
http://win.51apps.com.cn/https: ...
temp -
ztwsl:
不错,支持很好
HttpServletRequest和ServletRequest的区别 -
guodongkai:
谢谢您能将知识精华汇编总结,让初学者们从原理中学会和提高。
javaScript之function定义 -
kangwen23:
谢谢了,顶顶
struts2中的ValueStack学习
java8之新特性介绍(java 8 new features)
一、可以在接口中写方法体
在接口中增加新方法时,可以带方法体。
带来的好处:
当新接口发布时,以前实现该接口的类,无需改动。
即:无需实现新添加的方法。
语法:使用 default 关键字
注意:
1、实现多个接口时,接口中有重名的方法体的实现。
如果同时实现两个具有相同方法名且实现了该方法的接口,
需要提供自己对该方法的实现。才可以解决冲突。
sayHello() 重复,类 C 需要实现自己的 sayHello()
2、类方法的 优先级 高于 接口方法
接口 A 和 类 B 中都定义了方法 sayHello() 的实现,
类 C 优先使用 类 B 的方法。
3、接口中不可以重写 java.lang.Object 里面的方法
4、可以在接口中定义 static 方法
二、Functional Programming VS. Object Oriented Programming
Functional Programming with Java 8
https://www.youtube.com/watch?v=LcfzV38YDu4
面向函数的编程(Functional Programming)是指对于只有一个方法的接口可以使用 Lambda 表达式实现。
只有一个方法的接口很好的一个例子就是: java.lang.Comparable 接口。
它只有一个方法: compareTo(Object obj)
三、Lambda Expression ( -> 斜着看 )
Lambda Expression VS. Anonymous inner class
对于只有一个方法的接口的实现类,写实现类时,只需提供( 参数 + Lambda + 方法体 )即可。
说明:
为接口引入了一个新的注解:@FunctionalInterface
表示该接口为函数式接口,只允许有一个方法。否则编译器报错。
四、为 java.lang.Iterable 接口增加了 forEach() 方法
NOTE:
1、java 8 之前, java.lang.Iterable 接口只有一个方法:
java.util.Iterator<T> iterator()
2、java.util.Collection 接口,继承了 java.lang.Iterable 接口。
五、Streaming API
Java collections got a new package java.util.Stream.
classes in this package provide a stream API.
And supports functional-style operations on streams of elements.
Stream API enables bulk operations like sequential or parallel map-readuce on Collections.
-
So, if you know, in this world, it's an information age, we have a concept of "Big Data", "Haddoop", we have to process huge amount of data.
1) In stream we have two types of methods:
1. Intermediate method. Like: filter(), map()
lazy, it can't give result immediately until you call a terminate method.
2. Terminate method. Like: findFirst(), forEach()
Example Given:
2) Stream is once used, it can't be reused:
--
中间操作
该操作会保持 stream 处于中间状态,允许做进一步的操作。它返回的还是的 Stream,允许更多的链式操作。常见的中间操作有:
- filter():对元素进行过滤;
- sorted():对元素排序;
- map():元素的映射;
- distinct():去除重复元素;
- subStream():获取子 Stream 等。
例如,下面是对一个字符串集合进行过滤,返回以“s”开头的字符串集合,并将该集合依次打印出来:
这里的 filter(...) 就是一个中间操作,该中间操作可以链式地应用其他 Stream 操作。
终止操作
该操作必须是流的最后一个操作,一旦被调用,Stream 就到了一个终止状态,而且不能再使用了。常见的终止操作有:
- forEach():对每个元素做处理;
- toArray():把元素导出到数组;
- findFirst():返回第一个匹配的元素;
- anyMatch():是否有匹配的元素等。
例如,下面是对一个字符串集合进行过滤,返回以“s”开头的字符串集合,并将该集合依次打印出来:
这里的 forEach(...) 就是一个终止操作,该操作之后不能再链式的添加其他操作了。
-
转载请注明
原文出处:http://lixh1986.iteye.com/blog/2329376
引用:
http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html
Java 8 new features with Example
https://www.youtube.com/playlist?list=PLsyeobzWxl7qbvNnJKjYbkTLn2w3eRy1Q
Java 8 Stream API Features Lambda Expression
https://www.youtube.com/playlist?list=PLsyeobzWxl7otduRddQWYTQezVul0xIX6
http://programtalk.com/java/java-8-new-features/
更多:
Java 9 新特性
http://programtalk.com/java/java-9-new-features/
-
一、可以在接口中写方法体
在接口中增加新方法时,可以带方法体。
带来的好处:
当新接口发布时,以前实现该接口的类,无需改动。
即:无需实现新添加的方法。
语法:使用 default 关键字
interface A{ void show(); default void sayHello(){ System.out.println("Hello, World!"); } }
注意:
1、实现多个接口时,接口中有重名的方法体的实现。
如果同时实现两个具有相同方法名且实现了该方法的接口,
需要提供自己对该方法的实现。才可以解决冲突。
sayHello() 重复,类 C 需要实现自己的 sayHello()
interface A{ void showA(); default void sayHello(){ System.out.println("Hello, A!"); } } interface B{ void showB(); default void sayHello(){ System.out.println("Hello, B!"); } } class C implements A,B{ public void sayHello(){ System.out.println("Hello, C!"); } public static void main(String[] args){ C c = new C(); c.sayHello(); // Hello, C! } }
2、类方法的 优先级 高于 接口方法
接口 A 和 类 B 中都定义了方法 sayHello() 的实现,
类 C 优先使用 类 B 的方法。
interface A{ void showA(); default void sayHello(){ System.out.println("Hello, A!"); } } class B{ public void sayHello(){ System.out.println("Hello, B!"); } } class C extends B implements A{ public static void main(String[] args){ C c = new C(); c.sayHello(); // Hello, B! } }
3、接口中不可以重写 java.lang.Object 里面的方法
interface A { // can't define a equals method in interface. default public boolean equals(){ } }
4、可以在接口中定义 static 方法
interface A{ void showA(); static void sayHello(){ System.out.println("Hello, A!"); } } class C { public static void main(String[] args){ A.sayHello(); // Hello, A! } }
二、Functional Programming VS. Object Oriented Programming
Functional Programming with Java 8
https://www.youtube.com/watch?v=LcfzV38YDu4
面向函数的编程(Functional Programming)是指对于只有一个方法的接口可以使用 Lambda 表达式实现。
只有一个方法的接口很好的一个例子就是: java.lang.Comparable 接口。
它只有一个方法: compareTo(Object obj)
三、Lambda Expression ( -> 斜着看 )
Lambda Expression VS. Anonymous inner class
对于只有一个方法的接口的实现类,写实现类时,只需提供( 参数 + Lambda + 方法体 )即可。
interface A{ void show(); } public class LambdaDemo{ public static void main(String[] args){ A obj; // old obj = new A(){ public void show(){ System.out.println("Hello"); } } // java 8 obj = () -> { System.out.println("Multiple"); System.out.println("Lines ... "); } // or obj = () -> System.out.println("Single line."); } }
说明:
为接口引入了一个新的注解:@FunctionalInterface
表示该接口为函数式接口,只允许有一个方法。否则编译器报错。
// 只允许有一个方法。否则编译器报错。 @FunctionalInterface public interface Runnable { public abstract void run(); }
四、为 java.lang.Iterable 接口增加了 forEach() 方法
NOTE:
1、java 8 之前, java.lang.Iterable 接口只有一个方法:
java.util.Iterator<T> iterator()
2、java.util.Collection 接口,继承了 java.lang.Iterable 接口。
import java.util.Arrays; import java.util.List; import java.util.function.Consumer; class IConsumer implements Consumer<Integer>{ @Override public void accept(Integer t) { System.out.println(t); } } public class Java8ForEach { public static void main(String[] args) { List<Integer> list =Arrays.asList(1,2,3,5,6); // normal loop for(Integer i : list){ System.out.println(i); } // Java 8 forEach - normal Consumer<Integer> action = new IConsumer(); list.forEach(action); // Java 8 forEach - use lambda expressions. // see how we do it in one liner list.forEach(each -> System.out.println(each)); } }
五、Streaming API
Java collections got a new package java.util.Stream.
classes in this package provide a stream API.
And supports functional-style operations on streams of elements.
Stream API enables bulk operations like sequential or parallel map-readuce on Collections.
//Java 7 or earlier: public List listFilter(List<Integer> bigList){ List<Integer> filteredList = new ArrayList<>(); for (Integer p : bigList) { if (p > 40) { filteredList.add(p); } } return filteredList; }
//Java 8: public List listFilter(List<integer> bigList){ return bigList .stream() .filter(p -> p > 40) .collect(Collectors.toList()); }
-
So, if you know, in this world, it's an information age, we have a concept of "Big Data", "Haddoop", we have to process huge amount of data.
1) In stream we have two types of methods:
1. Intermediate method. Like: filter(), map()
lazy, it can't give result immediately until you call a terminate method.
2. Terminate method. Like: findFirst(), forEach()
Example Given:
List<Integer> values = Arrays.asList(4,5,6,7,8); values.stream().filter(i->{ Sysout.out.println("Hello"); return true; });
2) Stream is once used, it can't be reused:
Stream<Integer> s = values.stream(); s.forEach(Sysout.out::println); // works s.forEach(Sysout.out::println); // throws Exception
--
中间操作
该操作会保持 stream 处于中间状态,允许做进一步的操作。它返回的还是的 Stream,允许更多的链式操作。常见的中间操作有:
- filter():对元素进行过滤;
- sorted():对元素排序;
- map():元素的映射;
- distinct():去除重复元素;
- subStream():获取子 Stream 等。
例如,下面是对一个字符串集合进行过滤,返回以“s”开头的字符串集合,并将该集合依次打印出来:
list.stream() .filter((s) -> s.startsWith("s")) .forEach(System.out::println);
这里的 filter(...) 就是一个中间操作,该中间操作可以链式地应用其他 Stream 操作。
终止操作
该操作必须是流的最后一个操作,一旦被调用,Stream 就到了一个终止状态,而且不能再使用了。常见的终止操作有:
- forEach():对每个元素做处理;
- toArray():把元素导出到数组;
- findFirst():返回第一个匹配的元素;
- anyMatch():是否有匹配的元素等。
例如,下面是对一个字符串集合进行过滤,返回以“s”开头的字符串集合,并将该集合依次打印出来:
list.stream() .filter((s) -> s.startsWith("s")) .forEach(System.out::println);
这里的 forEach(...) 就是一个终止操作,该操作之后不能再链式的添加其他操作了。
-
转载请注明
原文出处:http://lixh1986.iteye.com/blog/2329376
引用:
http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html
Java 8 new features with Example
https://www.youtube.com/playlist?list=PLsyeobzWxl7qbvNnJKjYbkTLn2w3eRy1Q
Java 8 Stream API Features Lambda Expression
https://www.youtube.com/playlist?list=PLsyeobzWxl7otduRddQWYTQezVul0xIX6
http://programtalk.com/java/java-8-new-features/
更多:
Java 9 新特性
http://programtalk.com/java/java-9-new-features/
-
发表评论
-
java 将文件夹所有的文件合并到指定的文件夹下
2020-06-30 19:17 971场景:将文件夹所有的文件合并到指定的文件夹下 另外:如果想效 ... -
多线程-线程池的四种创建方式
2020-04-01 18:38 403多线程-线程池的四种创建方式 https://blog.cs ... -
Java基础之:nio
2019-11-13 15:38 407一、理论讲解: 史上最强Java NIO入门:担心从入门到放弃 ... -
Java 分布式之:RPC 基本概念
2019-11-13 15:07 394转载: https://www.jianshu.com/p/ ... -
Java之 volatile 关键字原理详解
2019-11-07 15:36 435一、什么是 volatile ? ... -
POI实现excell批注背景图片(仿html浮窗显示图片)
2019-10-21 08:17 597POI实现excell批注背景图片(仿html浮窗显示图片) ... -
Java之设计模式之 Observer 观察者
2019-07-04 17:21 969观察者设计模式 Java 已经实现了该模式,并且提供了使用类 ... -
HashMap, LinkedHashMap and TreeMap
2019-03-01 11:04 622https://stackoverflow.com/a/177 ... -
Java lib 操作 excel 插入图片
2019-01-19 12:46 836https://poi.apache.org/componen ... -
数据库连接池C3P0
2018-05-29 16:50 802一、名字的由来 很多 ... -
Java8之集合(Collection)遍历 forEach()、stream()
2018-05-29 14:39 20654package java8.collections; ... -
Junit Vs main on "java.util.concurrent.Executors"
2017-11-10 16:44 726Same code with different result ... -
Java之大数据学习路线
2017-11-03 10:08 5668三个月大数据研发学习 ... -
Java中创建对象的5种方式
2017-10-26 14:21 799一、Java之5种创建对象的方式 ————————————— ... -
Log4j和Slf4j的比较
2017-06-23 12:41 1358一直搞不清 Log4j 和 SLF4j 的关系。今天才若有所 ... -
Java之Java7新特性之try资源句式
2017-04-20 14:58 5334Java之Java7新特性之try资源句式 一、【try资源 ... -
Java之 java.util.concurrent 包之ExecutorService之submit () 之 Future
2017-03-04 21:27 3768一、如何使用 ExecutorService.submit() ... -
Java之 java.util.concurrent 包之Executor与ExecutorService
2017-03-04 21:18 2636一、问题: execute() 与 submit() 的区别? ... -
JAVAEE之单用户登录
2017-02-05 11:55 1030单用户登录是系统中数据一直性的解决方案之一。 问题背景: 试 ... -
Java之多线程之线程池之线程重复使用
2017-02-04 13:33 5516一、问题背景 在使用多线程时,如果要开启一个任务,则就需要新 ...
相关推荐
JDK1.8新特性,New Features in Java SE 8
java7 new features cookbook
Java 7 New Features Cookbook.pdf
java8-new-features总结
Enhance your development skills with Java's state-of-the-art features and projects to make your applications leaner and faster... Make the best use of new Java features and libraries in your applications
Java 7 New Features Cookbook
吸引了将近两万人参会的甲骨文大会,着实让广大Java开发和数据库管理员云集一堂,甲骨文推出了掌控sun以来真正意义上的一次大的版本发布,Java SE 8将是一个里程碑式的版本,55个新特性,让人感觉好多Java的东西让人...
Java EE 8 continues to improve API and programming models needed for today's applications and adds features requested by our world-wide community. This release modernizes support for many industry ...
> You should also read my Java 11 Tutorial (including new language and API features from Java 9, 10 and 11). Welcome to my introduction to Java 8. This tutorial guides you step by step through all new...
It continues by introducing the new Java 8 syntax features, such as lambdas and function references, and some functional programming basics. From this point on, the book focuses on RxJava in depth. ...
新特性示例 大多数示例都基于《现代 Java 实战》一书 [1]。 Java Magazine [2] 中提供了更详细的选项示例。 The Java Tutorials [3] 中描述了使用 lambda 的类似“行为参数化”方法。 项目结构 领域模型图(plantuml...
Java Enterprise Edition 8, the latest version of the Java EE specification, adds several new features to the specification. Several existing Java EE APIs have seen major improvements in this version ...
Horstmann ('Core Java') concisely introduces Java 8's most valuable new features (plus a few Java 7 innovations that haven't gotten the attention they deserve). If you're an experienced Java ...
Practical JSF in Java EE 8 pdf Master the Java EE 8 and JSF (JavaServer Faces) APIs and web framework with this practical...Those new to Java EE 8 and JSF. Some prior experience with Java is recommended.
Many old Java idioms are no longer required and new features like lambda expressions will increase programmer productivity, but navigating these changes can be challenging., , Core Java® for the ...
Fully updated for the new Java SE 6 platform, this no-nonsense tutorial and reliable reference illuminates the most important language and library features with thoroughly tested real-world examples....
Creating Pop Up Window Using New Features in WebDynpro Java SAP NetWeaver