理解Java8的编程概念
Java 8对于程序员的主要好处在于它提供了更多的编程工具和概念,它们促使Java 8中开发出并行和编写更简洁通用代码的功能。
Java 8 流处理
流是Java API的新成员,它允许你以声明性方式处理数据集合,可以把它们看成遍历数据集的高级迭代器。
此外,流还可以透明的并行处理,无需写任何多线程代码。
下面是一段返回低卡路里的菜单名称的,java 7 的写法如下,
package com.common.stream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class StreamApiTest {
private static List<Dish> menu = new ArrayList<>();
static {
Dish dish0 = new Dish();
dish0.setCalories(900);
dish0.setName("sad");
Dish dish1 = new Dish();
dish1.setCalories(200);
dish1.setName("test");
menu.add(dish0);
menu.add(dish1);
}
public static void main(String[] args) {
List<Dish> lowCaloricDishes = new ArrayList<>();
for (Dish d : menu) {
if (d.getCalories() < 400) {
lowCaloricDishes.add(d);
}
}
Collections.sort(lowCaloricDishes, new Comparator<Dish>() {
public int compare(Dish d1, Dish d2) {
return Integer.compare(d1.getCalories(), d2.getCalories());
}
});
List<String> lowCaloricDishesName = new ArrayList<>();
for (Dish d : lowCaloricDishes) {
lowCaloricDishesName.add(d.getName());
}
}
}
class Dish {
private String name;
private int calories;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCalories() {
return calories;
}
public void setCalories(int calories) {
this.calories = calories;
}
}
java8的写法如下,
package com.common.stream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class StreamApiTest {
private static List<Dish> menu = new ArrayList<>();
static {
Dish dish0 = new Dish();
dish0.setCalories(900);
dish0.setName("sad");
Dish dish1 = new Dish();
dish1.setCalories(200);
dish1.setName("test");
menu.add(dish0);
menu.add(dish1);
}
public static void main(String[] args) {
//******************Java8 语法
List<String> names = menu.stream().filter((m) -> {
return m.getCalories() < 400;
}).sorted(Comparator.comparing(Dish::getCalories)).map(Dish::getName).collect(Collectors.toList());
//******************Java8 语法
names = menu.stream().filter(m -> m.getCalories() < 400).sorted(Comparator.comparing(Dish::getCalories)).map(Dish::getName).collect(Collectors.toList());
System.out.println(names);
}
}
class Dish {
private String name;
private int calories;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCalories() {
return calories;
}
public void setCalories(int calories) {
this.calories = calories;
}
}
行为参数化
behavior parameterization可以将一段代码块当做参数传给另一个方法,之后执行。这样做的好处是,方法的行为可以由传入的代码块控制。举个例子,比如有一堆苹果,需要把绿色的苹果筛选出来,可能还需要把 大于500g 的苹果筛选出来等,这些筛选条件可以表示成一段代码,传入另一个方法,完成筛选的行为。如下,定义一个方法
/**
* 使用泛型
*
* @param source
* @param predicate 表示筛选的条件
* @param <T>
* @return
*/
public <T> List<T> filter(List<T> source, Predicate<T> predicate) {
return source.stream().filter(t -> predicate.test(t)).collect(Collectors.toList());
}
我们就可以使用这个filter 方法来实现多种条件的筛选苹果了
package com.common;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class BehaviorParameterization {
/**
* 使用泛型
*
* @param source
* @param predicate 表示筛选的条件
* @param <T>
* @return
*/
public <T> List<T> filter(List<T> source, Predicate<T> predicate) {
return source.stream().filter(t -> predicate.test(t)).collect(Collectors.toList());
}
public static void main(String[] args) {
List<Apple> apples = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Apple apple;
if (i % 2 == 0) {
apple = new Apple(100 * (i + 1), "green");
} else {
apple = new Apple(100 * (i + 1), "red");
}
apples.add(apple);
}
BehaviorParameterization test = new BehaviorParameterization();
/**
* t -> t.getWeight() > 500
* t -> t.getColor().equals("red")
* 将这段代码传入另外一个方法 这就是行为参数化。
* 其实这段代码就是 lambda 表达式
*/
List<Apple> dest = test.filter(apples, t -> t.getWeight() > 500);
List<Apple> dest0 = test.filter(apples, t -> t.getColor().equals("red"));
System.out.println(dest);
System.out.println(dest0);
}
}
class Apple {
private int weight;
private String color;
public Apple(int weight, String color) {
this.weight = weight;
this.color = color;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Apple{");
sb.append("weight=").append(weight);
sb.append(", color='").append(color).append('\'');
sb.append('}');
return sb.toString();
}
}
=====END=====