2013-02-08 2 views
0

J'ai un HashMap et je suis itérer comme ceci:Comment ajouter des valeurs HashMap dans un ArrayList

for(HashMap<String, Integer> aString : value){ 
       System.out.println("key : " + key + " value : " + aString); 

      } 

-je obtenir le résultat que:

key : My Name value : {SKI=7, COR=13, IN=30} 

Maintenant je dois séparer le `SKI , COR et IN dans 3 ArrayLists différents avec leurs valeurs correspondantes? Comment faire ça?

+0

Vos données sont toujours JSON? –

+0

Je ne peux pas utiliser JSON? ne pouvons-nous pas le faire d'une manière normale? – Goofy

+0

Comment cette question est liée à Android? – iargin

Répondre

0

Si vos données sont toujours JSON, vous pouvez simplement décoder comme vous le feriez normalement JSON:

ArrayList<Integer> array = new ArrayList<Integer>(); 
JSONArray json = new JSONArray(aString); 
for (int i =0; i< json.length(); i++) { 
    array.add(json.getInt(i)); 
} 
+0

Je ne peux pas utiliser JSON? ne pouvons-nous pas le faire d'une manière normale? – Goofy

+0

Vous devez analyser la chaîne manuellement. Diviser sur ',', puis diviser chaque ligne par '='. –

0

Je ne suis pas sûr de ce que votre super hashmap contient parce que votre code est si court, mais presque On dirait qu'il vous donne le toString() de la hashmap. La meilleure façon de parcourir une hashmap serait:

Map mp; 
    ..... 
    Iterator it = mp.entrySet().iterator(); 
    while (it.hasNext()) { 
     Map.Entry pairs = (Map.Entry)it.next(); 
     System.out.println(pairs.getKey() + " = " + pairs.getValue()); 
     String key = pairs.getKey(); 
     String value = pairs.getValue(); 
     //Do something with the key/value pair 
    } 

Mais si vous Itère votre hashmap correctement puis est inférieure à la solution pour analyser manuellement cette chaîne en trois ArrayLists différentes, ce qui est probablement la meilleure façon de faire il.

ArrayList <String> ski = new ArrayList <String>(); 
ArrayList <String> cor = new ArrayList <String>(); 
ArrayList <String> in = new ArrayList <String>(); 

for (HashMap < String, Integer > aString: value) { 
    System.out.println("key : " + key + " value : " + aString); 
    aString.replace("{", ""); 
    aString.replace("}", ""); 
    String[] items = aString.split(", "); 
    for (String str: items) { 
     if (str.contains("SKI")) { 
      String skiPart = str.split("="); 
      if (skiPart.length == 2) ski.add(skiPart[1]); 
     } 
     elseif(str.contains("COR")) { 
      String corPart = str.split("="); 
      if (corPart.length == 2) cor.add(corPart[1]); 
     } 
     elseif(str.contains("IN")) { 
      String inPart = str.split("="); 
      if (inPart.length == 2) in.add(inPart[1]); 
     } 
    } 

} 
0

Voici une liste de tableaux (Liste) complète de HashMaps:

ArrayList<HashMap<String, Object>> userNotifications = new ArrayList<HashMap<String, Object>>(); 
int count = 0; 

HashMap<String, Object> notificationItem = new HashMap<String, Object>(); 
notificationItem.put("key1", "value1"); 
notificationItem.put("key2", "value2"); 
userNotifications.add(count, notificationItem); 
count++; 

ensuite pour récupérer les valeurs:

ArrayList<HashMap<String, Object>> resultGetLast5PushNotificationsByUser = new ArrayList<HashMap<String, Object>>(); 

resultGetLast5PushNotificationsByUser = methodThatReturnsAnArrayList(); 
HashMap<String, Object> item1= resultGetLast5PushNotificationsByUser.get(0); 
String value1= item1.get("key1"); 
String value2= item1.get("key2"); 
HashMap<String, Object> item1= resultGetLast5PushNotificationsByUser.get(1); 
String value1= item1.get("key1"); 
String value2= item1.get("key2"); 
Questions connexes