原来的代码:
KeyValueHelper.setMessageText(orderList, orderDetail::getlMateria, orderDetail::setSubclass, convertMap(materiaMap, materiaMap::getSubclass));
KeyValueHelper.setMessageText(rmaReturnDetailSnList, RmaReturnDetailSn::getMaterialSn, RmaReturnDetailSn::setSupplierSn, KeyValueHelper.extractKVMap(snTempVOS, SNTempVO::getSnCode, SNTempVO::getSupplierSn));
支持以上两种为map和List
就是获取各种根据code查询出来的name时批量set的方法,基本思路就是讲查询回来的的list根据code分组得到map 然后根据
key值去除map 速度比而且方便
但是由于 materiaMap::getSubclass值可能null,所以会报空指针
KeyValueHelper.setMessageText(orderList, orderDetail::getlMateria, orderDetail::setSubclass, convertMap(materiaMap, materiaMap::getSubclass));
public static <T> void setMessageText(Collection<T> objects, Function<T, ? extends Object> keyGetter, BiConsumer<T, String> valueSetter, Map<Object, String> kvs) {
Iterator var4 = objects.iterator();
while(var4.hasNext()) {
T object = var4.next();
setMessageText(object, keyGetter, valueSetter, kvs);
}
}
private static <K extends Object, V1, V2> Map<Object, V2> convertMap(Map<K, V1> original, Function<V1, V2> converter) {
Map<Object, V2> result = original.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, kv1Entry -> {
return converter.apply(kv1Entry.getValue());
}));
return result;
}
改为
private static <K extends Object, V1, V2> Map<Object, V2> convertMap(Map<K, V1> original, Function<V1, V2> converter) {
if(MapUtils.isEmpty(original)){
return Collections.emptyMap();
}
Map<Object, V2> result = new HashMap<>();;
for (Map.Entry<K, V1> kv1Entry : original.entrySet()) {
if(kv1Entry.getKey() != null && kv1Entry.getValue() !=null){
result.put(kv1Entry.getKey(), converter.apply(kv1Entry.getValue()));
}
}
return result;
}
public class KeyValueHelper { public KeyValueHelper() { } public static void main(String[] args) throws Exception { SimpleIdGenerator simpleIdGenerator = null; Range<Long> longRange = ((SimpleIdGenerator)simpleIdGenerator).nextIds(Object.class, 100L); for(long i = (Long)longRange.getLowerBound(); i < (Long)longRange.getUpperBound(); ++i) { ; } } public static <K, E> Map<K, String> extractKVMap(Collection<E> objects, Function<? super E, K> keyGetter, Function<? super E, String> valueGetter) { if (CollectionUtils.isEmpty(objects)) { return Collections.emptyMap(); } else { Map<K, String> kvs = new HashMap(); Iterator var4 = objects.iterator(); while(var4.hasNext()) { E object = var4.next(); kvs.put(keyGetter.apply(object), valueGetter.apply(object)); } return kvs; } } public static <T> void setMessageText(T object, Function<? super T, ? extends Object> keyGetter, BiConsumer<T, String> valueSetter, Map<Object, String> kvs) { Object key = keyGetter.apply(object); if (key != null) { valueSetter.accept(object, kvs.get(key)); } } public static <T> void setMessageText(Collection<T> objects, Function<T, ? extends Object> keyGetter, BiConsumer<T, String> valueSetter, Map<Object, String> kvs) { Iterator var4 = objects.iterator(); while(var4.hasNext()) { T object = var4.next(); setMessageText(object, keyGetter, valueSetter, kvs); } }
private static <K extends Object, V1, V2> Map<Object, V2> convertMap(Map<K, V1> original, Function<V1, V2> converter) {
if(MapUtils.isEmpty(original)){
return Collections.emptyMap();
}
Map<Object, V2> result = new HashMap<>();;
for (Map.Entry<K, V1> kv1Entry : original.entrySet()) {
if(kv1Entry.getKey() != null && kv1Entry.getValue() !=null){
result.put(kv1Entry.getKey(), converter.apply(kv1Entry.getValue()));
}
}
return result;
}
}