Java中使用带有Lambda的map()方法将对象转换为另一种类型 - 经验笔记 文章内的不要看 ,看我的

在Java 8中,我们可以使用带有lambda表达式的Stream对象的map()方法将对象转换为另一种类型。 map()方法是流对象中的中间操作,因此我们需要一种终端方法来完成流。

语法

<R>Stream <R> map(Function<? super T,? extends R> mapper)

示例

import java.util.*;
import java.util.stream.*;

public class EmployeeInformationTest {
   public static void main(String args[]) {
      List<Employee> employees = Arrays.asList(
         new Employee("Jai"),
         new Employee("Adithya"),
         new Employee("Raja"));
      List<String> names = employees.stream()
         .map(s -> s.getEmployeeName())//Lambda表达式
         .collect(Collectors.toList());
      System.out.println(names);
   }
}

//Employee类
class Employee {
   private String empName;
   private String empDesignation;
   public Employee(String empName) {
      this.empName = empName;
   }
   public String getEmployeeName() {
      return empName;
   }
   public void setEmployeeName(String empName) {
      this.empName = empName;
   }
   public String getEmployeeDesignation() {
      return empDesignation;
   }
   public void setEmployeeDesignation(String empDesignation) {
      this.empDesignation = empDesignation;
   }
}

输出结果

[Jai, Adithya, Raja]


原网址: 访问
创建于: 2022-03-01 18:44:57
目录: default
标签: 无

private InvoiceTypeMoudelXModeIsDefaultStructModule initModeFieldIsDefaultStruct() {

        InvoiceTypeMoudelXModeIsDefaultStructModule result = new InvoiceTypeMoudelXModeIsDefaultStructModule();

////        客户结算^_^customerGoods
//        List<InvoiceTypeMoudelXModeIsDefaultStructModuleMode> customerGoods;
////        积分结算^_^customerCredit
//        List<InvoiceTypeMoudelXModeIsDefaultStructModuleMode> customerCredit;
////        供应商结算^_^supplierGoods
//        List<InvoiceTypeMoudelXModeIsDefaultStructModuleMode> supplierGoods;

//        线下开票(offLine)|深圳航天信息电子发票(szhtxxElectronicInvoice)
//        客户结算(customerGoods)|积分结算(customerCredit)|供应商结算(supplierGoods)


        EnumSet.allOf(InvoiceTypeMoudelEnum.class).forEach(_module -> {

            String setFieldMethodName = StrUtil.format("set{}", StrUtil.upperFirst(_module.getKey()));

            ReflectUtil.invoke(result, setFieldMethodName, EnumSet.allOf(InvoiceTypeMoudelXModeEnum.class).stream().map(_mode ->
                    InvoiceTypeMoudelXModeIsDefaultStructModuleMode
                            .builder()
                            .modeCode(_mode.getKey())
                            .modeName(_mode.getDesc())
                            .enabled(CommonEnabledEnum.启用.getKey())
                            .isDefault(CommonIsDefaultEnum.正常.getKey())
                            .build()
            ).collect(Collectors.toList()));

        });

        log.info("initModeFieldIsDefaultStruct \n{}", JSONUtil.toJsonStr(result));

        return result;


//        return InvoiceTypeMoudelXModeIsDefaultStructModule.builder()
//                .customerGoods(customerGoods)
//                .customerCredit(customerCredit)
//                .supplierGoods(supplierGoods)
//                .build();

    }

请先后发表评论
  • 最新评论
  • 总共0条评论