首页 > 其他分享 >流的使用之“如何将List<String>转为Map”

流的使用之“如何将List<String>转为Map”

时间:2022-12-31 11:11:51浏览次数:59  
标签:Map List value item KeyValue key 转为 public

流的使用之“如何将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;
    }
}

 

标签:Map,List,value,item,KeyValue,key,转为,public
From: https://www.cnblogs.com/saoge/p/17016319.html

相关文章