![](/img/trans.png)
[英]How to convert List<NameValuePair> into a hashMap<String, String>?
[英]Difference between HashMap<String,String> and List<NameValuePair>
在調用loginTask時,我必須發送用戶名和密碼。 現在我嘗試用HashMap<String,String>
替換此List<NameValuePair>
代碼,但是我不能。 知道我需要知道它們之間的區別。 什么時候應該使用列表以及什么時候應該使用HashMap
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("username", params[0]));
list.add(new BasicNameValuePair("password", params[1]));
httppost.setEntity(new UrlEncodedFormEntity(list));
HttpResponse responce = httpclient.execute(httppost);
HttpEntity httpEntity = responce.getEntity();
response = EntityUtils.toString(httpEntity);
HashMap
(在Java中是java.util.Map
接口的實現,理論上稱為哈希表)允許您在O(1)
進行訪問,而在n
對列表中則具有O(n)
訪問時間。
使用的選擇既與用例相關(我在下面的評論中保留了建議,因為它涉及的是您的特定用例),並且在軟件工程的不同維度之間進行了權衡,例如,如果您不熟悉Map
,可能會遇到與性能改進相反的維護開銷(在這種情況下用O表示)。 這是一個決定。
UrlEncodedFormEntity( http://developer.android.com/reference/org/apache/http/client/entity/UrlEncodedFormEntity.html )的構造函數僅接受List作為參數,因此編譯器將拒絕使用HashMap(任何類型)
除此之外,兩個String不能不構成NameValuePair( http://developer.android.com/reference/org/apache/http/NameValuePair.html );)
HashMap是鍵/值對的集合,當要基於鍵檢索和插入值時,應使用HashMap。
您可以在下面的代碼中使用它,如下所示,
Map<String, String> map = new HashMap<String, String>();
map.put("username",param[0]);
map.put("password",param[1]);
UrlEncodedFormEntity構造函數僅接受List <? extends NameValuePair>
List <? extends NameValuePair>
而不是HashMap
並使用format()創建結果,即以下算法
public static String format (
final List <? extends NameValuePair> parameters,
final String encoding) {
final StringBuilder result = new StringBuilder();
for (final NameValuePair parameter : parameters) {
final String encodedName = encode(parameter.getName(), encoding);
final String value = parameter.getValue();
final String encodedValue = value != null ? encode(value, encoding) : "";
if (result.length() > 0)
result.append(PARAMETER_SEPARATOR);
result.append(encodedName);
result.append(NAME_VALUE_SEPARATOR);
result.append(encodedValue);
}
return result.toString();
}
我在此處添加答案,因為我可以看到其他答案之間有些混淆。
首先, HashMap<K,V>
是Map<K,V>
接口的實現,而List<E>
只是一個接口,需要實現。 ArrayList<E>
是這樣的實現。
當您要將某些鍵與某些值關聯時,可以使用Map。 例如,對於通用JSON解析器來說,將JSON對象存儲在HashMap中是有意義的。 另一方面,當您想要有序的項目集合時使用列表。
我想澄清的另一件事是,與@KarlRichter提到的相反, List
的實現(如果正確完成)將訪問O(1)中的元素,而不是O(n)中的元素。 他似乎已經將List
與LinkedList
混淆了。 HashMap
通常會增加哈希的開銷,因此它可能比List
慢一點(在大多數情況下不明顯),但從技術上講,它仍為O(1)。
但是, List
的用途不同於Map
,因此比較仍然沒有意義。
你的情況,你不能代替List<NameValuePair>
用HashMap<String,String>
,因為,如圖所示這里 , URLEncodedFormEntity
只接受List<? extends NameValuePair>
List<? extends NameValuePair>
或Iterable<? extends NameValuePair>
在所有可用的構造函數中Iterable<? extends NameValuePair>
。
如果必須使用HashMap<String,String>
,則可以進行某種轉換,如下所示:
public List<NameValuePair> hashMapToNameValuePairList(HashMap<String,String> map) {
List<NameValuePair> list = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
list.add(new BasicNameValuePair(key, value));
}
return list;
}
因此,您可以從HashMap創建列表,如下所示:
List<NameValuePair> list = hashMapToNameValuePairList(yourHashMap);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.