I thought another reason was that explicitly using null values was seen as a likely implementation error but I got that wrong. That was about to duplicate keys, which are illegal as well. So disallowing null had some technical reason but it was also done to improve the robustness of the code using the created collections.
Exactly - a HashMap is allowed to store null, not the Map returned by the static factory methods. Not all maps are the same. Generally as far as I know it has a mistake in the first place to allow nulls in the HashMap as keys, newer collections ban that possibility to start with.
You do get, it returns null. What does the mean? It has a mapping of null or it is not present? Same goes for a Key - hashcode from such a null key has to treated specially all the time. Banning nulls to start with - make this easier. While HashMap does allow null values, Map. The Map. The Map instances created by these methods have the following characteristics:. The major difference is: when you build your own Map the "option 1" way So, when you decide that your map should have a null key or value maybe for the reasons listed here then you are free to do so.
But "option 2" is about a convenience thing - probably intended to be used for constants. And the people behind Java simply decided: "when you use these convenience methods, then the resulting map shall be null-free". Or more precisely: it is something to remember when dealing with that very map object. And just an anecdotal hint why this seems to be a reasonable approach: our team implemented similar convenience methods ourselves.
And guess what: without knowing anything about plans about future Java standard methods - our methods do the exact same thing: they return an immutable copy of the incoming data; and they throw up when you provide null elements. Allowing nulls in maps has been an error.
We can see it now , but I guess it wasn't clear when HashMap was introduced. NullpointerException is the most common bug seen in production code. Some examples:. So, I think that disallowing null in the new JDK9 immutable collections and maps goes in the same direction.
In my opinion, the Map. Then, what is the reason to keep a null as the key or the value? The reason mentioned in the docs of Map. Some map implementations have restrictions on the keys and values they may contain.
For example, some implementations prohibit null keys and values, and some have restrictions on the types of their keys. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Why does Map. Ask Question. Asked 4 years, 5 months ago. Active 4 months ago. Viewed 45k times. NullPointerException at java. I am not able to get, why null is not allowed in second case. Improve this question. Naman The question is wrong. Should be: why does HashMap allow nulls? ZhekaKozlov When we are able to wrap a Map with HashMap, it should allow the features of hashmap or Hashmap should not be allowed to wrap it.
Since "of" fn is related to immutability, it should be there in the ConcurrentMap may be which is extended by ConcurrentHashmap or so. ZhekaKozlov There is no reason to restrict keys or values to being non-null in a general purpose utility class. It is especially pointless to provide a builder abstraction that does not support what the API supports.
Add a comment. Active Oldest Votes. But why? You can listen to Stuart Marks explain it in this talk but JEP the one that introduced the factory methods summarizes it as well: Null elements, keys, and values will be disallowed. I would personally recommend using a Null Object see Null Object Pattern , or some sort of default key, rather than null as a key. Using null always means you have to code to defend against NullPointerException , and should be avoided whenever possible.
As an example, if you are creating an API that will allow users to add values to a map be it a home made Map implementation, or a utility class that uses a map to store its data and your first version let them use null as a key, you will always have to support null keys.
So either your backing Map will have to be an implementation that accepts null keys, or you will have to trap suck keys, and replace them with a different key that will represent a null key. As Erick Robertson mentionned, there are cases where accepting null keys makes sense.
However, if you are designing an API, you should really make sure that you want to handle such keys, as it always means more code to check for null. And it also means more defensive code for the clients of your API. Consider using something like a DefaultedMap that returns a known value if the map does not contain a given key. Depending on your use case, it might be cleaner than using a a null key, or b a NullObject.
If you describe your actual usecase it might be easier to help. Sometimes it's useful to store default value in map as a value of Null key.
I don't know other cases. But it's legal so why you shouldn't if it can make some profit? Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 10 years, 1 month ago. Active 4 months ago. Viewed 2k times. Improve this question. Aleksandar Savkov Aleksandar Savkov 2, 3 3 gold badges 20 20 silver badges 30 30 bronze badges. A null value is usually treated as an unintialised value.
Why do you want to put it in a map? Viewed 10k times. Let's say I have some stream and want to collect to map like this stream. Of course, I can do like this stream. Joel Joel 1 1 gold badge 7 7 silver badges 20 20 bronze badges. I'm voting to close this question as off-topic because questions about working code belong on Code Review.
Since this is your methods func1 and func2 , one of the ways you can beautify this is by having a third method which would act as a predicate filter. Add a comment. Active Oldest Votes. Entry::getKey, Map. Since this entry type disallows null right from the start, it would need filtering before constructing the entry: stream.
Holger Holger k 34 34 gold badges silver badges bronze badges. Michael I'd steer away from any javafx imports tbh — FrederikVH. Michael starting with Java 9, you can use the much simpler Map. Show 3 more comments. Michael Michael You could also implement doFuncs as return Optional. According to Wikipedia: memoization or memoisation is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.
Better name of memoize is memleak. Terran Agreed, you shouldn't forget those things there — fps.
0コメント