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

 

posted @ 2022-12-31 11:01  骚哥  阅读(358)  评论(0编辑  收藏  举报