流的使用之“如何将List<String>转为Map”
流的使用之“如何将List<String>转为Map”
ProductIllegalCustom query = new ProductIllegalCustom(); query.setUnionSkus(unionSkus); Map<String, String> illegalMap = productIllegalService.getProductIllegalList(query).stream().filter(item -> StringUtils.isNotEmpty(item.getIllegalReason())).map(item -> { List<String> skus = Optional.ofNullable(CommonUtils.tokenizeToStringList(item.getSimilarSkuList())).orElse(Lists.newArrayList()); skus.add(item.getProductSku());
// List<String> SKU.流化.去重.过滤空值.map(sku -> KeyValue存储<key, value>).转为List【当前应为List<KeyValue<String,String>>】 return skus.stream().distinct().filter(StringUtils::isNotEmpty).map(sku -> new KeyValue<>(sku, item.getIllegalReason())).collect(Collectors.toList());
// 降级流化KeyValue<String,String>.转为Map<String, String> }).flatMap(Collection::stream).collect(Collectors.toMap(KeyValue::getKey, KeyValue::getValue, (key1, key2) -> key1));
KeyValue对象
public class KeyValue<K, V> { private K key; private V value; public KeyValue() { } public KeyValue(K key, V value) { this.key = key; this.value = value; } public K getKey() { return key; } public void setKey(K key) { this.key = key; } public V getValue() { return value; } public void setValue(V value) { this.value = value; } }
* 博客文章部分截图及内容来自于学习的书本及相应培训课程,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。
* 备注:王子威
* 我的网易邮箱:wzw_1314_520@163.com