Comment Obtenir les Valeurs à partir de la Liste de la table de hachage?

J'ai un List de HashMap's qui a la clé de type Integer et la valeur de 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);
        }

Maintenant, comment puis-je récupérer la clé et de la valeur à partir de la liste des HashMap?

Si vous utilisez Collection classe est la solution, merci de m'éclairer.

Mise à jour 1:

En fait je suis à l'obtention de la valeur à partir de la base de données et le mettre dans un 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;


}

où getIntFromDB et getLongFromDB pouvez récupérer tout int et long valeurs respectivement.

Maintenant c'est où je veux obtenir mes valeurs que j'ai reçu de DB à la fois des clés et des valeurs

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

//This is where i got confused

}

RÉPONSE C'est pourquoi Peter Lawrey suggéré

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());
}
}
}
}

Ma Tâche est accomplie, même sans la création d'une Liste.

OriginalL'auteur Ads | 2011-06-15