public static < S , T > Map < S , T > compactAndLockHashMap ( HashMap < S , T > map ) { if ( map == null || map . isEmpty ( ) ) { return Collections . emptyMap ( ) ; } if ( map . size ( ) == 1 ) { Map . Entry < S , T > stEntry = map . entrySet ( ) . iterator ( ) . next ( ) ; return Collections . singletonMap ( stEntry . getKey ( ) , stEntry . getValue ( ) ) ; } Map < S , T > newMap = new HashMap < S , T > ( map . size ( ) , 1 ) ; newMap . putAll ( map ) ; return Collections . unmodifiableMap ( newMap ) ; }
Returns a compacted and locked map representing the map passed in . This method can freely change the implementation type of the map . I . e . it can return an emptyMap singletonMap or even a completely different map implementation .