在一群苹果中筛选出自己想要的苹果
java8之前
package com.chen.java8;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 代码传递
*
* @author jj
*
*/
public class Charpter_02 {
public static void main(String[] args) {
List<Apple> apples = new ArrayList<>();
apples.addAll(Arrays.asList(new Apple("green", 4d),new Apple("red", 4d),new Apple("green", 8d)));
List<Apple> greenApples = getGreenApples(apples);
greenApples.forEach((Apple apple)->System.out.println(apple.toString()));
}
//筛选出绿色的苹果
public static List<Apple> getGreenApples(List<Apple> apples) {
List<Apple> greenApples = new ArrayList<>();
for (Apple apple : apples) {
if(apple.getColor().equals("green")) {
greenApples.add(apple);
}
}
return greenApples;
}
}
class Apple {
private String color;
private Double weight;
public Apple(String color, Double weight) {
super();
this.color = color;
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Apple [color=" + color + ", weight=" + weight + "]";
}
}
java8
package com.chen.java8;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 代码传递
*
* @author jj
*
*/
public class Charpter_02 {
public static void main(String[] args) {
List<Apple> apples = new ArrayList<>();
apples.addAll(Arrays.asList(new Apple("green", 4d),new Apple("red", 4d),new Apple("green", 8d)));
List<Apple> greenApples = getApples(apples, Apple::isGreenApple);
greenApples.forEach((Apple apple)->System.out.println(apple.toString()));
}
//筛选出绿色的苹果
// public static List<Apple> getGreenApples(List<Apple> apples) {
// List<Apple> greenApples = new ArrayList<>();
// for (Apple apple : apples) {
// if(apple.getColor().equals("green")) {
// greenApples.add(apple);
// }
// }
// return greenApples;
// }
//筛选出符合条件的苹果
public static List<Apple> getApples(List<Apple> apples, Predicate<Apple> p){
List<Apple> rApples = new ArrayList<>();
for (Apple apple : apples) {
if(p.test(apple)) {
rApples.add(apple);
}
}
return rApples;
}
}
class Apple {
private String color;
private Double weight;
public Apple(String color, Double weight) {
super();
this.color = color;
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Apple [color=" + color + ", weight=" + weight + "]";
}
public static boolean isGreenApple(Apple apple) {
return "green".equals(apple.getColor());
}
public static boolean isWeightApple(Apple apple) {
return apple.getWeight()>5d;
}
}
interface Predicate<T> {
boolean test(T t);
}
甚至可以将筛选语句改为
// List<Apple> greenApples = getApples(apples, Apple::isGreenApple);
List<Apple> greenApples = getApples(apples, (Apple a) -> a.getColor().equals("red"));