标签:null tco lin except session arraylist 分享 图片 list
例:有数据表:express
对list集合:通过code获取name(orderController)
方式1:
(先查出数据表express中所有的对象List<Express>,在List<ExpressStatus>的遍历中,通过获取code,再获取name值,循环较多,i*j次)
controller:
List<Express> nameList = null;
try {
nameList = expressService.getAllName();
} catch (Exception e) {
nameList = null;
}
// create List<ExpressStatus> object
List<ExpressStatus> list = null;
try {
list = expressStatusService.getByAllNo(orderId);
} catch (Exception e) {
return new APIResult(GlobalConstantsUtil.GENERAL_ERROR, "获取物流数据失败");
}
if (list == null) {
return new APIResult(GlobalConstantsUtil.GENERAL_ERROR, "获取物流数据失败");
}
// create HashMap and add data
Map<String, String> expMap = new HashMap<String, String>();
if (nameList == null) {
nameList = new ArrayList<Express>();
}
for (int i = 0; i < nameList.size(); i++) {
expMap.put(nameList.get(i).getCode(), nameList.get(i).getName());
}
for (int i = 0; i < list.size(); i++) {
ExpressStatus expressStatus = list.get(i);// achieve ExpressStatus object
String company = expressStatus.getCom();// achieve company
String eCompanyName = null;
eCompanyName = expMap.get(company);// achieve eCompanyName from Map
}
使用Map,通过键值对,获得键:code,使用方法:map.get(key)获取值,可直接获取name,效率高。
标签:null tco lin except session arraylist 分享 图片 list
原文地址:https://www.cnblogs.com/qqzhulu/p/10339937.html