How do you pass in null values into a 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?
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();
Inserting null objects is not possible in ConcurrentHashMap as a key or value.
HashMap puts null key in bucket 0 and maps null as key to passed value. HashMap does it by linked list data structure. HashMap uses linked list data structure internally. In Entry class the K is set to null and value mapped to value passed in put method.
For HashMap, it allows one null key and there is a null check for keys, if the key is null then that element will be stored in a zero location in Entry array. We cannot have more than one Null key in HashMap because Keys are unique therefor only one Null key and many Null values are allowed.
There are perfectly legitimate use cases for maps with null values, e.g. representing the properties and values of a bean, some of which may be null-valued.
HashMap supports both null
keys and values
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:
null
.However with multiple null
keys and values it will only take a null key value pair once.
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
null
only onceoptions.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
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. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With