@Test public void convertListWithNameAndObject() { String key1 = "key1"; String key2 = "key2"; TestObject testObject1 = new TestObject(key1, "something"); TestObject testObject2 = new TestObject(key2, "something"); Map<String, String> mapWithNameAndObjectKey = DataStructureUtils.mapToMap(List.of(testObject1, testObject2), TestObject::getName, TestObject::getDesc); assertEquals(2, mapWithNameAndObjectKey.size()); assertTrue(mapWithNameAndObjectKey.containsKey(key1)); assertTrue(mapWithNameAndObjectKey.containsKey(key2)); assertEquals(testObject1.getDesc(), mapWithNameAndObjectKey.get(key1)); assertEquals(testObject2.getDesc(), mapWithNameAndObjectKey.get(key2)); }
public static <K, V, C> Map<K, V> mapToMap(Collection<C> valueCollection, Function<C, K> keyExtractor, Function<C, V> valueExtractor) { return valueCollection.stream().collect(Collectors.toMap(keyExtractor::apply, valueExtractor::apply)); }
DataStructureUtils { public static <K, V, C> Map<K, V> mapToMap(Collection<C> valueCollection, Function<C, K> keyExtractor, Function<C, V> valueExtractor) { return valueCollection.stream().collect(Collectors.toMap(keyExtractor::apply, valueExtractor::apply)); } }
DataStructureUtils { public static <K, V, C> Map<K, V> mapToMap(Collection<C> valueCollection, Function<C, K> keyExtractor, Function<C, V> valueExtractor) { return valueCollection.stream().collect(Collectors.toMap(keyExtractor::apply, valueExtractor::apply)); } private  DataStructureUtils(); }
DataStructureUtils { public static <K, V, C> Map<K, V> mapToMap(Collection<C> valueCollection, Function<C, K> keyExtractor, Function<C, V> valueExtractor) { return valueCollection.stream().collect(Collectors.toMap(keyExtractor::apply, valueExtractor::apply)); } private  DataStructureUtils(); static Map<K, V> mapToValues(Collection<V> valueCollection, Function<V, K> keyExtractor); static Map<K, V> mapToKeys(Collection<K> valueCollection, Function<K, V> valueExtractor); static Map<K, V> mapToMap(Collection<C> valueCollection, Function<C, K> keyExtractor, Function<C, V> valueExtractor); }
DataStructureUtils { public static <K, V, C> Map<K, V> mapToMap(Collection<C> valueCollection, Function<C, K> keyExtractor, Function<C, V> valueExtractor) { return valueCollection.stream().collect(Collectors.toMap(keyExtractor::apply, valueExtractor::apply)); } private  DataStructureUtils(); static Map<K, V> mapToValues(Collection<V> valueCollection, Function<V, K> keyExtractor); static Map<K, V> mapToKeys(Collection<K> valueCollection, Function<K, V> valueExtractor); static Map<K, V> mapToMap(Collection<C> valueCollection, Function<C, K> keyExtractor, Function<C, V> valueExtractor);  }