I've just started looking at Java 8 and to try out lambdas, I have an use case for the above problem, which I am solving using the usual for loop which is very lengthy and hard to read
My Existing code
private static Map<DataType, List<OperatorType>> buildmap() {
Map<DataType, List<OperatorType>> map = Maps.newHashMap();
for (OperatorType type : OperatorType.values()) {
List<DataType> supportedTypes = type.getSupportedtypes();
supportedTypes.forEach(datatype -> {
if (map.containsKey(datatype)) {
List<OperatorType> list = map.get(datatype);
list.add(type);
map.put(datatype,
list);
} else {
map.put(datatype,
new ArrayList<OperatorType>() {
private static final long serialVersionUID = 1L;
{
add(type);
}
});
}
});
}
return Collections.unmodifiableMap(new LinkedHashMap<>(map));
}