Java tutorial
//package com.java2s; //License from project: Apache License import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; public class Main { public static <T> Map<String, T> toMap(List<T> list, Function<T, String> getId) { return stream(list).collect(Collectors.toMap(getId, Function.identity())); } public static <T> Stream<T> stream(Collection<T> collection) { return Optional.ofNullable(collection).orElseGet(Collections::emptyList).stream(); } }