最近刚开始学习java8的新特性,发现lambda真是好用。
场景设计,你有一个list集合,需要对list进行过滤取值,有哪些做法?
public class Student {
private String name;
private int age;
private int score;
//省略get set方法
}
List studentList =null;
@Before
public void befor(){
studentList = Arrays.asList(
new Student("zhangsan",15,93),
new Student("lisi",16,100),
new Student("wangwu",17,86),
new Student("zhaoliu",13,71),
new Student("yuanqi",14,65)
);
}
原始做法:
/**
* 查找成绩>70的学生
*/
@Test
public void test1(){
List dataList = new ArrayList<>();
for(Student student :studentList){
if(student.getScore()>70){
dataList.add(student);
}
}
for(Student student :dataList){
System.out.println(student);
}
}
/**
* 查找年龄>14的学生
*/
@Test
public void test2(){
List dataList = new ArrayList<>();
for(Student student :studentList){
if(student.getAge()>14){
dataList.add(student);
}
}
for(Student student :dataList){
System.out.println(student);
}
}
缺点显而易见,每次有新的过滤方法,都得写一堆重复代码。
优化一:策略模式
/**
* 优化1 策略模式
*/
public List getStudentByFilter(List studentList ,StudentFilter studentFilter){
List data = new ArrayList<>();
for(Student student:studentList){
if(studentFilter.compare(student)){
data.add(student);
}
}
return data;
}
public void printStudentList(List studentList){
for(Student student:studentList){
System.out.println(student);
}
}
public interface StudentFilter {
boolean compare(Student student);
}
public class StudentFilterByAge implements StudentFilter{
@Override
public boolean compare(Student student) {
return student.getAge()>14;
}
}
public class StudentFilterByScore implements StudentFilter{
@Override
public boolean compare(Student student) {
return student.getScore()>70;
}
}
/**
* 优化1之后 查找成绩>70的学生
*/
@Test
public void test3(){
List studentByFilter = getStudentByFilter(studentList, new StudentFilterByScore());
printStudentList(studentByFilter);
}
/**
* 优化1之后 查找年龄>14的学生
*/
@Test
public void test4(){
List studentByFilter = getStudentByFilter(studentList, new StudentFilterByAge());
printStudentList(studentByFilter);
}
使用策略模式以后重复代码减少了,但是同样有个问题,每次新增过滤方法得写一个新类。
优化二:匿名内部类
/**
* 继续优化2 采用匿名内部类查找成绩>70的学生
*/
@Test
public void test5(){
List studentByFilter = getStudentByFilter(studentList, new StudentFilter(){
@Override
public boolean compare(Student student) {
return student.getScore()>70;
}
});
printStudentList(studentByFilter);
}
/**
* 继续优化2 采用匿名内部类查找年龄>14的学生
*/
@Test
public void test6(){
List studentByFilter = getStudentByFilter(studentList, new StudentFilter(){
@Override
public boolean compare(Student student) {
return student.getAge()>14;
}
});
printStudentList(studentByFilter);
}
这种做法也是java7的常规做法,有没有更好的方式呢,这个时候lambda表达式就诞生了
优化三:lambda
/**
* 优化3 lambda 查找年龄>14的学生
*/
@Test
public void test7(){
List studentByFilter =getStudentByFilter(studentList,(e)->e.getAge()>14);
studentByFilter.forEach(System.out::println);
}
/**
* 优化3 lambda 查找成绩>70的学生
*/
@Test
public void test8(){
List studentByFilter =getStudentByFilter(studentList,(e)->e.getScore()>70);
studentByFilter.forEach(System.out::println);
}
除了lambda表达式还有别的优化方式吗,java8有StreamAPI
优化四:StreamAPI
/**
* 优化4 StreamAPI 查找年龄>14的学生
*/
@Test
public void test9(){
studentList.stream()
.filter(student -> student.getAge()>14)
.forEach(System.out::println);
}
/**
* 优化4 StreamAPI 查找成绩>70的学生
*/
@Test
public void test10(){
studentList.stream()
.filter(student -> student.getScore()>70)
.forEach(System.out::println);
}