[英]Difference between HashMap<String,String> and List<NameValuePair>
While calling a loginTask i have to send username and password. 在调用loginTask时,我必须发送用户名和密码。 Now i tried to replace this List<NameValuePair>
code with HashMap<String,String>
but i couldn't . 现在我尝试用HashMap<String,String>
替换此List<NameValuePair>
代码,但是我不能。 Know i need to know the difference between them . 知道我需要知道它们之间的区别。 when i should use List and when i should use HashMap 什么时候应该使用列表以及什么时候应该使用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);
A HashMap
(in Java an implementation of the java.util.Map
interface and in theory referred to as hashtable) allows you access in O(1)
while in a list of n
pairs you have O(n)
access time. HashMap
(在Java中是java.util.Map
接口的实现,理论上称为哈希表)允许您在O(1)
进行访问,而在n
对列表中则具有O(n)
访问时间。
The choice which to use is both related to the use case (I left recommendations in my comment below because it refers to your very specific use case) and a tradeoff between different dimensions of software engineering, eg if your unfamiliar with Map
s you might experience a maintenance overhead which stands in opposition to performance improvement (expressed as in O notation in this case). 使用的选择既与用例相关(我在下面的评论中保留了建议,因为它涉及的是您的特定用例),并且在软件工程的不同维度之间进行了权衡,例如,如果您不熟悉Map
,可能会遇到与性能改进相反的维护开销(在这种情况下用O表示)。 It's a decision. 这是一个决定。
The constructors of UrlEncodedFormEntity ( http://developer.android.com/reference/org/apache/http/client/entity/UrlEncodedFormEntity.html ) only accept List as parameter so the compiler will refuse to use a HashMap (of any kind) UrlEncodedFormEntity( http://developer.android.com/reference/org/apache/http/client/entity/UrlEncodedFormEntity.html )的构造函数仅接受List作为参数,因此编译器将拒绝使用HashMap(任何类型)
Besides that, two Strings no not make a NameValuePair ( http://developer.android.com/reference/org/apache/http/NameValuePair.html ) ;) 除此之外,两个String不能不构成NameValuePair( http://developer.android.com/reference/org/apache/http/NameValuePair.html );)
HashMap is collection of key/value pair and one should use it when one wants to retrieve and insert values based on Key. HashMap是键/值对的集合,当要基于键检索和插入值时,应使用HashMap。
You can use it in this code like below, 您可以在下面的代码中使用它,如下所示,
Map<String, String> map = new HashMap<String, String>();
map.put("username",param[0]);
map.put("password",param[1]);
UrlEncodedFormEntity constructor accepts only List <? extends NameValuePair>
UrlEncodedFormEntity构造函数仅接受List <? extends NameValuePair>
List <? extends NameValuePair>
not HashMap
and to create result using format() ie following algorithm 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();
}
Have a look on following link http://grepcode.com/file/repo1.maven.org/maven2/org.apache.httpcomponents/httpclient/4.0/org/apache/http/client/entity/UrlEncodedFormEntity.java 看看以下链接http://grepcode.com/file/repo1.maven.org/maven2/org.apache.httpcomponents/httpclient/4.0/org/apache/http/client/entity/UrlEncodedFormEntity.java
I'm adding my answer here because I can see there is some confusion among the other answers. 我在此处添加答案,因为我可以看到其他答案之间有些混淆。
First of all, HashMap<K,V>
is an implementation of the Map<K,V>
interface, while the List<E>
is just an interface, which needs to be implemented. 首先, HashMap<K,V>
是Map<K,V>
接口的实现,而List<E>
只是一个接口,需要实现。 ArrayList<E>
is such an implementation. ArrayList<E>
是这样的实现。
A Map is used when you want to associate certain keys, with certain values. 当您要将某些键与某些值关联时,可以使用Map。 For example, it would make sense for a generic JSON parser to store the JSON object in a HashMap. 例如,对于通用JSON解析器来说,将JSON对象存储在HashMap中是有意义的。 A List, on the other hand, is used when you want to have an ordered collection of items. 另一方面,当您想要有序的项目集合时使用列表。
Another thing I'd like to clear up, is that, contrary to what @KarlRichter mentions, an implementation of List
, if done correctly, would access an element in O(1), and not in O(n). 我想澄清的另一件事是,与@KarlRichter提到的相反, List
的实现(如果正确完成)将访问O(1)中的元素,而不是O(n)中的元素。 He seems to have confused the List
with a LinkedList
. 他似乎已经将List
与LinkedList
混淆了。 A HashMap
, would typically add the overhead of the hashing, so it could be just a little bit slower than List
(in most cases unnoticeable), but still it would technically remain O(1). HashMap
通常会增加哈希的开销,因此它可能比List
慢一点(在大多数情况下不明显),但从技术上讲,它仍为O(1)。
However, a List
serves a different purpose than a Map
, so the comparison is still off the point. 但是, List
的用途不同于Map
,因此比较仍然没有意义。
In your case, you cannot replace List<NameValuePair>
with HashMap<String,String>
, because, as shown here , URLEncodedFormEntity
only accepts List<? extends NameValuePair>
你的情况,你不能代替List<NameValuePair>
用HashMap<String,String>
,因为,如图所示这里 , URLEncodedFormEntity
只接受List<? extends NameValuePair>
List<? extends NameValuePair>
or Iterable<? extends NameValuePair>
List<? extends NameValuePair>
或Iterable<? extends NameValuePair>
Iterable<? extends NameValuePair>
in all the available constructors. 在所有可用的构造函数中Iterable<? extends NameValuePair>
。
If you must use a HashMap<String,String>
, you could do some kind of conversion, like the following: 如果必须使用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;
}
So, then, you create your list from the HashMap like below: 因此,您可以从HashMap创建列表,如下所示:
List<NameValuePair> list = hashMapToNameValuePairList(yourHashMap);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.