0

My issue is this, I have hashMap

//for (String tablaBuscar : listaTablas) {
        // RECUPERAR REGISTROS POR TABLA
    HashMap<String,String> listaRegistros =(HashMap<String,String>) RequerimientosTablaDestino
                .getListaLineasPorTablaYColumna(idCabecera,idCriterio);

when I want to iterate (I have tried several ways, this is the last one)

Iterator<String>  entradaMap =  listaRegistros.keySet().iterator();
Iterator<String> valorMap =  listaRegistros.values().iterator();
while(entradaMap.hasNext()&&valorMap.hasNext()) {
    String elemento=entradaMap.next().toString();
    int registro =(Integer.parseInt(elemento));
    String tablaBuscar = (String)valorMap.next();

in "int registro" line I get "cannot cast from BigDecimal to Int" Debugging I have seen that ALLWAYS the key of the hash map is "big decimal type", no matter the way I have created it.

If I try to use it as a BigDecimal as seen in debugging time, I get compilations failures because "type missmatch" "cannot cast" etc...

So the compiler thinks that the key is the type I have put, but at runtime the program thinks that te key is allways a bigDecimal.

Any idea? thanks

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    Please post a short but *complete* program demonstrating the problem. I can't see how you'd get the problem you're reporting on the line you're reporting it... – Jon Skeet Nov 02 '15 at 14:03

2 Answers2

0

If your key set only contains String values parseable as BigDecimal, you can easily retrieve an actual BigDecimal object from the keys by iterating them and using the constructor taking a String argument:

for (String key: myMap.keySet()) {
    BigDecimal converted = new BigDecimal(key);
}

Also as shown, no need for a double iteration through Iterator on both keys and values.

If you want an additional layer of validation, you can catch a NumberFormatException when initializing your BigDecimal, e.g. new BigDecimal("foo") would throw one.

Also, you can retrieve the integer value of your BigDecimal objects by invoking BigDecimal#intValue or BigDecimal.intValueExact.

Mena
  • 47,782
  • 11
  • 87
  • 106
  • Thanks, guys. The issue happened because ibatis parametrization has not the java type in resultMap. And (Idon't know why) it takes key as BigDecimal when is declared as String. – Miguel Abreu Nov 02 '15 at 15:35
0

I just ran into this same issue. Miguel alluded to the solution in his comment on Mena's answer, but to be more explicit the fix involves adding a parameter to your result mapper to force the return of a String:

<resultMap type="java.util.HashMap" id="dataTypeMap">
    <result column="DATA_TYPE_ID" javaType="java.lang.String" property="id" />
    <result column="DATA_TYPE" property="name" />
</resultMap>
Ethan Shepherd
  • 569
  • 3
  • 13