Java 迭代HashMap并将值放入列表

Java 迭代HashMap并将值放入列表,java,Java,我有一个HashMap,其中包含各种“Product”作为键,“Product Group ID”作为值。 我需要分离出具有相同组ID的产品,并将它们放入新的ArrayList(仅包含产品,即密钥)中。 我被困在如何才能做到这一点,任何帮助将不胜感激 大概是这样的: HashMap:(1,a)(2,b)(3,c)(4,a)(5,b) 新的ArrayList应该包含:{1,4}-此列表将被发送到另一个函数进行处理。 同样,2,5应该存储在另一个列表中并得到处理。 最后,3应该存储在一个数组列表中并

我有一个HashMap,其中包含各种“Product”作为键,“Product Group ID”作为值。 我需要分离出具有相同组ID的产品,并将它们放入新的ArrayList(仅包含产品,即密钥)中。 我被困在如何才能做到这一点,任何帮助将不胜感激

大概是这样的: HashMap:(1,a)(2,b)(3,c)(4,a)(5,b) 新的ArrayList应该包含:{1,4}-此列表将被发送到另一个函数进行处理。 同样,2,5应该存储在另一个列表中并得到处理。
最后,3应该存储在一个数组列表中并进行处理。

我认为您需要这样的东西:

private static Map<Product, Long> map = new HashMap<>();

public static void main(String[] args) {
    fillTestData();
    List<Product> products = findProductByGroup(1L);
    System.out.println(products.size());
}

private static List<Product> findProductByGroup(Long groupId){
    return map.entrySet().stream()
        .filter(entry -> entry.getValue() == groupId)
        .map(Entry::getKey)
        .collect(Collectors.toList());
}

private static void fillTestData() {
    String product = "product";
    long groupId = 0;
    for(int i = 0; i < 100; i++) {
        if(i % 2 == 0) {
            groupId = 1;
        }else {
            groupId = 2;
        }
        map.put(new Product(product+i), groupId);
    }
}
private static Map=new HashMap();
公共静态void main(字符串[]args){
fillTestData();
列表产品=findProductByGroup(1L);
System.out.println(products.size());
}
私有静态列表findProductByGroup(长组ID){
返回map.entrySet().stream()
.filter(条目->条目.getValue()==groupId)
.map(条目::getKey)
.collect(Collectors.toList());
}
私有静态void fillTestData(){
字符串product=“product”;
长groupId=0;
对于(int i=0;i<100;i++){
如果(i%2==0){
groupId=1;
}否则{
groupId=2;
}
map.put(新产品(产品+i),groupId);
}
}

另外,您必须清楚地声明您需要什么,一种方法是使用另一个
数组列表的
HashMap
,然后向其中添加值。您可以尝试以下代码:

我假设乘积是一个字符串,ID是一个整数,但您可以更改它

Map < Integer, String > sourceMap = Map.of ( 1 , "a" , 2 , "b" , 3 , "c" , 4 , "a " , 5 , "b" );

HashMap <String, ArrayList<Integer>> result = new HashMap<>();
for (Map.Entry <Integer, String> entry : sourceMap.entrySet()) {
    String value = entry.getValue();
    int key = entry.getKey();

    if (result.containsKey(value)) {
        ArrayList<Integer>temp = result.get(value);
        temp.add(key);
    } else {
        ArrayList<Integer> temp = new ArrayList<>();
        temp.add(key);
        result.put(value, temp);
    }
}
System.out.println ("result.toString():\n" + result);
MapsourceMap=Map.of(1,“a”,2,“b”,3,“c”,4,“a”,5,“b”);
HashMap结果=新建HashMap();
对于(Map.Entry:sourceMap.entrySet()){
字符串值=entry.getValue();
int key=entry.getKey();
if(结果容器(值)){
ArrayListtemp=result.get(值);
临时添加(键);
}否则{
ArrayList temp=新的ArrayList();
临时添加(键);
结果。输入(值、温度);
}
}
System.out.println(“result.toString():\n”+结果);

现在,您有了一个名为
result
HashMap
,其中包含您想要的
ArrayList
。您可以遍历以获得每个
ArrayList

我建议使用以下模式

假设您有一个简单的
HashMap
。然后你可以这样做:

Map<String, List<String>> result = map.entrySet().stream().collect(
   Collectors.groupingBy(e -> e.getValue(), 
   Collectors.mapping(e -> e.getKey(), Collectors.toList()))
)
Map result=Map.entrySet().stream().collect(
Collectors.groupingBy(e->e.getValue(),
Collectors.mapping(e->e.getKey(),Collectors.toList())
)
结果是:
{a=[1,4],b=[2,5],c=[3]}


可以通过
result.get(“a”)
等方式访问列表。

创建地图。然后迭代原始地图的条目,并填充此新地图。@adityaaraop99,如果您希望得到巴兹尔先生所说的答案,请更改标题。您的示例数据应成对重写,这是讨论地图的常规方法。按照大家所说的重写问题。我没有太多关于堆栈溢出的经验,所以如果我不清楚这个问题,请原谅。@BasilBourque,你能给我一个解决方案吗?谢谢你指出@BasilBourque。我在StackOverflow中编写代码,因此没有任何自动完成建议,但应该是
result.contains()
,而不是
result.containsKey()
。一个小错误,但我已经修复了代码。正如所见,这似乎现在起作用了。但我还没有确定正确性(问题中的示例数据令人困惑)。好得足以让我投赞成票。在我看来,与收藏家分组要干净得多对。这只是我想到的第一个解决方案。@Michael展示更“手动”的方法也很有价值。使用lambdas和收集器对新手来说有点不可思议。