user5985236
user5985236

Reputation:

Get the key from the value of a list of hashMaps in java

I have 4 hashMaps:

         Map <String, Integer> item1 =new HashMap<String, Integer>();
         Map <String, Integer> item2 =new HashMap<String, Integer>();
         Map <String, Integer> item3 =new HashMap<String, Integer>();
         Map <String, Integer> item4 =new HashMap<String, Integer>();  

I added the 4 of them to a list:

       List<Map> Items= new ArrayList<>();
        Items.add(item1);
        Items.add(item2);
        Items.add(item3);
        Items.add(item4);

I wanted to get the key from the value using the list of hashMaps "Items" So I tried this code:

  for(int i=0; i<n; i++)
            {  
               String key= null;
               int value=v;
              for(Map.Entry entry: Items.get(i).entrySet()){
                  if(value == (entry.getValue())){
                      key = (String) entry.getKey();
                      break;
                  }
              }

But the Items.get(i).entrySet() doesn't work!

I get these errors:

-Map.Entry is a raw type. References to generic type Map<K,V>.Entry<K,V> should be parameterized
-Type mismatch: cannot convert from element type Object to Map.Entry
-Incompatible operand types int and Object

Does anyone know how to do it?

Upvotes: 2

Views: 16432

Answers (3)

Bahramdun Adil
Bahramdun Adil

Reputation: 6089

You can try this

First add the generic part <String, Integer> to the Map of List as below:

List<Map<String, Integer>> Items= new ArrayList<>();

And then:

for (Map<String, Integer> item : Items) {
    for (Map.Entry<String, Integer> entry: items.get(i).entrySet()) {
        // do what you want with the entry
    }
}

Upvotes: 1

SkyWalker
SkyWalker

Reputation: 29168

You can use it. It will work fine.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MapTest {

    public static void main(String[] args) {
        Map <String, Integer> item1 =new HashMap<String, Integer>();
        Map <String, Integer> item2 =new HashMap<String, Integer>();
        Map <String, Integer> item3 =new HashMap<String, Integer>();
        Map <String, Integer> item4 =new HashMap<String, Integer>();  

        List<Map<String, Integer>> Items= new ArrayList<Map<String, Integer>>();
        Items.add(item1);
        Items.add(item2);
        Items.add(item3);
        Items.add(item4);

        for (int i = 0; i < Items.size(); i++) {
            String key = null;
            int value = 0;
            for (Map.Entry<String, Integer> entry: Items.get(i).entrySet()) {
                if (value == (entry.getValue())) {
                    key = (String) entry.getKey();
                    break;
                }
            }
        }
    }
}

Upvotes: 0

Eric G
Eric G

Reputation: 926

First remake the List variable to include the types of the map. This will make the .entrySet() return the correct formatting of the entry Map<String,Integer>

List<Map<String, Integer>> items = new ArrayList<>();

for (int i = 0; i < items.size(); i++) {
    String key = null;
    int value = 0;
    for (Map.Entry<String, Integer> entry: items.get(i).entrySet()) {
        if (value == (entry.getValue())) {
            key = (String) entry.getKey();
            break;
        }
    }
}

Upvotes: 5

Related Questions