开发者

What is inside an empty index of a Hashmap?

开发者 https://www.devze.com 2023-01-08 15:19 出处:网络
I have a hashmap that is 1开发者_运维百科01 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

I have a hashmap that is 1开发者_运维百科01 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.


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


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).


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?
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消