java8之新特性介绍(java 8 new features)

Java 8 new features with Example
https://www.youtube.com/playlist?list=PLsyeobzWxl7qbvNnJKjYbkTLn2w3eRy1Q


一、可以在接口中写方法体
   在接口中增加新方法时,可以带方法体。

   带来的好处:
   当新接口发布时,以前实现该接口的类,无需改动。
   即:无需实现新添加的方法。

   语法:使用 default 关键字
Java代码   收藏代码
  1. interface A{  
  2.     void show();  
  3.     default void sayHello(){  
  4.         System.out.println("Hello, World!");  
  5.     }  
  6. }  


注意:
       1、实现多个接口时,接口中有重名的方法体的实现。

          如果同时实现两个具有相同方法名且实现了该方法的接口,
          需要提供自己对该方法的实现。才可以解决冲突。

          sayHello() 重复,类 C 需要实现自己的 sayHello()
Java代码   收藏代码
  1. interface A{  
  2.     void showA();  
  3.     default void sayHello(){  
  4.         System.out.println("Hello, A!");  
  5.     }  
  6. }  
  7. interface B{  
  8.     void showB();  
  9.     default void sayHello(){  
  10.         System.out.println("Hello, B!");  
  11.     }  
  12. }  
  13.   
  14. class C implements A,B{  
  15.   
  16.     public void sayHello(){  
  17.         System.out.println("Hello, C!");  
  18.     }  
  19.   
  20.     public static void main(String[] args){  
  21.         C c = new C();  
  22.         c.sayHello(); // Hello, C!  
  23.     }  
  24. }  

     
      2、类方法的 优先级 高于 接口方法

         接口 A 和 类 B 中都定义了方法 sayHello() 的实现,
         类 C 优先使用 类 B 的方法。

Java代码   收藏代码
  1. interface A{    
  2.     void showA();    
  3.     default void sayHello(){    
  4.         System.out.println("Hello, A!");    
  5.     }    
  6. }  
  7.   
  8. class B{  
  9.     public void sayHello(){    
  10.         System.out.println("Hello, B!");    
  11.     }    
  12. }    
  13.     
  14. class C extends B implements A{   
  15.     
  16.     public static void main(String[] args){    
  17.         C c = new C();    
  18.         c.sayHello(); // Hello, B!    
  19.     }    
  20. }  


       3、接口中不可以重写 java.lang.Object 里面的方法
Java代码   收藏代码
  1. interface A {  
  2.   
  3.     // can't define a equals method in interface.  
  4.     default public boolean equals(){  
  5.          
  6.     }  
  7. }  


       4、可以在接口中定义 static 方法
Java代码   收藏代码
  1. interface A{    
  2.     void showA();    
  3.     static void sayHello(){    
  4.         System.out.println("Hello, A!");    
  5.     }    
  6. }  
  7.   
  8. class C {  
  9.     public static void main(String[] args){    
  10.         A.sayHello(); // Hello, A!  
  11.     }    
  12. }  



二、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 + 方法体 )即可。

Java代码   收藏代码
  1. interface A{  
  2.     void show();  
  3. }  
  4.   
  5. public class LambdaDemo{  
  6.     public static void main(String[] args){  
  7.         A obj;  
  8.           
  9.         // old  
  10.         obj = new A(){  
  11.             public void show(){  
  12.                 System.out.println("Hello");  
  13.             }  
  14.         }  
  15.   
  16.         // java 8  
  17.         obj = () -> {  
  18.             System.out.println("Multiple");  
  19.             System.out.println("Lines ... ");  
  20.         }  
  21.   
  22.         // or  
  23.         obj = () -> System.out.println("Single line.");  
  24.   
  25.     }  
  26. }  


说明:



四、为 java.lang.Iterable 接口增加了 forEach() 方法

NOTE:

1、java 8 之前, java.lang.Iterable 接口只有一个方法:
                java.util.Iterator<T> iterator()

2、java.util.Collection 接口,继承了 java.lang.Iterable 接口。

Java代码   收藏代码
  1. import java.util.Arrays;  
  2. import java.util.List;  
  3. import java.util.function.Consumer;  
  4.   
  5.   
  6. class IConsumer implements Consumer<Integer>{  
  7.     @Override  
  8.     public void accept(Integer t) {  
  9.         System.out.println(t);  
  10.     }  
  11. }  
  12.   
  13.   
  14. public class Java8ForEach {  
  15.    
  16.     public static void main(String[] args) {  
  17.         List<Integer> list =Arrays.asList(1,2,3,5,6);  
  18.            
  19.         // normal loop  
  20.         for(Integer i : list){  
  21.             System.out.println(i);  
  22.         }  
  23.            
  24.         // Java 8 forEach - normal  
  25.         Consumer<Integer> action = new IConsumer();  
  26.         list.forEach(action);  
  27.            
  28.         // Java 8 forEach - use lambda expressions.  
  29.         // see how we do it in one liner  
  30.         list.forEach(each -> System.out.println(each));  
  31.            
  32.     }  
  33.        
  34.   
  35. }  



五、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代码   收藏代码
  1. //Java 7 or earlier:  
  2. public List listFilter(List<Integer> bigList){  
  3.     List<Integer> filteredList = new ArrayList<>();  
  4.     for (Integer p : bigList)  {  
  5.         if (p  > 40) {  
  6.             filteredList.add(p);  
  7.         }   
  8.     }  
  9.     return filteredList;  
  10. }  

Java代码   收藏代码
  1. //Java 8:  
  2. public List listFilter(List<integer> bigList){  
  3.     return bigList  
  4.             .stream()  
  5.             .filter(p -> p > 40)  
  6.             .collect(Collectors.toList());  
  7. }  



-

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:
Java代码   收藏代码
  1. List<Integer> values = Arrays.asList(4,5,6,7,8);  
  2. values.stream().filter(i->{  
  3.     Sysout.out.println("Hello");  
  4.     return true;  
  5. });  



2) Stream is once used, it can't be reused:

Java代码   收藏代码
  1. Stream<Integer> s = values.stream();  
  2. s.forEach(Sysout.out::println);  // works  
  3. s.forEach(Sysout.out::println);  // throws Exception  






更多:

Java 9 新特性
http://programtalk.com/java/java-9-new-features/



引用:
Java 8 Stream API Features Lambda Expression
https://www.youtube.com/playlist?list=PLsyeobzWxl7otduRddQWYTQezVul0xIX6


http://programtalk.com/java/java-8-new-features/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值