[英]How to add each hashmap to a arrayList
final ArrayList<HashMap<String, String>> actualList = new ArrayList<HashMap<String, String>>();
for (Response response : responseList) {
if (responseList != null) {
resultsMap.put("a", response.getResult());
resultsMap.put("b", response.getOriginalUrl())
actualList.add(resultsMap);
}
這不會添加到hashList到arrayList
在給定的代碼中
嘗試這個,
final ArrayList<HashMap<String, String>> actualList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> resultsMap = null;
for (Response response : responseList) {
resultsMap = new HashMap<>();
resultsMap.put("a", response.getResult());
resultsMap.put("b", response.getOriginalUrl())
actualList.add(resultsMap);
}
final ArrayList<HashMap<String,String>> actualList = new ArrayList<HashMap<String, String>>();
for (Response response : responseList) {
if (response != null){
Map resultsMap = new HashMap<String, String>();
resultsMap.put("a", response.getResult());
resultsMap.put("b", response.getOriginalUrl())
actualList.add(resultsMap);
}
}
final ArrayList<HashMap<String, String>> actualList = new ArrayList<HashMap<String, String>>();
if (responseList != null) {
for (Response response : responseList) {
HashMap<String, String> map=new HashMap<>();
map.put("a", response.getResult());
map.put("b", response.getOriginalUrl())
actualList.add(map);
}
}
您應該為循環的每次迭代創建一個新的HashMap
實例。
除此之外,也許您打算檢查循環內的response
是否不為空,因為responseList
不能為空。
final ArrayList<HashMap<String, String>> actualList = new ArrayList<HashMap<String, String>>();
for (Response response : responseList) {
HashMap<String, String> resultsMap = new HashMap<String, String> ();
if (response != null) {
resultsMap.put("a", response.getResult());
resultsMap.put("b", response.getOriginalUrl())
actualList.add(resultsMap);
}
}
您可以這樣寫:
final ArrayList<HashMap<String, String>> actualList = new ArrayList<HashMap<String, String>>();
for (Response response : responseList) {
HashMap<String,String>resultMap = new HashMap<String,String>();
if (responseList != null) {
resultsMap.put("a", response.getResult());
resultsMap.put("b", response.getOriginalUrl())
}
actualList.add(resultMap);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.