HashMap - error condition

P

Pete

Hi,

Just a small query w.r.t a HashMap.

The HashMap has a size() method which returns the number of entries in
the map and than I can use the Iterator to walk through these entries.

If the size () returns >= 1 and when I iterate the map, zero entries
returned, is this a valid error condition ! How should this be
handled ?

Pete
 
L

Lew

Pete said:
The HashMap has a size() method which returns the number of entries in
the map and than I can use the Iterator to walk through these entries.

Which iterator, 'map.entrySet().iterator()' or 'map.keySet().iterator()'?
(Not that it makes a difference to the point of your question, really, but
just for completeness).

Would you provide an SSCCE?
If the size () returns >= 1 and when I iterate the map, zero entries
returned, is this a valid error condition !

Is this a question?

What do you mean by "a valid error condition"?

Are you getting an error message? What (precisely) is it?

Why are you testing 'size()' first anyway?
How should this be handled ?

That depends on several things.

Are there other threads accessing the Map simultaneously?

Do you wish to report this condition as an error, or simply treat it as if the
map had tested empty to begin with?

One way to handle such a situation is:

public class Foo
{
private Map <K, V> stuff =
new HashMap <K, V> ();

public void fill()
{
// fill 'stuff' with stuff
}

public void process()
{
for ( K key : stuff.keySet() )
{
doSomethingWith( stuff.get( K ));
}
}

private void doSomethingWith( V value )
{
// etc.
}
}

This illustrates a thread-unsafe approach.
 
T

Tom Anderson

If the size () returns >= 1 and when I iterate the map, zero entries
returned, is this a valid error condition !

No. At least, not if there's no change being made to the map between
calling size() and getting the iterator.

Show us your code, and we can help you work out what's happening.

tom
 
R

Roedy Green

Why are you testing 'size()' first anyway?

just use for:each loop. It will work fine with 0 elements.
--
Roedy Green Canadian Mind Products
http://mindprod.com

We are almost certainly going to miss our [global warming] deadline.
We cannot get the 10 lost years back, and by the time a new global agreement to
replace the Kyoto accord is negotiated and put into effect, there will probably
not be enough time left to stop the warming short of the point where we must not
go. ~ Gwynne Dyer
 
L

Lew

Roedy said:
just use for:each loop. It will work fine with 0 elements.

Good advice. For example,

for ( K key : stuff.keySet() )
{
doSomethingWith( stuff.get( K ));
}
 
T

Tom Anderson

Good advice. For example,

for ( K key : stuff.keySet() )
{
doSomethingWith( stuff.get( K ));
}

Better yet:

for (V value: stuff.values()) {
doSomethingWith(V);
}

Saves a lookup for each item.

tom
 
L

Lew

Oops. Should've been 'stuff.get( key )'. /Mea culpa/.

Tom said:
Better yet:

for (V value: stuff.values()) {
doSomethingWith(V);
}

Saves a lookup for each item.

This excellent suggestion raises for me the question of what to do if a value
is in the value 'Collection' more than once. It could be that the OP is
iterating through the 'Map.Entry' set because the required action depends on
both the key and the value:

for ( Map.Entry <K, V> entry : stuff.entrySet() )
{
doSomethingWith( entry.getKey(), entry.getValue() );
}

If one wants to avoid processing the same value more than once without regard
for the key:

for ( V value : new HashSet<V>( stuff.values() ))
{
doSomethingWith( value );
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top