【发布时间】: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<Person[], Person[]>。当我在变量声明的左侧看到HashMap时,我倾向于删除“哈希”。
标签: java null hashmap hashcode