Java如何从Map对象获取指定字段值并设置制造商价格代码
问题解决:从Map中正确获取制造商价格代码
你当前代码里的ipcToMFPNameMap.getClass().getName()完全是在拿Map的类名称,这肯定不对,你要做的是根据当前ItemPriceCode的priceCode作为键,从Map里取出对应的ManufacturerPriceCodes对象,再提取里面的制造商价格代码字段。
修改步骤:
- 先获取当前
ipcs的priceCode作为查询Map的key - 用key从Map中取出对应的ManufacturerPriceCodes实例
- 做非空判断避免空指针,再提取目标字段值设置给
ipcs
修改后的循环代码:
for (ItemPriceCode ipcs : itemPriceCodes) { // 获取当前ItemPriceCode的priceCode作为Map的key String priceCodeKey = ipcs.getPriceCode(); // 从Map中取出对应的ManufacturerPriceCodes对象 ManufacturerPriceCodes mpcEntry = ipcToMFPNameMap.get(priceCodeKey); // 非空判断后,设置正确的制造商价格代码(替换getManufacturerPriceCode()为你实际的字段方法名) if (mpcEntry != null) { ipcs.setManufacturerPriceCode(mpcEntry.getManufacturerPriceCode()); } else { // 可选:无匹配记录时设置默认值或跳过 ipcs.setManufacturerPriceCode(null); } }
注意:请把代码里的mpcEntry.getManufacturerPriceCode()替换为你实际需要的字段对应的getter方法,比如如果ManufacturerPriceCodes中存储目标价格代码的字段是targetPriceCode,就改成mpcEntry.getTargetPriceCode()。
内容的提问来源于stack exchange,提问作者Njena
相关产品推荐
相关产品推荐

