HashMaps和Null值?

时间:2013-02-26 14:07:13

标签: java null hashmap hashcode

如何将空值传递给HashMap?
以下代码段适用于填写的选项:

HashMap<String, String> options = new HashMap<String, String>();  
options.put("name", "value");
Person person = sample.searchPerson(options);  
System.out.println(Person.getResult().get(o).get(Id));    

那么问题是必须输入选项和/或传递空值的方法?
我尝试了以下代码但没有成功:

options.put(null, null);  
Person person = sample.searchPerson(null);    

options.put(" ", " ");  
Person person = sample.searchPerson(null);    

options.put("name", " ");  
Person person = sample.searchPerson(null);  

options.put();  
Person person = sample.searchPerson();    

6 个答案:

答案 0 :(得分:97)

HashMap支持null个键和值

http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html

  

...并允许空值和空键

所以你的问题可能不是地图本身。

答案 1 :(得分:7)

您可以记下以下可能性:

1。在地图中输入的值可以是null

但是,如果使用多个null键和值,则只需使用一次键值对。

Map<String, String> codes = new HashMap<String, String>();

codes.put(null, null);
codes.put(null,null);
codes.put("C1", "Acathan");

for(String key:codes.keySet()){
    System.out.println(key);
    System.out.println(codes.get(key));
}

输出将是:

null //key  of the 1st entry
null //value of 1st entry
C1
Acathan

2。您的代码只会执行null

options.put(null, null);  
Person person = sample.searchPerson(null);   

这取决于您的searchPerson方法的实施 如果您希望多个值为null,则可以相应地实现

Map<String, String> codes = new HashMap<String, String>();

    codes.put(null, null);
    codes.put("X1",null);
    codes.put("C1", "Acathan");
    codes.put("S1",null);


    for(String key:codes.keySet()){
        System.out.println(key);
        System.out.println(codes.get(key));
    }

输出:

null
null

X1
null
S1
null
C1
Acathan

答案 2 :(得分:3)

您似乎正在尝试使用Map参数调用方法。 因此,要使用空人名称呼叫,正确的方法应该是

HashMap<String, String> options = new HashMap<String, String>();
options.put("name", null);  
Person person = sample.searchPerson(options);

或者你可以这样做

HashMap<String, String> options = new HashMap<String, String>();
Person person = sample.searchPerson(options);

使用

Person person = sample.searchPerson(null);

可以获得空指针异常。这一切都取决于searchPerson()方法的实现。

答案 3 :(得分:0)

避免在地图中使用null,这是一种很好的编程习惯。

如果您有一个null值的条目,则无法判断某个条目是否存在于地图中,或者是否有与其关联的null值。

您可以为这种情况定义常量(例如:String NOT_VALID = "#NA"),也可以让另一个集合存储具有null值的键。

请查看此link了解详情。

答案 4 :(得分:-1)

根据你的第一个代码,snipet似乎没问题,但是由于编程错误导致了类似的行为。您是否在put调用之前检查了“options”变量是否为null?

我正在使用Struts2(2.3.3)webapp并使用HashMap来显示结果。 何时执行(在Action类初始化的类中):

if(value != null) pdfMap.put("date",value.toString());
else pdfMap.put("date","");

出现此错误:

Struts Problem Report

Struts has detected an unhandled exception:

Messages:   
File:   aoc/psisclient/samples/PDFValidation.java
Line number:    155
Stacktraces

java.lang.NullPointerException
    aoc.psisclient.samples.PDFValidation.getRevisionsDetail(PDFValidation.java:155)
    aoc.action.signature.PDFUpload.execute(PDFUpload.java:66)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    ...

似乎NullPointerException指向put方法(行号155),但问题是de Map之前尚未初始化。它编译好了,因为变量超出了设置值的方法。

答案 5 :(得分:-5)

你可以这样做:

String k = null;
String v = null;
options.put(k,v);
相关问题