Java 将值附加到哈希映射<;字符串,数组列表<;对象>&燃气轮机;

Java 将值附加到哈希映射<;字符串,数组列表<;对象>&燃气轮机;,java,arraylist,hashmap,Java,Arraylist,Hashmap,我试图创建一个简单的HashMap,它将有一个字符串作为键,每个键都有一个对象的ArrayList 我提出的解决方案的问题是在循环结束时,我在ArrayList中只得到一个值(对象) 我曾尝试在ApacheCommons中使用多值映射,但我的IDE表示反对,它说它已被弃用 //filter products by supplier for(String nume:SuppliersNames){ ArrayList<Order.Pr

我试图创建一个简单的HashMap,它将有一个字符串作为键,每个键都有一个对象的ArrayList

我提出的解决方案的问题是在循环结束时,我在ArrayList中只得到一个值(对象)

我曾尝试在ApacheCommons中使用多值映射,但我的IDE表示反对,它说它已被弃用

//filter products by supplier
            for(String nume:SuppliersNames){

                ArrayList<Order.Product> gol = new ArrayList<>();
                SuppliersMap.put(nume,gol);

                //we make a copy of the products collection so we can iterate it and filter elements by supplier

                ArrayList<Orders.Order.Product> interm = new ArrayList<Orders.Order.Product>(products);

                Iterator<Orders.Order.Product> iter = interm.iterator();

                while (iter.hasNext()){

                    Orders.Order.Product curent = iter.next();

                    if(curent.getSupplier()== nume){

                        SuppliersMap.get(nume).add(curent);
                    }
                }
            }
//按供应商筛选产品
用于(字符串nume:供应商名称){
ArrayList gol=新的ArrayList();
供应商映射放置(nume,gol);
//我们制作了一份产品集合的副本,以便我们可以按供应商对其进行迭代和过滤
ArrayList interm=新的ArrayList(产品);
迭代器iter=interm.Iterator();
while(iter.hasNext()){
Orders.Order.Product curent=iter.next();
if(curent.getSupplier()==nume){
供应商映射获取(nume).添加(curent);
}
}
}

如注释中所述,问题很可能是检查
curent.getSupplier()==nume
。你在这里比较身份,这可能不是你想要的。尝试将其更改为
curent.getSupplier().equals(nume)
,看看会发生什么

此外,不需要创建
产品
集合的本地副本,因为您没有在发布的代码段中修改此集合

使用Java8流,您的代码可以更改为:

for (String name : suppliersNames) {
    List<Product> gol = products.stream().filter(p -> p.getSupplier().equals(name))
            .collect(Collectors.toList());
    suppliersNames.put(name, gol);
}
for(字符串名称:供应商名称){
List gol=products.stream().filter(p->p.getSupplier().equals(name))
.collect(Collectors.toList());
供应商名称(名称,gol);
}

如注释中所述,问题很可能是检查
curent.getSupplier()==nume
。你在这里比较身份,这可能不是你想要的。尝试将其更改为
curent.getSupplier().equals(nume)
,看看会发生什么

此外,不需要创建
产品
集合的本地副本,因为您没有在发布的代码段中修改此集合

使用Java8流,您的代码可以更改为:

for (String name : suppliersNames) {
    List<Product> gol = products.stream().filter(p -> p.getSupplier().equals(name))
            .collect(Collectors.toList());
    suppliersNames.put(name, gol);
}
for(字符串名称:供应商名称){
List gol=products.stream().filter(p->p.getSupplier().equals(name))
.collect(Collectors.toList());
供应商名称(名称,gol);
}

你所说的“我的IDE抗议”是什么意思?我可能忽略了一些东西,但你不需要复制到过滤器。您可以使用streams吗?代码的问题是您在地图中的每个循环上都添加了一个新的
gol
。因此,您将替换值
ArrayList
。您应该检查
nume
键是否已经存在,如果不存在就添加它。除此之外,使用Java8流可能更简洁,问题可能是检查
curent.getSupplier()==nume
。你在这里比较身份,这可能不是你想要的。尝试将其更改为
curent.getSupplier().equals(nume)
,看看会发生什么。的类型非常好,请使用它,而不是非常过时的Apache Commons。你说的“我的IDE抗议”是什么意思?我可能忽略了一些内容,但你不需要复制到筛选器。您可以使用streams吗?代码的问题是您在地图中的每个循环上都添加了一个新的
gol
。因此,您将替换值
ArrayList
。您应该检查
nume
键是否已经存在,如果不存在就添加它。除此之外,使用Java8流可能更简洁,问题可能是检查
curent.getSupplier()==nume
。你在这里比较身份,这可能不是你想要的。尝试将其更改为
curent.getSupplier().equals(nume)
,然后看看会发生什么。的类型非常好,请使用它而不是非常过时的Apache Commons。