title: How to Get Values from List of HashMap?
body: <p>I have a <code>List</code> of <code>HashMap</code>'s which has key of type <code>Integer</code> and value of type <code>Long</code>.</p>
  
  <pre><code>List&lt;HashMap&lt;Integer, Long&gt;&gt; ListofHash = new ArrayList&lt;HashMap&lt;Integer, Long&gt;&gt;();
          for (int i = 0; i &lt; 10; i++) {
              HashMap&lt;Integer, Long&gt; mMap = new HashMap&lt;Integer, Long&gt;();
              mMap.put(Integer.valueOf(i), Long.valueOf(100000000000L+i));
              ListofHash.add(mMap);
          }
  </code></pre>
  
  <p>Now, how do I retrieve the key and value from the list of <code>HashMap</code>?</p>
  
  <p>If using <code>Collection</code> class is the solution, please enlight me.</p>
  
  <p><strong>Update 1:</strong></p>
  
  <p>Actually i am getting the value from the database and putting that into a HashMap</p>
  
  <pre><code>public static List&lt;Map&lt;Integer, Long&gt;&gt; populateMyHashMapFromDB()
  {
  
  List&lt;HashMap&lt;Integer, Long&gt;&gt; ListofHash = new ArrayList&lt;HashMap&lt;Integer, Long&gt;&gt;();
  
  for (int i = 0; i &lt; 10; i++) {
                  HashMap&lt;Integer, Long&gt; mMap = new HashMap&lt;Integer, Long&gt;();
                  mMap.put(Integer.valueOf(getIntFromDB(i)), Long.valueOf(getLongFromDB(i)));
                  ListofHash.add(mMap);
              }
  return ListofHash;
  
  
  }
  </code></pre>
  
  <p>where getIntFromDB and getLongFromDB can retreive any int and long values respectively.</p>
  
  <p>Now this is where i want to get my values that i got from DB both keys and values </p>
  
  <pre><code>public void getmyDataBaseValues()
  {
  List&lt;HashMap&lt;Integer,Long&gt;&gt; ListofHash=populateMyHashMapFromDB();
  
  // This is where i got confused
  
  }
  </code></pre>
  
  <p><strong><em>ANSWER</em></strong> This is why <a href="https://stackoverflow.com/users/57695/peter-lawrey">Peter Lawrey</a> suggested </p>
  
  <pre><code>public class HashMaps {
  
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
  
          testPrintHashmapValues(putToHashMap());
          testPrintHashmapValuesWithList(putToHashMapWithList());
      }
  
      public static Map&lt;Integer, Long&gt; putToHashMap() {
          Map&lt;Integer, Long&gt; map = new LinkedHashMap&lt;Integer, Long&gt;();
          for (int i = 0; i &lt; 10; i++) {
              map.put(Integer.valueOf(i), Long.valueOf(200000000000L + i));
          }
          return map;
      }
  
      public static void testPrintHashmapValues(Map&lt;Integer, Long&gt; map) {
          for (Map.Entry&lt;Integer, Long&gt; entry : map.entrySet()) {
              System.out.println("key: " + entry.getKey() + " value: "
                      + entry.getValue());
          }
      }
  
      public static List&lt;Map&lt;Integer, Long&gt;&gt; putToHashMapWithList() {
          List&lt;Map&lt;Integer, Long&gt;&gt; listOfHash = new ArrayList&lt;Map&lt;Integer, Long&gt;&gt;();
          for (int i = 0; i &lt; 10; i++) {
              Map&lt;Integer, Long&gt; mMap = new HashMap&lt;Integer, Long&gt;();
  
              mMap.put(Integer.valueOf(i), Long.valueOf(100000000000L + i));
              listOfHash.add(mMap);
          }
          return listOfHash;
      }
  
      public static void testPrintHashmapValuesWithList(
              List&lt;Map&lt;Integer, Long&gt;&gt; listOfHash) {
  
          for (int i = 0; i &lt; 10; i++) {
              Map&lt;Integer, Long&gt; map = listOfHash.get(i);
              for (Map.Entry&lt;Integer, Long&gt; entry : map.entrySet()) {
                  System.out.println(i + " hashMap");
                  System.out.println("key: " + entry.getKey() + " value: "
                          + entry.getValue());
              }
          }
      }
  
  }
  </code></pre>
  
  <p>My Task is done even without creating a List.</p>