简体   繁体   English

HashMaps 和 Null 值?

[英]HashMaps and Null values?

How do you pass in null values into a HashMap?如何将 null 值传递给 HashMap?
The following code snippet works with options filled in:以下代码片段适用于填充的选项:

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

So the issue is what has to be entered into the options and or method to pass in a null value?所以问题是必须在选项和/或方法中输入什么才能传递 null 值?
I tried the following code without any success:我尝试了以下代码但没有成功:

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 supports both null keys and values HashMap支持null键和值

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

... and permits null values and the null key ...并允许空值和空键

So your problem is probably not the map itself. 所以你的问题可能不是地图本身。

You can keep note of below possibilities: 您可以记下以下可能性:

1. Values entered in a map can be null . 1.在地图中输入的值可以为null

However with multiple null keys and values it will only take a null key value pair once. 但是,对于多个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));
}

output will be : 输出将是:

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

2. your code will execute null only once 2.您的代码只执行一次null

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

It depends on the implementation of your searchPerson method if you want multiple values to be null , you can implement accordingly 如果您希望多个值为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));
    }

output: 输出:

null
null

X1
null
S1
null
C1
Acathan

It seems that you are trying to call a method with a Map parameter. 您似乎正在尝试使用Map参数调用方法。 So, to call with an empty person name the right approach should be 因此,使用空人名称呼叫应该是正确的方法

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

Or you can do it like this 或者你可以这样做

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

Using 运用

Person person = sample.searchPerson(null);

Could get you a null pointer exception. 可以得到一个空指针异常。 It all depends on the implementation of searchPerson() method. 这一切都取决于searchPerson()方法的实现。

Its a good programming practice to avoid having null values in a Map . 它是一种很好的编程习惯, 可以避免在Map中使用null

If you have an entry with null value, then it is not possible to tell whether an entry is present in the map or has a null value associated with it. 如果您有一个具有null值的条目,则无法判断某个条目是否存在于地图中,或者是否具有与之关联的null值。

You can either define a constant for such cases (Example: String NOT_VALID = "#NA" ), or you can have another collection storing keys which have null values. 您可以为这种情况定义常量(例如: String NOT_VALID = "#NA" ),或者您可以拥有另一个存储具有null值的键的集合。

Please check this link for more details. 请查看此链接以获取更多详细信息。

i was getting hashmap with null values like this我得到 hashmap 和 null 这样的值

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

i used the method from using Guava library using below link https://www.techiedelight.com/remove-null-values-map-java/我使用以下链接使用 Guava 库的方法https://www.techiedelight.com/remove-null-values-map-java/

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

inputParams is the hashmap inputParams 是 hashmap

Hashmap value after using the method {fieldCode1=F0001} Hashmap 使用方法 {fieldCode1=F0001} 后的值

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

Acording to your first code snipet seems ok, but I've got similar behavior caused by bad programing. 根据你的第一个代码snipet似乎没问题,但我有类似的行为导致程序错误。 Have you checked the "options" variable is not null before the put call? 在put调用之前你检查过“options”变量是不是null吗?

I'm using Struts2 (2.3.3) webapp and use a HashMap for displaying results. 我正在使用Struts2(2.3.3)webapp并使用HashMap来显示结果。 When is executed (in a class initialized by an Action class) : 何时执行(在Action类初始化的类中):

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

Got this error: 得到此错误:

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)
    ...

Seems the NullPointerException points to the put method (Line number 155), but the problem was that de Map hasn't been initialized before. 似乎NullPointerException指向put方法(行号155),但问题是de Map之前尚未初始化。 It compiled ok since the variable is out of the method that set the value. 它编译好了,因为变量超出了设置值的方法。

you can probably do it like this: 你可以这样做:

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

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

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