How to iterate over a collection of a Map

How to iterate over a collection of a Map

I get a ClassCastException when doing the following pls help:

ollection all=CDA.getC(VO);//some method returnign a collection

for (Iterator it=all.iterator(); it.hasNext(); ) {

HashMap element =(HashMap) it.next();

for(Iterator j=element.entrySet().iterator(); j.hasNext(); ) {

Map.Entry e = (Map.Entry) j.next();

String key=(String)e.getKey();

String value=(String)e.getValue();

[478 byte] By [Rhea1978a] at [2007-11-27 11:26:17]
# 1

That would be a way to iterate over a Map. Are you sure the collection "all" contains HashMaps, and that the HashMaps contain Strings for both key and value? What version of Java are you using, by the way?

georgemca at 2007-7-29 16:09:32 > top of Java-index,Core,Core APIs...
# 2

Basically this collection is returned by a query to the database and it is in the form of collection of maps.

and yes both the keys and values are of string

i am using jdk 1.4

Rhea1978a at 2007-7-29 16:09:32 > top of Java-index,Core,Core APIs...
# 3

> Basically this collection is returned by a query to

> the database and it is in the form of collection of

> maps.

> and yes both the keys and values are of string

On what line is the ClassCastException happening?

> i am using jdk 1.4

Ok. If you were on a later version, you would have other, more typesafe options here, but no matter

georgemca at 2007-7-29 16:09:32 > top of Java-index,Core,Core APIs...
# 4

HashMap element =(HashMap) it.next();

Rhea1978a at 2007-7-29 16:09:32 > top of Java-index,Core,Core APIs...
# 5

> HashMap element =(HashMap) it.next();

Then the collection doesn't contain HashMaps. Where is this collection coming from? Try casting it to java.util.Map, if some ORM layer is returning this collection it may be some proprietary implementation

georgemca at 2007-7-29 16:09:32 > top of Java-index,Core,Core APIs...
# 6

Thanks That works with Map yes I have a ORM layer which is returning the values from the query

Rhea1978a at 2007-7-29 16:09:32 > top of Java-index,Core,Core APIs...
# 7

but again a problem how about if some of the values in the returned map is null and i need to access those values I will get a nullpointerexception when I try to do

e.getValue().toString()

Rhea1978a at 2007-7-29 16:09:32 > top of Java-index,Core,Core APIs...
# 8

create a utility class with a method which turns null to an empty String

kilyasa at 2007-7-29 16:09:32 > top of Java-index,Core,Core APIs...
# 9

> but again a problem how about if some of the values

> in the returned map is null and i need to access

> those values I will get a nullpointerexception when I

> try to do

> e.getValue().toString()

Don't do toString on the values! Cast them to string as you were, the CCE will be a lot clearer to fix than a rogue NPE

georgemca at 2007-7-29 16:09:32 > top of Java-index,Core,Core APIs...
# 10

e.getValue() may contain NULL. Thats why you should not do a toString(). Just cast it to String (String).

If you are sure of calling toString(), check for NULL before doing so, to avoid NullPointerException

if (e.getValue() != null) {

e.getValue().toString();

}

http://eathumblepie.blogspot.com

Message was edited by:

saracgi

saracgia at 2007-7-29 16:09:32 > top of Java-index,Core,Core APIs...