Is it possible to use the values method for a HashMap if the values are ArrayLists?

Posted by Denman on Stack Overflow See other posts from Stack Overflow or by Denman
Published on 2012-05-15T14:24:53Z Indexed on 2012/12/12 17:04 UTC
Read the original article Hit count: 136

Filed under:
|
|

I'm stuck trying to get something to work in an assignment. I have a HashMap<Integer, ArrayList<Object>> called sharedLocks and I want to check whether a certain value can be found in any ArrayList in the HashMap.

The following code obviously wouldn't work because Object[] can't be cast to ArrayList[], but it is a demonstration of the general functionality that I want.

ArrayList[] values = (ArrayList[]) sharedLocks.values().toArray();
boolean valueExists = false;
for (int i = 0; i < values.length; i++) {
    if (values[i].contains(accessedObject)) {
        valueExists = true;
    }
}

Is there a way for me to check every ArrayList in the HashMap for a certain value? I'm not sure how to use the values method for HashMaps in this case.

Any help would be much appreciated.

© Stack Overflow or respective owner

Related posts about java

Related posts about arraylist