面试频繁被问到,问烦了,学习一下。教程主要采用了汪文君的,因为以前看多线程部分也参考的他的视频,其中涉及到了很多的设计模式.也是希望通过视频,能够同事将常见设计模式练习一下.
1.引出lambda表达式
1.需求
农场主从一堆苹果种挑选绿色苹果的需求.
1.原始方式
1.定义苹果类
public class Apple {
private String color;//颜色
private long weight;//重量
//略
}
2.实现
public class AppleFilter01 {
//删选绿色苹果的方法
public static List<Apple> findGreenApples(List<Apple> appleList){
List<Apple> list= new ArrayList<>();
for (Apple apple:appleList){
if ("green".equals(apple.getColor())){
list.add(apple);
}
}
return list;
}
public static void main(String[] args) {
List<Apple>appleList= new ArrayList<>();
appleList.add(new Apple("green",100));
appleList.add(new Apple("green",120));
appleList.add(new Apple("red",150));
List<Apple> greenApples = findGreenApples(appleList);
System.out.println(greenApples);
}
}
2.策略模式*
比如说,如果挑选苹果的标准变了,我们可能又要修改这个方法findGreenApples(List<Apple> appleList)
,但是为了让我们再调用的时候,看起来…略…采用策略模式.
1.定义接口
@FunctionalInterface
public interface AppleFilter02 {
public boolean filter(Apple apple);
}
@FunctionalInterface函数式编程,表征其为一个…
2.实现类种可以定义各种需求的方法
如:
public class AppleFilter0201 implements AppleFilter02{
@Override
public boolean filter(Apple apple) {
return ("green".equals(apple.getColor()));
}
}
3.调用实现
public class AppleFilter0202 {
public static List<Apple> findGreenApple(List<Apple> appleList,AppleFilter02 appleFilter02){
List<Apple> apples= new ArrayList<>();
for (Apple apple:appleList){
if (appleFilter02.filter(apple)){
apples.add(apple);
}
}
return apples;
}
public static void main(String[] args) {
List<Apple>appleList= new ArrayList<>();
appleList.add(new Apple("green",100));
appleList.add(new Apple("green",120));
appleList.add(new Apple("red",150));
List<Apple> greenApples = findGreenApple(appleList,new AppleFilter0201());
System.out.println(greenApples);
}
}
4.匿名内部类的方式
当然也可以不单独写实现,使用匿名内部类的方式
public class AppleFilter0202 {
public static List<Apple> findGreenApple(List<Apple> appleList,AppleFilter02 appleFilter02){
List<Apple> apples= new ArrayList<>();
for (Apple apple:appleList){
if (appleFilter02.filter(apple)){
apples.add(apple);
}
}
return apples;
}
public static void main(String[] args) {
List<Apple>appleList= new ArrayList<>();
appleList.add(new Apple("green",100));
appleList.add(new Apple("green",120));
appleList.add(new Apple("red",150));
List<Apple> greenApples = findGreenApple(appleList, new AppleFilter02() {
@Override
public boolean filter(Apple apple) {
return "green".equals(apple.getColor());
}
});
System.out.println(greenApples);
}
}
2.lambda表达式
Java8的内存有了变化,其实使用lambda表达式最重要是节省了内存.当然同样情况下,也确实简化了不少代码.
public class AppleFilter0202 {
public static List<Apple> findGreenApple(List<Apple> appleList,AppleFilter02 appleFilter02){
List<Apple> apples= new ArrayList<>();
for (Apple apple:appleList){
if (appleFilter02.filter(apple)){
apples.add(apple);
}
}
return apples;
}
public static void main(String[] args) {
List<Apple>appleList= new ArrayList<>();
appleList.add(new Apple("green",100));
appleList.add(new Apple("green",120));
appleList.add(new Apple("red",150));
//lambda表达式
List<Apple> greenApples = findGreenApple(appleList, (Apple apple) -> {
return "green".equals(apple.getColor());
});
System.out.println(greenApples);
}
}