protected < K , V > Map < K , V > generifyMap ( final Map < Object , Object > map , final Class < K > keyType , final Class < V > valueType ) { if ( keyType == String . class ) { // only value type is changed, we can make value replacements for ( Map . Entry < Object , Object > entry : map . entrySet ( ) ) { Object value = entry . getValue ( ) ; Object newValue = convert ( value , valueType ) ; if ( value != newValue ) { entry . setValue ( newValue ) ; } } return ( Map < K , V > ) map ; } // key is changed too, we need a new map Map < K , V > newMap = new HashMap <> ( map . size ( ) ) ; for ( Map . Entry < Object , Object > entry : map . entrySet ( ) ) { Object key = entry . getKey ( ) ; Object newKey = convert ( key , keyType ) ; Object value = entry . getValue ( ) ; Object newValue = convert ( value , valueType ) ; newMap . put ( ( K ) newKey , ( V ) newValue ) ; } return newMap ; }
Change map elements to match key and value types .