views:

81

answers:

3

I have a hashmap that is 101 keys in size, but I know for sure about 6 of those have no data inside, and there may be more without data as well. What exactly is inside the empty indexes? Is it null? or is there a Hash(index).isEmpty() method that I can use to see if its empty?

I realize there is a isEmpty method inside hashmap, but I thought that only checked if the entire map was empty not just a single index.

A: 

Well, for the keys to arrive there with no data, you have to put them there.

If you did map.put(key, null) then yes the data for that key is null. You always have to give the second parameter to the method, you can't just map.put(key).

If you know for sure that a certain key should have no data you could try going into debug mode and putting a watch for myMap.get(myEmptyKey) and see what you get (in case that no data is an empty object or something else, you should be able to see that).

Edit: Some code would be useful to help you, but if I understand correctly you do something like this:

for (Object obj : list) {
    if (matchesCriteriaX(obj)) {
         map.put("X", obj);
    else if (matchesCriteriaY(obj)) {
         map.put("Y", obj);
    }
}

Well, if you do that and try to do map.get("X"), but you never actually put anything for that key (becaus no object matched criteria X), you will most definitely get back a null.

On the other hand, if you did something like

Map<String, List<Object>> map = new HashMap<String, List<Object>>();
map.add("X", new ArrayList<Object>());
map.add("Y", new ArrayList<Object>());
for (Object obj : list) {
    if (matchesCriteriaX(obj)) {
         List<Object> list = map.get("X");
         list.add(obj);
    else if (matchesCriteriaY(obj)) {
         List<Object> list = map.get("Y");
         list.add(obj);
    }
}

then you could check if a category is empty by doing map.get("x").isEmpty() since List has that method (and it would be empty if no object matched the key criteria).

Andrei Fierbinteanu
Well basically, my application is reading in 3300 products from a database and then mapping them based on certain criteria to different keys in the hash, so I know a few will be empty, but since I am using external hashing I know a couple of the keys will be empty due to not all the criteria being met.So while I am running a loop to add all these into my tree I am trying to figure out how to skip the empty keys and move on to the next one.
SomeoneRandom
"A couple of keys will be empty", what does that mean? Does that mean these keys have no associated values, if so, then how did you add them in the hashmap in the first place?
Cambium
@SomeoneRandom see my edit please
Andrei Fierbinteanu
Yes your basic first example was the short version of what I was doing. Got it all working, thanks for the help.
SomeoneRandom
@SomeoneRandom: Instead of using `Map<String, List<Object>>` you can also use [`Multimap<String, Object>`](http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Multimap.html) from the Google Collections Framework (now part of the [Guava library](http://code.google.com/p/guava-libraries/)).
Adam Paynter
+1  A: 

I realize there is a isEmpty method inside hashmap, but I thought that only checked if the entire map was empty not just a single index.

I think what you're looking for is the containsKey(Object) method. According to the documentation:

Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k)). (There can be at most one such mapping.)

Parameters:

key - key whose presence in this map is to be tested

Returns:

true if this map contains a mapping for the specified key

Adam Paynter
A: 

Judging from what you said, I'm suspecting something like this:

Map<SomeKey, List<SomeValue>> yourMap;

If this is the case, what you can do is

if( yourMap.contains(someKey) ){
    List<SomeValue> someList = yourMap.get(someKey);
    if(someList.size() == 0){
        // it's empty, do something?
    }
}
Cambium