前言

  目前我们知道java的版本已经发布到12了,之前的项目用的是JDK1.7,听说JDK1.8的改动相对来说大一些,因此抽空学学JDK1.8的一些新特性。本人也是通过阅读Java8实战这本书做一些小的总结,方便以后巩固,同时也为想学习Java1.8的人提供一些思路望大家多多包涵,有不对的地方请提出了我们一起学习。想要Java8实战这本书电子版的小伙伴可以留言告我可以发给你呦!

 演变过程

  这里我们通过一个简单的故事来引出神奇的Lambda。这里首先有一个苹果的实例,其包含简单的属性:颜色和重量。

 public class Apple {
private int weight = 0;
private String color = ""; public Apple(int weight, String color){
this.weight = weight;
this.color = color;
}
// get/set/toString
}

  首先我们要获取红色的苹果(因为红色的好吃嘛),这时候我们会定义一个简单的方法来进行过滤获取到红色的苹果。

     /**
* 筛选红苹果
* @param inventory
* @return
*/
public static List<Apple> filterRedApples(List<Apple> inventory){
List<Apple> result = new ArrayList<>(); for (Apple apple: inventory){
if ("red".equals(apple.getColor())) {
result.add(apple);
}
} return result;
}

  后来由于苹果收购商有来了,我不管什么苹果,只要重量大于150g的统统给我装上车!好办啦!我们再加个方法来进行过滤就可以了。

    /**
* 筛选重量大于150的苹果
* @param inventory
* @return
*/
public static List<Apple> filterHeavyApples(List<Apple> inventory){
List<Apple> result = new ArrayList<>(); for (Apple apple: inventory){
if (apple.getWeight() > 150) {
result.add(apple);
}
} return result;
}

  过了几天水果商又来了,我们现在想要绿色的苹果。这时候你可能想到是加一个方法 filterGreenApples 专门过滤绿色苹果,然而,如果要筛选多种颜色呢?黄色?暗红色?难道我们要每个新加一个方法吗?当然不是!我们最开始想到的是吧颜色作为参数传进去,试着将其抽象化。

     public static List<Apple> filterApplesByColor(List<Apple> inventory,
String color) {
List<Apple> result = new ArrayList<Apple>(); for (Apple apple: inventory){
if ( apple.getColor().equals(color) ) {
result.add(apple);
}
} return result;
}

  同时我们可能想到将获取重量的方法也进行优化

     public static List<Apple> filterApplesByWeight(List<Apple> inventory,
int weight) {
List<Apple> result = new ArrayList<Apple>(); for (Apple apple : inventory){
if ( apple.getWeight() > weight ){
result.add(apple);
}
} return result;
}

  这时候就很不错了,可(cao)爱(dan)的水果商又来了让我们提供一种方式来区分不同的属性进行过滤。OK! 我们进行优化!

     public static List<Apple> filterApples(List<Apple> inventory, String color,
int weight, boolean flag) {
List<Apple> result = new ArrayList<Apple>(); for (Apple apple: inventory){
if ( (flag && apple.getColor().equals(color)) ||
(!flag && apple.getWeight() > weight) ){
result.add(apple);
}
} return result;
}

  你会发现这个方法中的flag很难让人读懂。而且如果水果商想要再根据不同的属性进行过滤呢?比如产地、形状,那么这个方案就不能满足我们的需求了。

  那么我们如何应对这种多变的需求呢?让我们来定义一个接口对苹果过滤器进行建模。

 public interface AppleFilter {
boolean test(Apple apple);
}

  我们定义一个接口对苹果进行过滤,你可以定义多个实现类来选择不同的标准。

 public class AppleGreenColorFiler implements AppleFilter {
@Override
public boolean test(Apple apple) {
return "green".equals(apple.getColor());
}
} public class AppleHeavyWeightFilter implements AppleFilter {
@Override
public boolean test(Apple apple){
return apple.getWeight() > 150;
}
}

  这种实现方式类似与策略模式,我们可以将这种行为的实现作为一个参数传递进去。

     public static  List<Apple> filterApples(List<Apple> inventory, AppleFilter appleFilter) {
List<Apple> result = new ArrayList<Apple>(); for (Apple apple : inventory) {
if(appleFilter.test(apple)) {
result.add(apple);
}
} return result;
}

  这时候我们会发现我们的代码灵活很多了,可以应对水果商的各种需求了。而过滤的主要业务我们是在其实现类中进行定义的。我们甚至可以同时获取重量大于150并且是红色的苹果。

 public class AppleRedAndHeavyFilter implements AppleFilter {
@Override
public boolean test(Apple apple) {
return "red".equals(apple.getColor()) && apple.getWeight() > 150;
}
}

  这时候我们发现我们可以使用匿名内部类做代码的优化。并不需要每次都创建其实现类来完成。

 List<Apple> redApples = filterApples(inventory, new AppleFilter() {
@Override
public boolean test(Apple apple) {
return "red".equals(apple.getColor());
}
});

  那么在Java8中我们怎么做呢?看好啦!

 List<Apple> redApples = filterApples(inventory, (Apple apple) -> "red".equals(apple.getColor()));

  这样的代码即简洁又容易读懂。

其他例子

  其实这样的写法可以针对很多地方,例如我们在使用Comparator对集合中的数据进行排序的时候,再比如使用Runnable创建线程的时候。这里就不再举例了,希望大家自己在下面试试这几种方式。

最新文章

  1. Unique Paths II
  2. SmartUpload实现文件上传时file和表单文本同时提交的问题
  3. git 本地库提交至远程服务器
  4. Django中如何查找模板
  5. [你必须知道的.NET] 第八回:品味类型---值类型与引用类型(上)-内存有理
  6. Docker容器中运行ASP.NET Core
  7. oracle表空间查询维护命令大全之二(undo表空间)
  8. DOM事件简介
  9. input响应慢问题解决办法
  10. keepalived(nat)+ftp+http
  11. 181102 Windows下安装kivy(用python写APP)
  12. BZOJ3237 AHOI2013连通图(线段树分治+并查集)
  13. mysql面试题分组并合并列
  14. ELK菜鸟手记 (二) - 高级配置之多应用索引过滤
  15. mysql数据库导出CSV乱码问题
  16. 《Android开发艺术探索》图书勘误
  17. oracle_jdbc_Query
  18. 转换和删除重复命令tr
  19. 2.4 GO Interface
  20. Codeforces Gym101502 F.Building Numbers-前缀和

热门文章

  1. 《SLAM for Dummies》中文版《SLAM初学者教程》
  2. Visual C++中的ADO编程
  3. 安装Tomcat时 ,设置JAVA_HOME和JRE_HOME
  4. jersey学习笔记
  5. Spring框架事务支持模型的优势
  6. 用C#开发的双色球走势图(原创)值得园友拥有
  7. 实现EventHandler的监测
  8. VS2012安装ClaudiaIDE失败
  9. MongoDB下载及安装
  10. [LeetCode] Minimum Number of K Consecutive Bit Flips 连续K位翻转的最小次数