生活随笔
收集整理的這篇文章主要介紹了
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集合中的數據,減少遍歷次數
總結
以上是生活随笔為你收集整理的Java数组操作的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。