Java lambda功能读取文件和创建bean花费太多时间+;爪哇8

Java lambda功能读取文件和创建bean花费太多时间+;爪哇8,java,java-8,javabeans,java-7,Java,Java 8,Javabeans,Java 7,我正在尝试读取CSV文件并使用java创建bean对象。 我有一个约束条件,我们在LeadStg对象名中有一个对象列表,作为customAnswersStgs,我正在动态添加到这个文件中。 由于我使用的是JAVA 8,所以我使用的是lambda功能,但下面代码的响应非常耗时,我希望在这方面得到帮助,以尽量减少下面代码的响应时间。 有人能迅速帮上忙吗,因为这是一个瓶颈。提前谢谢 private List<LeadStg> processInputFile(String inputFil

我正在尝试读取CSV文件并使用java创建bean对象。 我有一个约束条件,我们在LeadStg对象名中有一个对象列表,作为customAnswersStgs,我正在动态添加到这个文件中。 由于我使用的是JAVA 8,所以我使用的是lambda功能,但下面代码的响应非常耗时,我希望在这方面得到帮助,以尽量减少下面代码的响应时间。 有人能迅速帮上忙吗,因为这是一个瓶颈。提前谢谢

private List<LeadStg> processInputFile(String inputFilePath) throws FileNotFoundException {
    List<LeadStgRequest> inputList = new ArrayList<>();
    List<LeadStg> outputList = new ArrayList<>();
    try {

        File inputF = new File(inputFilePath);
        InputStream inputFS = new FileInputStream(inputF);
        BufferedReader br = new BufferedReader(new InputStreamReader(inputFS, "ISO-8859-1"));
        inputList = br.lines().skip(1).map(mapToLeadStg).collect(Collectors.toList());
        br.close();
        inputFS.close();

        for (LeadStgRequest obj : inputList) {
            ModelMapper modelMapper = new ModelMapper();
            LeadStg value = modelMapper.map(obj, LeadStg.class);
            outputList.add(value);
        }

    }
    catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
    return outputList;
}

private Function<String, LeadStgRequest> mapToLeadStg = (line) -> {
    List<CustomAnswersStg> customAnswersStgs = new ArrayList<>();
    int count = 0;
    String[] p = line.split(",");
    LeadStgRequest leadStgReq = new LeadStgRequest(Integer.parseInt(p[count++]), p[count++], p[count++], p[count++], p[count++], p[count++],
            p[count++], p[count++], p[count++], p[count++], p[count++], p[count++], p[count++], p[count++], p[count++], p[count++]);
    if (header.contains(COMMENTS))
        leadStgReq.setComments(p[count++]);
    List<String> res = header.stream().filter(k -> k.startsWith(CUSTOM)).collect(Collectors.toList());
    if (!res.isEmpty()) {
        customAnswersStgs = new ArrayList<>();

        for (int i = 1; i <= res.size(); i++) {
            CustomAnswersStg cas = new CustomAnswersStg();
            cas.setQuestionNumber(i);
            cas.setAnswer(p.length > count ? p[count++] : "");
            customAnswersStgs.add(cas);
        }
        leadStgReq.setCustomAnswersStgs(customAnswersStgs);
    }
    return leadStgReq;
};
private List processInputFile(字符串inputFilePath)引发FileNotFoundException{
List inputList=新建ArrayList();
List outputList=new ArrayList();
试一试{
文件inputF=新文件(inputFilePath);
InputStream inputFS=新文件InputStream(inputF);
BufferedReader br=新的BufferedReader(新的InputStreamReader(inputFS,“ISO-8859-1”);
inputList=br.lines().skip(1).map(mapToLeadStg).collect(Collectors.toList());
br.close();
inputFS.close();
对于(LeadStgRequest对象:输入列表){
ModelMapper ModelMapper=新的ModelMapper();
LeadStg值=modelMapper.map(对象,LeadStg.class);
输出列表。添加(值);
}
}
捕获(IOE异常){
logger.error(e.getMessage(),e);
}
返回输出列表;
}
私有函数mapToLeadStg=(行)->{
List customAnswersStgs=new ArrayList();
整数计数=0;
字符串[]p=line.split(“,”);
LeadStgRequest leadStgReq=新的LeadStgRequest(Integer.parseInt(p[count++])、p[count++]、p[count++]、p[count++]、p[count++]、p[count++],
p[count++]、p[count++]、p[count++]、p[count++]、p[count++]、p[count++]、p[count++]、p[count++]、p[count++]、p[count++]、p[count++];
if(标题包含(注释))
leadStgReq.setComments(p[count++]);
List res=header.stream().filter(k->k.startsWith(CUSTOM)).collect(collector.toList());
如果(!res.isEmpty()){
customAnswersStgs=newarraylist();
对于(inti=1;i计数?p[count++]:“”);
customAnswersStgs.add(cas);
}
leadStgReq.setCustomAnswersStgs(customAnswersStgs);
}
返回leadStgReq;
};

读取文件确实需要一些时间。你的文件有多大?你的运行时间是多少?嗨@AndréStannek,我有20行文件,时间大约需要1分钟。我只有“活动Id、公司名称、名字、姓氏、标题、电子邮件、工作电话、街道、城市、州、邮政编码、国家、公司员工、公司收入、行业、资产名称、客户1、客户2、客户3、客户4”你的瓶颈到底在哪里?@AnandKumar我想IO不是你的瓶颈。尝试在循环外部实例化
ModelMapper
。您应该了解。除此之外,很容易将lambda部分重写为普通循环,因为您并没有真正使用流功能。然后你可以查一下有多少兰姆达斯是罪魁祸首…