[英]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
...並允許空值和空鍵
所以你的問題可能不是地圖本身。
您可以記下以下可能性:
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
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.