使用java流将品牌名称和产品的SimpleEntry列表转换为品牌名称到产品列表的映射

时间:2021-05-27 15:45:18

标签: java java-stream

我希望将 SimpleEntry<String, Product> 列表转换为 Map<String, List<Product>。字符串是brandName,每个brandName 都有产品。所以,我想将 List<SimpleEntry<> 转换为 Map<String, List<Product>,在地图中,我得到一个品牌名称的产品列表。

目前,我正在使用以下代码,但我认为这也可以通过流来完成

//List<AbstractMap.SimpleEntry<String, Product>> listOfSimpleEntries = new ArrayList<>() is a list of simple Entries 
//e.g. 
//nike, productA 
//adidas, productB,
//nike, productC

Map<String, List<Product>> brandToProductsMap = new HashMap<>();
for (AbstractMap.SimpleEntry<String, Product> simpleEntry : listOfSimpleEntries) {
    if (!brandToProductsMap.containsKey(simpleEntry.getKey())) {
        brandToProductsMap.put(simpleEntry.getKey(), new ArrayList<>());
    }
    brandToProductsMap.get(simpleEntry.getKey()).add(simpleEntry.getValue());
}

我试图用下面的流替换上面的代码,但我得到 java.util.ArrayList 无法转换为产品

Map<String, List<Product>> brandToProductsMap = listOfSimpleEntries.stream().collect(Collectors.groupingBy(
          w -> w.getKey(), Collectors.mapping(x -> x.getValue(), Collectors.toList())));

有人可以指出我在这里缺少什么吗?谢谢。

编辑 2 我编写的代码作为一个单独的程序工作,但不是项目的一部分。我发现在目标文件夹中,代码编译如下,看起来不对。

        Map<String, List<Product>> brandToProductsMap = (Map)listOfSimpleEntries.stream().collect(Collectors.groupingBy((w) -> {
            return (String)w.getKey();
        }, Collectors.mapping((x) -> {
            return (Product)x.getValue();
        }, Collectors.toList())));
        Iterator var5 = brandToProductsMap .entrySet().iterator();

2 个答案:

答案 0 :(得分:1)

你可以这样做:

List<AbstractMap.SimpleEntry<String, Product>> entries = Arrays.asList(
        new AbstractMap.SimpleEntry<>("Nike", new Product("ProductA")),
        new AbstractMap.SimpleEntry<>("Adidas", new Product("ProductB")),
        new AbstractMap.SimpleEntry<>("Nike", new Product("ProductC"))
);

Map<String, List<Product>> brandNameToProduct = entries.stream()
        .collect(Collectors.groupingBy(e -> e.getKey(), 
                Collectors.mapping(e -> e.getValue(), Collectors.toList())));

System.out.println(brandNameToProduct);

输出:

{Nike=[Product(name=ProductA), Product(name=ProductC)], Adidas=[Product(name=ProductB)]}

编辑:

我想你的做法和我完全一样,所以也许你的 IDE 疯了。

答案 1 :(得分:1)

希望我下面的代码片段能有所帮助:

public static void main(String[] args) {
        List<SimpleEntry<String, Product>> list = new ArrayList<SimpleEntry<String, Product>>();
        list.add(SimpleEntry.of("electronic", new Product(1, "SmartPhone")));
        list.add(SimpleEntry.of("electronic", new Product(2, "Laptop")));
        list.add(SimpleEntry.of("office", new Product(3, "Book")));
        list.add(SimpleEntry.of("office", new Product(4, "Pencil")));
        list.add(SimpleEntry.of("office", new Product(5, "Notes")));
        list.add(SimpleEntry.of("garden", new Product(6, "Rosesmany")));
        list.add(SimpleEntry.of("garden", new Product(7, "Soil Mix")));
        list.add(SimpleEntry.of("garden", new Product(8, "Pressure Sprayer")));
        
        System.out.println("Before:");
        list.stream().forEach(System.out::println);
        
        Map<String, List<Product>> map = list.stream().collect(
                    groupingBy(entry -> entry.getKey(), mapping(entry -> entry.getEntry(), toList())) 
                );
        
        System.out.println("After:");
        map.forEach((k, v) -> System.out.println("Key : " + k + ", Value : " + v));
}

简单入口

public class SimpleEntry<K, V> {
    private K key;
    private V entry;
        
    static public <K, V> SimpleEntry<K, V> of (K key, V entry)
    {
        SimpleEntry<K, V> sEntry = new SimpleEntry<>();
        sEntry.setKey(key);
        sEntry.setEntry(entry);
        return sEntry;
    }
    //getters/setters...
}

我写了它,它对我有用:

Before:
electronic - Product(1, SmartPhone)
electronic - Product(2, Laptop)
office - Product(3, Book)
office - Product(4, Pencil)
office - Product(5, Notes)
garden - Product(6, Rosesmany)
garden - Product(7, Soil Mix)
garden - Product(8, Pressure Sprayer)
After:
Key : electronic, Value : [Product(1, SmartPhone), Product(2, Laptop)]
Key : garden, Value : [Product(6, Rosesmany), Product(7, Soil Mix), Product(8, Pressure Sprayer)]
Key : office, Value : [Product(3, Book), Product(4, Pencil), Product(5, Notes)]
相关问题