package com.mindprod.example;
import java.util.HashMap;
import java.util.Iterator;
import static java.lang.System.*;
/**
* example use of java.util.HashMap. How to iterate through a HashMap removing some of the elements.
* <p/>
* Requires JDK 1.5+.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2008-06-19
* @since 2008-06-19
*/
@SuppressWarnings( { "UnusedAssignment" } )
public final class TestHashMapRemove
{
/**
* How to iterate through a HashMap removing some of the elements.
*
* @param args not used
*/
@SuppressWarnings( "unchecked" )
public static void main( String[] args )
{
HashMap<String, String> h = new HashMap<>( 149
,
0.75f
);
h.put( "WA", "Washington" );
h.put( "NY", "New York" );
h.put( "RI", "Rhode Island" );
h.put( "CA", "California" );
h.put( "BC", "British Columbia" );
h.put( "OR", "Oregon" );
h.remove( "CA" );
for ( Iterator<String> iter = h.values().iterator(); iter.hasNext(); )
{
String item = iter.next();
if ( item.indexOf( ' ' ) > 0 )
{
iter.remove();// avoids ConcurrentModificationException
}
}
for ( String stateAbbr : h.keySet() )
{
out.println( stateAbbr );
}
}
}