CollectionUtils { public static <X, Y, Z> Map<Y, Map<X, Z>> exchangeKeys(Map<X, Map<Y, Z>> map) { return map.entrySet() .stream() .flatMap(xyz -> xyz.getValue() .entrySet() .stream() .map(yz -> new ImmutablePair<>(yz.getKey(), new ImmutablePair<>(xyz.getKey(), yz.getValue()))) ).collect(groupingBy(ImmutablePair::getLeft, mapping(ImmutablePair::getRight, toMap(ImmutablePair::getLeft, ImmutablePair::getRight)))); } private  CollectionUtils(); static List<String> removeAllByStrWithSeparator(List<String> originList,
                                                           String strWithSeparator, String separator); static Object[] intersection(Object[] a, Object[] b); static Collection<T> intersection(Collection<T> a, Collection<T> b); static Object getDuplicate(Collection<E> coll, E o, String field, Class fieldClass); static Object getDuplicate(Collection<E> coll, E o, String fieldName,
                                          Class fieldClass, Class elementClass); static Collection<T> remove(Collection<T> coll, final String field, final Object... removes); static int getNthNumberMin(int[] arr, int n); static int getNthNumberMax(int[] arr, int n); static Map<Y, Map<X, Z>> exchangeKeys(Map<X, Map<Y, Z>> map); static String join(T[] arr, String separator); static String join(Collection<T> list, String separator);  }
@Test public void exchangeKeysTest() { Map<String, Map<Long, String>> map = Maps.newHashMap(); Map<Long, String> internalMap = Maps.newHashMap(); internalMap.put(1L, "1L"); internalMap.put(2L, "2L"); Map<Long, String> internalMap2 = Maps.newHashMap(); internalMap2.put(2L, "2L"); internalMap2.put(3L, "3L"); map.put("1", internalMap); map.put("2", internalMap2); Map<Long, Map<String, String>> aim = CollectionUtils.exchangeKeys(map); assertEquals("{1:{\"1\":\"1L\"},2:{\"1\":\"2L\",\"2\":\"2L\"},3:{\"2\":\"3L\"}}", JSON.toJSONString(aim)); }