public static < X , Y > Map < Y , X > invert ( Map < X , Y > map ) { Map < Y , X > invertedMap = new HashMap < Y , X > ( ) ; for ( Map . Entry < X , Y > entry : map . entrySet ( ) ) { X key = entry . getKey ( ) ; Y value = entry . getValue ( ) ; invertedMap . put ( value , key ) ; } return invertedMap ; }
Inverts a map x - > y to a map y - > x assuming unique preimages . If they are not unique you get an arbitrary ones as the values in the inverted map .