繁体   English   中英

HashMaps 和 Null 值?

[英]HashMaps and Null values?

如何将 null 值传递给 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));    

所以问题是必须在选项和/或方法中输入什么才能传递 null 值?
我尝试了以下代码但没有成功:

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();    

HashMap支持null键和值

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

...并允许空值和空键

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

您可以记下以下可能性:

1.在地图中输入的值可以为null

但是,对于多个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);   

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

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

您似乎正在尝试使用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()方法的实现。

它是一种很好的编程习惯, 可以避免在Map中使用null

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

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

请查看此链接以获取更多详细信息。

我得到 hashmap 和 null 这样的值

{fieldCode1=F0001, fieldId10=null, fieldId11=null }

我使用以下链接使用 Guava 库的方法https://www.techiedelight.com/remove-null-values-map-java/

Iterables.removeIf(inputParams.values(), Predicates.isNull());

inputParams 是 hashmap

Hashmap 使用方法 {fieldCode1=F0001} 后的值

使用下面的导入包 import com.google.common.collect.Iterables; 导入 com.google.common.base.Predicates;

根据你的第一个代码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之前尚未初始化。 它编译好了,因为变量超出了设置值的方法。

你可以这样做:

String k = null;
String v = null;
options.put(k,v);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM