I am creating a Map
from a List
as follows:
List strings = Arrays.asList(\"a\", \"bb\", \"ccc\");
Map
Simple function to map array of objects by some field:
public static Map toLinkedHashMap(List list, Function someFunction) {
return list.stream()
.collect(Collectors.toMap(
someFunction,
myObject -> myObject,
(key1, key2) -> key1,
LinkedHashMap::new)
);
}
Map myObjectsByIdMap1 = toLinkedHashMap(
listOfMyObjects,
MyObject::getSomeStringField()
);
Map myObjectsByIdMap2 = toLinkedHashMap(
listOfMyObjects,
MyObject::getSomeIntegerField()
);