基础用法
1.list<String>、、list<Long>互转
String转Long
List<Long>=stringList.stream().map(Long::valueOf).collect(Collectors.toList());
Long转String
List<>=longList.stream().map(String::valueOf).collect(Collectors.toList());
2.list和array互转
转换List为数组
String[] ss = listStrings.stream().toArray(String[]::new);
String[] sss = listStrings.toArray(new String[listStrings.size()]);
也可转换类型
Long[] ss = listStrings.stream().toArray(Long[]::new);
转换数组为List
List<String> listStrings = Stream.of(arrays).collect(Collectors.toList());
List<String> listStrings = Arrays.asList(arrays);
lambda表达式的理解
有一个员工表
需求1:获取当前公司中员工年龄大于35的员工信息。
需求2:获取当前公司中员工工资大于5000的员工信息。
员工类
public class Employee {
private String name;
private Integer age;
private double salary;
//构造方法省略
//get和set,tostring省略
}
新建一个测试类添加一些数据
public class TestLambda {
List<Employee> employees = Arrays.asList(
new Employee("张三",50,9999.99),
new Employee("李四",38,4444.44),
new Employee("王五",18,6666.66),
new Employee("赵六",16,7777.77),
new Employee("田七",28,8888.88)
);
}
方式一:
//需求1:获取当前公司中员工年龄大于35的员工信息
@Test
public void test3(){
List<Employee> list = filterEmployees2(employees);
for (Employee employee : list) {
System.out.println(employee);
}
}
public List<Employee> filterEmployees(List<Employee> list){
List<Employee> emps = new ArrayList<>();
for (Employee emp : list) {
if(emp.getAge() > 35){
emps.add(emp);
}
}
return emps;
}
//需求2:获取当前公司中员工工资大于5000的员工信息
public List<Employee> filterEmployees2(List<Employee> list){
List<Employee> emps = new ArrayList<>();
for (Employee emp : list) {
if(emp.getSalary() > 5000){
emps.add(emp);
}
}
return emps;
}
总结:如果有新的需求,需要新增一个方法,并且需要修改源码,不符合编码规范。
优化方案一:策略模式
1.新建一个MyPredicate的接口
public interface MyPredicate<T> {
public boolean test(T t);
}
2.为需求1创建一个类实现MyPredicate的接口。
public class FilterEmpolyeeByAge implements MyPredicate<Employee> {
// 获取当前公司中员工年龄大于35的员工信息
@Override
public boolean test(Employee employee) {
return employee.getAge() >= 35;
}
}
3.为需求2创建一个类实现MyPredicate的接口。
public class FilterEmployeeBySalary implements MyPredicate<Employee> {
// 获取当前公司中员工工资大于5000的员工信息
@Override
public boolean test(Employee employee) {
return employee.getSalary() >= 5000;
}
}
4.测试类中的代码
@Test
public void test4(){
//需求1:获取当前公司中员工年龄大于35的员工信息
List<Employee> list = filterEmployee(this.employees, new FilterEmpolyeeByAge());
for (Employee employee : list) {
System.out.println(employee);
}
System.out.println("-------------------------------------");
//需求2:获取当前公司中员工工资大于5000的员工信息
List<Employee> list2 = filterEmployee(this.employees, new FilterEmployeeBySalary());
for (Employee employee : list2) {
System.out.println(employee);
}
}
public List<Employee> filterEmployee(List<Employee> list, MyPredicate<Employee> mp){
List<Employee> emps = new ArrayList<>() ;
for (Employee employee : list) {
if (mp.test(employee)) {
emps.add(employee);
}
}
return emps;
}
总结:新增一个需求的时候,只需要写一个类实现MyPredicate的接口,就可以直接调用即可,实现了代码的解耦。缺点:代码量还是很大。这个方法叫做策略模式。
优化方案二:匿名内部类
1.新建一个MyPredicate的接口
public interface MyPredicate<T> {
public boolean test(T t);
}
2.在测试类中的代码
public List<Employee> filterEmployee(List<Employee> list, MyPredicate<Employee> mp){
List<Employee> emps = new ArrayList<>() ;
for (Employee employee : list) {
if (mp.test(employee)) {
emps.add(employee);
}
}
return emps;
}
//获取当前公司中员工工资小于5000的员工信息
@Test
public void test5(){
List<Employee> list = filterEmployee(this.employees, new MyPredicate<Employee>() {
@Override
public boolean test(Employee employee) {
return employee.getSalary() <= 5000;
}
});
for (Employee employee : list) {
System.out.println(employee);
}
}
总结:相对于策略模式,简洁了些,不需要为每个需求在创建一个实现类,直接用匿名内部类表示即可。
优化方案三:Lambda表达式
1.新建一个MyPredicate的接口
public interface MyPredicate<T> {
public boolean test(T t);
}
2.在测试类中的代码
public List<Employee> filterEmployee(List<Employee> list, MyPredicate<Employee> mp){
List<Employee> emps = new ArrayList<>() ;
for (Employee employee : list) {
if (mp.test(employee)) {
emps.add(employee);
}
}
return emps;
}
//获取当前公司中员工工资小于5000的员工信息
@Test
public void test6(){
List<Employee> list = filterEmployee(this.employees, (e) -> e.getSalary() <= 5000);
list.forEach(System.out::println);
}
总结:相对于匿名内部类,Lambda表达式把重要的代码提取出来,简化了代码。
优化方案四:Stream API
@Test
public void test7(){
employees.stream()
.filter((e) -> e.getSalary() >= 5000)
.forEach(System.out::println);
}