5.21. Retrieving Map Values Without Casting

Problem

You need to retrieve a primitive double from a Map, but the value is stored as a Double object.

Solution

Use MapUtils.getDoubleValue() to retrieve a Double object from a Map as a double primitive. The following example demonstrates getDoubleValue( ):

import java.util.*;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.collections.MapUtils;

Object[] mapArray = new Object[][] {
    { "A", new Double( 2.0 ) },
    { "B", new Double( 0.223242 ) },
    { "C", new Double( 2828e4 ) },
    { "D", "GARBAGE"} };

Map numberMap = ArrayUtils.toMap( mapArray );

double a = MapUtils.getDoubleValue( numberMap, "A" );
double b = MapUtils.getDoubleValue( numberMap, "B" );
double c = MapUtils.getDoubleValue( numberMap, "C" );
double d = MapUtils.getDoubleValue( numberMap, "D", new Double( Double.NaN ) );

System.out.println( "A = " + a );
System.out.println( "B = " + b );
System.out.println( "C = " + c );
System.out.println( "D = " + d );

This simple utility retrieves four doubles from numberMap; the fourth call to getDoubleValue( ) supplies a default double to be returned if the value’s type cannot be converted to a double. This example produces the following output:

A = 2.0
B = 0.223242
C = 28280.0
D = NaN

Discussion

This utility is laughably simple, but if you are working with numbers, this utility can help you avoid casting and calling doubleValue( ). In addition to MapUtils.getDoubleValue( ), MapUtils also contains the MapUtils.getDouble( ) method, which simply returns a Double object. The following example demonstrates the various utility methods for obtaining primitives and their corresponding object types from a Map:

import java.util.*;
import org.apache.commons.collections.MapUtils;

Map map = new HashMap( );
map.put( "A", new Long( 3.4 ) );
map.put( "B", new Short( 33 ) );
map.put( "C", "BLAH" );

Number aNumber = MapUtils.getNumber( map, "A" );
Long aLong = MapUtils.getLong( map, "A" );

long a = MapUtils.getLongValue( map, "A" );
short b = MapUtils.getShortValue( map, "B" );
Byte c = MapUtils.getByte( map, "C", Byte.MAX_VALUE );

As shown in the previous example, MapUtils contains utility methods for working with bytes, shorts, and long, among other primitive types.

Table 5-1 details several related methods for Boolean, Double, and Number, which can retrieve objects and primitive values from a Map. While Table 5-1 deals with Boolean and Double, you should be aware that similar methods exist for all primitive types and corresponding objects.

Table 5-1. Methods on MapUtils

Return type

Signature

Boolean

getBoolean(Map map, Object key)getBoolean(Map map, Object key, Boolean default)

boolean

getBooleanValue(Map map, Object key)getBooleanBalue(Map map, Object key, boolean default)

Double

getDouble(Map map, Object key)getDouble(Map map, Object key, Double default)

double

getDoubleValue(Map map, Object key)getDoubleValue(Map map, Object key, double default)

Number

getNumber(Map map, Object key)getNumber(Map map, Object key, Number default)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.212.160