5

I have a List of HashMap's which has key of type Integer and value of type Long.

List<HashMap<Integer, Long>> ListofHash = new ArrayList<HashMap<Integer, Long>>();
        for (int i = 0; i < 10; i++) {
            HashMap<Integer, Long> mMap = new HashMap<Integer, Long>();
            mMap.put(Integer.valueOf(i), Long.valueOf(100000000000L+i));
            ListofHash.add(mMap);
        }

Now, how do I retrieve the key and value from the list of HashMap?

If using Collection class is the solution, please enlight me.

Update 1:

Actually i am getting the value from the database and putting that into a HashMap

public static List<Map<Integer, Long>> populateMyHashMapFromDB()
{

List<HashMap<Integer, Long>> ListofHash = new ArrayList<HashMap<Integer, Long>>();

for (int i = 0; i < 10; i++) {
                HashMap<Integer, Long> mMap = new HashMap<Integer, Long>();
                mMap.put(Integer.valueOf(getIntFromDB(i)), Long.valueOf(getLongFromDB(i)));
                ListofHash.add(mMap);
            }
return ListofHash;


}

where getIntFromDB and getLongFromDB can retreive any int and long values respectively.

Now this is where i want to get my values that i got from DB both keys and values

public void getmyDataBaseValues()
{
List<HashMap<Integer,Long>> ListofHash=populateMyHashMapFromDB();

// This is where i got confused

}

ANSWER This is why Peter Lawrey suggested

public class HashMaps {


    public static void main(String[] args) {
        // TODO Auto-generated method stub

        testPrintHashmapValues(putToHashMap());
        testPrintHashmapValuesWithList(putToHashMapWithList());
    }

    public static Map<Integer, Long> putToHashMap() {
        Map<Integer, Long> map = new LinkedHashMap<Integer, Long>();
        for (int i = 0; i < 10; i++) {
            map.put(Integer.valueOf(i), Long.valueOf(200000000000L + i));
        }
        return map;
    }

    public static void testPrintHashmapValues(Map<Integer, Long> map) {
        for (Map.Entry<Integer, Long> entry : map.entrySet()) {
            System.out.println("key: " + entry.getKey() + " value: "
                    + entry.getValue());
        }
    }

    public static List<Map<Integer, Long>> putToHashMapWithList() {
        List<Map<Integer, Long>> listOfHash = new ArrayList<Map<Integer, Long>>();
        for (int i = 0; i < 10; i++) {
            Map<Integer, Long> mMap = new HashMap<Integer, Long>();

            mMap.put(Integer.valueOf(i), Long.valueOf(100000000000L + i));
            listOfHash.add(mMap);
        }
        return listOfHash;
    }

    public static void testPrintHashmapValuesWithList(
            List<Map<Integer, Long>> listOfHash) {

        for (int i = 0; i < 10; i++) {
            Map<Integer, Long> map = listOfHash.get(i);
            for (Map.Entry<Integer, Long> entry : map.entrySet()) {
                System.out.println(i + " hashMap");
                System.out.println("key: " + entry.getKey() + " value: "
                        + entry.getValue());
            }
        }
    }

}

My Task is done even without creating a List.

Community
  • 1
  • 1
Ads
  • 6,681
  • 12
  • 47
  • 72

6 Answers6

2

You iterate over the list to get the maps, and then iterate over their key set:

public static void main(String[] args) throws NoSuchAlgorithmException {
  List<Map<Integer, Long>> ListofHash = new ArrayList<Map<Integer,Long>>();
  for (int i = 0; i < 10; i++) {
    Map<Integer, Long> mMap = new HashMap<Integer, Long>();
    mMap.put(Integer.valueOf(i), Long.valueOf(100000000000L + i));
    ListofHash.add(mMap);
  }

  for (Map<Integer, Long> map : ListofHash) {
    for (Integer key : map.keySet()) {
      System.out.println(map.get(key));       
    }
  }
}

Note: I've also changed a little your code, using Map instead of HashMap when possible

MarcoS
  • 13,386
  • 7
  • 42
  • 63
  • one further improvement is using google guava to declare the list: List> listOfHash = newArrayList(); – Kevin Jun 15 '11 at 14:08
2

I am assuming you are asking how to get it from ArrayList,

ListofHash.get(0).get(Object key);

Object key means whatever Integer key you want

Good Luck!

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
TeaCupApp
  • 11,316
  • 18
  • 70
  • 150
  • This assumes what you are looking for is in the first Map (and that there is a first Map) – Peter Lawrey Jun 15 '11 at 14:18
  • @Peter , Totally true! I thought his question was more general about how to access those elements. However Iteration on ListofHash will be a far better option. – TeaCupApp Jun 15 '11 at 14:22
  • it appears to me he just needs a `long[10]` which would much simpler. – Peter Lawrey Jun 15 '11 at 14:24
  • it seems that i must know the key to get the value right.but i dono the key too since i get both from DB. please see the update 1. – Ads Jun 15 '11 at 14:40
2

Something of this effect:

public static Long getValue(Integer key)) {

    for (HashMap<Integer, Long> entry : ListofHash) {
        if (entry.containsKey(key)) {
            return entry.get(key);
        }
    }

    return null;
}

//Retrieve all key/value
for (HashMap<Integer, Long> entry : ListofHash) {
    for (Integer key : entry.keySet()) {
        System.out.println("Key : " + key + ", value: " + entry.get(key));
    }
}

PS Untested.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
2

Its still not clear to me why you want a list.

public static Map<Integer, Long> populateMyHashMapFromDB() {
    Map<Integer, Long> map = new LinkedHashMap<Integer, Long>();
    for (int i = 0; i < 10; i++) 
            map.put(getIntFromDB(i), getLongFromDB(i));
    return map;
}

Map<Integer, Long> map = populateMyHashMapFromDB();
Long value = map.get(key);

This collection isn't designed to give you the key/values pairs easily. I fyou need to this functionality I would suggest using a different structure.

Assuming you have a some bad code you cannot change, you can do

List<Map<Integer, Long>> maps = new ArrayList<Map<Integer, Long>>();

Map<Integer, Long> all = new HashMap<Integer, Long>();
for(Map<Integer, Long> map: maps)
    all.putAll(map);

for(Map.Entry<Integer, Long> entry: all.entrySet() {
    // do something which each key/value.
}

In this example you don't need a List or a Map.

long[] longs = new long[10];
for (int i = 0; i < 10; i++) 
    longs[i] = i;

int key = 1;
int num = longs[key];
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    may i know what is the difference in using Map and HashMap, and what is complex is the pervious case??? (Just eager to know) – Ads Jun 15 '11 at 14:10
  • 1
    Map can be substituted with any Map implementation. Unless you need to use a method only available on HashMap it is better to use an interface. Using a List of Map is more complex than using a single Map as I have shown you in the example, it is not clear why you need a List of them. – Peter Lawrey Jun 15 '11 at 14:18
  • yep i agree that in the example hashmap is not needed but in my case i get key and value from the database where value is not incremental and we also dont know what the value also. see my update 1 code. hope now it is pretty much clear. – Ads Jun 15 '11 at 14:38
  • 1
    @Adhavan, I have updated my answer. Perhaps you are trying to make things more complicated than they need to be. – Peter Lawrey Jun 15 '11 at 14:48
  • 1
    yep.... u r right.... i just confused with list...it can be done using HashMap alone... – Ads Jun 15 '11 at 15:00
1
List<HashMap<Integer, Long>> ListofHash = new ArrayList<HashMap<Integer, Long>>();
        for (int i = 0; i < 10; i++) {
            HashMap<Integer, Long> mMap = new HashMap<Integer, Long>();
            mMap.put(Integer.valueOf(i), Long.valueOf(100000000000L+i));
            ListofHash.add(mMap);
        }
Manuel Selva
  • 18,554
  • 22
  • 89
  • 134
1

You have a total of 10 Maps with only one element stored. And those Maps are stored in a List. You can retrieve those Maps and its elements doing this:

public void testPrintHashmapValues()  {
    List<HashMap<Integer, Long>> listOfHash = new ArrayList<HashMap<Integer, Long>>();
    for (int i = 0; i < 10; i++) {
        HashMap<Integer, Long> mMap = new HashMap<Integer, Long>();
        mMap.put(Integer.valueOf(i), Long.valueOf(100000000000L+i));
        listOfHash.add(mMap);
    }

    for (int i = 0; i < 10; i++) {
        Map<Integer, Long> map = listOfHash.get(i);
        for (Map.Entry<Integer, Long> entry : map.entrySet()) {
            System.out.println(i + " hashMap");
            System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
        }
    }

}

Also the variables should not start with uppercase (ListOfHash). Even though java won't complain it is a bad practice and difficult to read code like that because one might think it's a class name instead of a variable.

Alfredo Osorio
  • 11,297
  • 12
  • 56
  • 84
  • he he.... sorry!!!! just felt lazy to change after typing... U spoted thank you !!!! ;) – Ads Jun 15 '11 at 14:46