Is there any way to make a key for searching the values in the collections and not returning null keys and values?
Just implement your own Map which throws for example IllegalArgumentException instead of returning null.
All with all, this makes no sense. If you want, just test the existence of the key using Map#containsKey() and/or check if the returned value is null or not.
Related
I'm trying to work with Java's Junit Test. My goal is to create a hashtable as
Hashtable< String , Hashtable<String,String> > student =
new Hashtable<String, Hashtable<String, String>>();
Next, I want to add at first only the key. After that, when I have the value then add it to the key in hashtable.
Example
student.put("student1",null) ;
I tried to work with null, but during test process I get a
java.lang.NullPointerException
Is it not possible? Any ideas to how adding only the key without value?
Is it possible to add a key without a value in hashtable Java?
Short answer: No
Why?
Because it will throw and Exception if
* #exception NullPointerException if the key or value is
* <code>null</code>
And in the same source code has a validation
public synchronized V put(K key, V value) {
// Make sure the value is not null
if (value == null) {
throw new NullPointerException();
}
...
You can use a Map Instead.
HashTable is an older implementation and HashMap came as an advanced version with more capabilities. You can't call .equals() or .hashCode() on it as null isn't an object.
HashMap is a better replacement for single threaded applications or any time synchronization is not a requirement, because of the performance impact synchronization introduces. If you need a Threadsafe option you can also use ConcurrentHashMap
The documentation says that you can't:
Maps the specified key to the specified value in this hashtable.
Neither the key nor the value can be null.
The best that you can do, if you need the key, is to use an empty HashTable as the value:
student.put("student1", new Hashtable<>());
In most use cases you would not care about putting null into a HashMap. You can check which keys have a value assigned via the keySet() and treat everything else as null (since it was never assigned).
However, your question to me indicates that you need to distinguish three states: Key-Value pair, key that were never assigned a value, and keys that were explicitly assigned a null value.
Since the default HashMap does not support this behavior, you could implement your own class that implements the Map interface. It basically only wraps a HashMap, with a few important exceptions:
When you assign a value of null to a key, you put the key into a Set of "NullKeys" instead of into the HashMap.
When you retrieve a key, check whether it is in the "NullKey" Set. If yes, return null, otherwise look into the HashMap.
There are some special cases (overwriting an existing value with null etc.) but this would be the basic strategy.
(I am not sure whether this is a useful class to have other than in very specialized scenarios but it would meet your requirements.)
This question already has answers here:
Why Hashtable does not allow null keys or values?
(10 answers)
Closed 4 years ago.
I know about null key is not allowed in Hashtable because to store element in Hashtable hash code must required. But if key is null it will unable to calculate hash code for null key. But I don't understand but what is the exact reason in mind for Sun developers not to allow null value.
Someone says there is null check for value inside put method implementation and that's why it throws NullPointerException. But my question is why that null value check. Is there any specific reason behind it.
I went through lots of read but no got satisfied answer. Some one says there is ambiguity if there is null value and if you try to retrieve value using get() method it will return null and this null is because of actual value is null or key is missing that's why null and could not predict reason. So i need pin point answer with proof.
You will get NULL for value if you do
hashtable.get("key")
and "key" is not in the map, then you don't need to store null values.
If you would be able to store null, you will never know what you had: null mapping or that is a missing mapping.
Hashtable is considered legacy code. You should use HashMap and it allow null for values and also one key can be null.
EDIT
After deeper search I may have argument for such decision. Hashtable is synchronized (and HashMap isn't).
From JavaDoc:
Unlike the new collection implementations, Hashtable is synchronized. If a thread-safe implementation is not needed, it is recommended to use HashMap in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.
As you can see successor of Hashtable is not HashMap as I previously write but ConcurrentHashMap. I was surprised that ConcurrentHashMap does not allows null. I start digging and found this:
From the author of ConcurrentHashMap himself (Doug Lea):
The main reason that nulls aren't allowed in ConcurrentMaps (ConcurrentHashMaps, ConcurrentSkipListMaps) is that ambiguities that may be just barely tolerable in non-concurrent maps can't be accommodated. The main one is that if map.get(key) returns null, you can't detect whether the key explicitly maps to null vs the key isn't mapped. In a non-concurrent map, you can check this via map.contains(key), but in a concurrent one, the map might have changed between calls.
So maybe authors of Hashtable have the same reason as authors of ConcurrentHashMap
Having a null value is still considered a bad decision in HashMap and the new Map classes and the static factory methods in java-9 prove that:
Map.of("test", null)
will throw a NulPointerException
From Java Documentation
To successfully store and retrieve objects from a hash table, the
objects used as keys must implement the hashCode method and the equals
method
Null is not an object, so can not call .equals() or .hashCode() on it, so the Hashtable can't compute a hash to use it as a key
Hashtable containsValue(Object value) function throw NullPointerException if the value is null so for the value also not allowed null
Today i read in a book:-
"HashMap allows one null key and multiple null values in a collection."
HashMap<Object,Object> ih=new HashMap<Object,Object>();
Object o1=null;
Integer o2=null;
ih.put(o1,null);
ih.put(new Integer(2),null);
ih.put(o2,new Integer(3));
This example is putting two null object references in the map.
But the following Example is successfully compiling and running...
I cant figure out why?
"HashMap allows one null key and multiple null values in a
collection."
Allow one null key means if you keep adding null key, it will overwrite the previous values. Actually, this is true for any key in HashMap.
The bottom line is - HashMap allows only one key
e.g. if you print the size of your Hashmap it will be 2.
System.out.println("size:: "+ih.size());
What the book probably means is this:
among the possible keys of a HashMap<X, Y>, there can be a null key;
whatever the key (even null!), the value associated to this key can be null.
Therefore, it is perfectly normal that your code works. It is just that the book's text here is quite confusing.
But of course, this entirely depends on the Map implementation; some of them, such as ConcurrentHashMap, will not allow null keys or values.
I know it's maybe not the best thing to do and that I'm not using properly this object, but here is my problem. I want to use a ConcurrentSkipListMap and have declared my map as such:
private ConcurrentSkipListMap<Date,T> map
Sometimes, I need to initialize elements of this map with a "null" value, meaning that on a given day, there were no data. For a regular hastable, that works, you can insert a null value associated to a key. When getting the value for a certain date, I just have to test if it is null or not. For a ConcurrentSkipListMap, that does not work. It throws a null value exception. When looking at the source code, I understood that this is an internal representation of the Nodes where a null value just means that the Node in the list is dead.
I know the best answer to this would be, just don't add the date in that case... but for many reasons, I still need to. And anyway, testing for the existence of the key is to me the same as testing for a null value associated to a key. I can't add a dummy value as well cause I will not be able to make a distinction between this dummy value and a real one (for, example, if my template class is an Integer, even if I choose -1 as a flag for empty, i would not be able to see the difference with a real value of -1 for this day).
I was previously using
private SortedMap<Date,T> map = Collections.synchronizedSortedMap(new TreeMap<Date,T>(new BasicDateComparatorIncreasing()));
but I like the fact that ConcurentSkipListMap implements the ConcurrentNavigableMap interface
Is there a way to achieve what I want to do ?
thanks
How about
private ConcurrentSkipListMap<Optional<Date>, T> map;
?
Then to add elements:
map.put(Optional.fromNullable(date), value);
And when accessing key check Optional.isPresent() and call Optional.get().
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Optional.html
I have some questions about Java Collection objects...
When we add objects to a collection like HashSet or HashMap, how the the objects internally stored?
Why doesn't Hashtable allow null values?
You're not adding an object to the collection. You're adding a reference.
As for why HashTable doesn't allow null keys and values - it's just a design decision; in some cases it's helpful to disallow null keys, while in others it's annoying. HashMap does allow for both null keys and values, for example. There are various reasons for prohibiting nulls:
Usually a null key or a null value indicates a programming error in the calling code. Rejecting this at the point of insertion makes it far easier to find the error than waiting until the code fetches a value and then has an unexpected null.
If you know that values in a map can't be null, then you don't need to do a separate check for containment and then fetch: you can fetch, and if the result is null, you know the key was missing.
It takes a bit more work to handle null keys in the map implementation. While null values can sometimes be useful, null keys almost never are.
Hashtable does not allow null values because it uses the equals and hashcode methods of the objects added to it
http://en.wikipedia.org/wiki/Hash_function