java8 in action 学习笔记 一

为什么学习Java8

  • ## java8 的变化 ##
    1. 流处理
      -Java8 在java.util.stream中添加了一个Stram API; Stream 就是一系列T类型的项目。可以看做是一种迭代器。Stream API 很多方法可以链接成一个复杂的流水线;
      Java8可以透明的吧输入的不想管的部分拿到几个cpu上运行,不用自己创建Thread。
    2. 用行为参数化把代码传递给方法
      java8新增的另一个概念是通过API来传递代码的能力。
    3. 并行和共享的可变数据
  • ##java中的函数 ##
    java8新增了函数——值的一种新形式,通过它Java8可以进行多核处理器上的并行编程。
    1. 方法和Lambda作为一等公民
      Java8的第一个新功能是方法引用。比如需要筛选一个目录中的所有隐藏文件,需要编写一个方法,然后给它一个File,判断是否隐藏。
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  inventory = Arrays.asList(new Apple(80,"green"),
                new Apple(155, "green"),
                new Apple(120, "red"));
        List greenApples = filterApples(inventory,FilteringApples::isGreenApple);
        System.out.println(greenApples);
        List heavyApples = filterApples(inventory,FilteringApples::isHeavyApple);
        System.out.println(heavyApples);

        List greenApples2 = filterApples(inventory, (Apple a)-> "green".equals(a.getColor()));
        System.out.println(greenApples2);

        List heavyApples2 = filterApples(inventory, (Apple a)->a.getWeight()>150);
        System.out.println(heavyApples2);

        List weirdApples = filterApples(inventory, (Apple a) -> a.getWeight() <= 80 &&
                "green".equals(a.getColor()));
        System.out.println(weirdApples);

    }

    public static List filterGreenApple(List inventory){
        List result = new ArrayList<>();
        for (Apple apple:inventory
             ) {
            if("green".equals(apple.getColor())){
                result.add(apple);
            }

        }
        return  result;
    }
    public static List filterHeavyApples(List inventory){
        List 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 filterApples(List inventory, Predicate p) {
        List 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 + '}';
        }
    }
}

你可能感兴趣的:(java)