【问题标题】:HashMaps and Null values?HashMaps 和 Null 值?
【发布时间】:2013-02-12 00:34:23
【问题描述】:

如何将空值传递给 HashMap?
以下代码 sn -p 与填写的选项一起工作:

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

【问题讨论】:

  • “没有成功”是什么意思?有什么问题?您收到错误消息吗?
  • 了解如何单独使用地图,然后将其集成到您的程序中。 docs.oracle.com/javase/6/docs/api/java/util/Map.html
  • 为什么在 map 中添加 null 作为键后没有出现空指针异常?
  • " " 不是null 字符串,它是一个包含单个空格字符的字符串。所以要搜索那个使用" """ 也不为空,它在一个空字符串中。只有null 是指向null 的正确指针。所以put.("name", null);
  • 丑陋,但也许创建一个Map&lt;Person[], Person[]&gt;。当我在变量声明的左侧看到HashMap 时,我倾向于删除“哈希”。

标签: java null hashmap hashcode


【解决方案1】:

HashMap 支持null 键和值

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

...并允许空值和空键

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

【讨论】:

  • 只是添加ConcurrentHashMap不允许null用作键或值。
【解决方案2】:

您可以注意以下可能性:

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

这取决于您的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

【讨论】:

    【解决方案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() 方法的实现。

    【讨论】:

      【解决方案4】:

      避免在 Map 中包含 null是一种很好的编程习惯。

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

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

      请查看link了解更多详情。

      【讨论】:

      • 是的,但在重构错误代码并希望保留 null 行为时,您可能仍需要将 null 插入 Map。
      • 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.:那Map.containsKey呢?或者遍历地图的所有条目并检查他们的Map.Entry.getKey()
      【解决方案5】:

      根据您的第一个代码片段似乎没问题,但我有类似的行为是由糟糕的编程引起的。你在 put 调用之前检查过“options”变量不为空吗?

      我正在使用 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 之前尚未初始化。它编译正常,因为变量不在设置值的方法中。

      【讨论】:

      • 这个问题似乎与Struts无关。
      【解决方案6】:

      你可以这样做:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-25
        • 2011-04-04
        • 2021-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多