Java数组操作

系统中有一个表中,人员存放的是多个
并且,存放的不是姓名,而是code,code之间使用逗号分隔

页面,需要显示所有人员的姓名
把数组中的code,作为key放在map中,获取每一个员工姓名

@RequestMapping(method = RequestMethod.GET)
public String list(Page<SamplePreparationInfoExt> page, Model model, @RequestParam Map<String, String> param) {
    List<PersonInfoExt> personInfoList = this.personInfoExtService.selectPersonByDeptId(currentUser != null ? currentUser.getDepartmentCode() : null);
    List<SamplePreparationInfoExt> SamplePreparationInfoExtList = samplePreparationWebService.selectExtListByMap(page, param);
    for (int i = 0; i < SamplePreparationInfoExtList.size(); i++) {
        String opertor = SamplePreparationInfoExtList.get(i).getLabSampleAcceptor();
        String opertorName = "";
        if (opertor == null || opertor.equals("")) {
            SamplePreparationInfoExtList.get(i).setLabSampleAcceptor(opertorName);
        } else {
            int number = StringUtils.countMatches(opertor, ",");
            Map<String, String> nameMap = new HashMap<>();
            for (int j = 0; j < number + 1; j++) {
                String opertorCode = "";
                opertorCode = opertor.substring(j * 8 + j, (j + 1) * 8 + j);
                nameMap.put(opertorCode, "");
            }
            if (nameMap.size() == 0) {
                SamplePreparationInfoExtList.get(i).setLabSampleAcceptor(opertorName);
            } else {
                int k = 0;
                for (String key : nameMap.keySet()) {
                    // 获取名字
                    String name = getOpertorNameByCode(personInfoList, key);
                    if (k == 0) {
                        opertorName = name;
                    } else {
                        opertorName = opertorName + ',' + name;
                    }
                    k = k + 1;
                }
                SamplePreparationInfoExtList.get(i).setLabSampleAcceptor(opertorName);
            }
        }
    }
    setCommonData(model);
    page.setResult(SamplePreparationInfoExtList);
    model.addAttribute("page", page);
    return this.viewName("list");
}

// 获取化验员姓名
public String getOpertorNameByCode(List<PersonInfoExt> personInfoList, String opertorCode) {
    String opertorName = "";
    for (int i = 0; i < personInfoList.size(); i++) {
        if (opertorCode != null) {
            if (opertorCode.equals(personInfoList.get(i).getCode())) {
                opertorName = personInfoList.get(i).getName();
            }
        }
    }
    return opertorName;
}

注意
使用substring截取String,必须保证这个code的位数,必须是固定的

考虑到性能,只有list在不大的情况下,才可以使用
或者尽量过滤list集合中的数据,减少遍历次数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值