### Question:
StreamUtil { public static <T, K> Map<K, T> list2Map(@NonNull Collection<T> list, @NonNull Function<? super T, K> keyFunc) { return list.stream().collect(Collectors.toMap(keyFunc, Function.identity(), (u, v) -> { throw new IllegalStateException(String.format("Multiple entries with same key,%s=%s,%s=%s", keyFunc.apply(u), u, keyFunc.apply(v), v)); }, HashMap::new)); }  static Map<K, T> list2Map(@NonNull Collection<T> list, @NonNull Function<? super T, K> keyFunc);  }### Answer:
@Test public void list2Map() { Map<String, Person> byName = StreamUtil.list2Map(persons, Person::getName); System.out.println("java8 stream: " + byName); byName = Maps.uniqueIndex(persons, Person::getName); System.out.println("maps util: " + byName); System.out.println("====key重复的错误信息提示===="); try { StreamUtil.list2Map(persons, Person::getLocation); } catch (Exception ex) { ex.printStackTrace(System.out); } try { Maps.uniqueIndex(persons, Person::getLocation); } catch (Exception ex) { ex.printStackTrace(System.out); } }