java8 in action 学习笔记 一
来源:互联网 发布:淘宝个人好评率怎么看 编辑:程序博客网 时间:2024/06/05 17:24
为什么学习Java8
- ## java8 的变化 ##
- 流处理
-Java8 在java.util.stream中添加了一个Stram API; Stream 就是一系列T类型的项目。可以看做是一种迭代器。Stream API 很多方法可以链接成一个复杂的流水线;
Java8可以透明的吧输入的不想管的部分拿到几个cpu上运行,不用自己创建Thread。 - 用行为参数化把代码传递给方法
java8新增的另一个概念是通过API来传递代码的能力。 - 并行和共享的可变数据
- 流处理
- ##java中的函数 ##
java8新增了函数——值的一种新形式,通过它Java8可以进行多核处理器上的并行编程。- 方法和Lambda作为一等公民
Java8的第一个新功能是方法引用。比如需要筛选一个目录中的所有隐藏文件,需要编写一个方法,然后给它一个File,判断是否隐藏。
- 方法和Lambda作为一等公民
File[] hiddenFiles = new File(".").listFiles(new FileFilter(){ public boolean accept(File file){ return file.isHidden(); }});
//Java8 File[] hiddenFiles = new File(".").listFiles(File::isHidden);
Lambdas——匿名函数
Java8 将函数作为值进行传递,包括lambdas,比如(int x) -> x+1; 表示调用是给定参数x,就返回x+1的值。
代码实例
/** * Created by tom on 17-7-1. */public class FilteringApples { public static void main(String[] args) { List<Apple> inventory = Arrays.asList(new Apple(80,"green"), new Apple(155, "green"), new Apple(120, "red")); List<Apple> greenApples = filterApples(inventory,FilteringApples::isGreenApple); System.out.println(greenApples); List<Apple> heavyApples = filterApples(inventory,FilteringApples::isHeavyApple); System.out.println(heavyApples); List<Apple> greenApples2 = filterApples(inventory, (Apple a)-> "green".equals(a.getColor())); System.out.println(greenApples2); List<Apple> heavyApples2 = filterApples(inventory, (Apple a)->a.getWeight()>150); System.out.println(heavyApples2); List<Apple> weirdApples = filterApples(inventory, (Apple a) -> a.getWeight() <= 80 && "green".equals(a.getColor())); System.out.println(weirdApples); } public static List<Apple> filterGreenApple(List<Apple> inventory){ List<Apple> result = new ArrayList<>(); for (Apple apple:inventory ) { if("green".equals(apple.getColor())){ result.add(apple); } } return result; } 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; } public static boolean isGreenApple(Apple apple){ return "green".equals(apple.getColor()); } public static boolean isHeavyApple(Apple apple){ return apple.getWeight()>150; } public static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p) { List<Apple> result = new ArrayList<>(); for (Apple apple : inventory) { if (p.test(apple)) { result.add(apple); } } return result; } public static class Apple { private int weight = 0; private String color = ""; public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } public Apple(int weight, String color) { this.weight = weight; this.color = color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String toString() { return "Apple{" + "color='" + color + '\'' + ", weight= " + weight + '}'; } }}
阅读全文
0 0
- java8 in action 学习笔记 一
- 《Junit In Action》学习笔记(一)
- AJAX IN ACTION 学习笔记(一)
- JUnit in action学习笔记(一)
- JQuery in Action学习笔记一
- Lucene In Action 学习笔记(一)
- Liferay in action 学习笔记(一)
- spring in action 学习笔记(一)
- AJAX in Action学习笔记:一、AJAX的四个基本原则
- Spring In Action 4 学习笔记(一)Spring概览
- 《spring in action》笔记(一)
- JSF In Action 学习一
- JUnit In Action 学习笔记
- 《Junit in Action》学习笔记
- junit in action 学习笔记
- junit in action 学习笔记
- java8的新特性学习笔记一
- AJAX IN ACTION 学习笔记(二)
- 位操作-leetcode 338 Counting Bits
- 安装Python
- 解决 Communications link failure due to underlying exception
- java的4种代码块
- Activiti HelloWorld流程实现及原理
- java8 in action 学习笔记 一
- POJ 2504 Bounding box 笔记
- Android 内存分析命令
- 前摄器(Proactor):用于为异步事件多路分离和分派处理器的对象行为模式
- js对象、数字深拷贝方式(转)
- hdu 3911 线段树的区间合并
- Websphere7.0修改JVM的内存设置 http://download.csdn.net/detail/zhaolong1990ok/5268650
- python3 网络爬虫(二)利用get请求获取网页的动态加载数据
- 字符串置换