sunmingtao / sample-code

3 stars 4 forks source link

Convert nullable map to list #155

Closed sunmingtao closed 4 years ago

sunmingtao commented 4 years ago
List<NameValuePair> arguments = new ArrayList<>();
if (params != null) {
    for (Map.Entry<String, String> entry : params.entrySet()) {
        arguments.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    }
}
return arguments;

convert to

return Optional.ofNullable(params)
            .orElse(Collections.emptyMap())
            .entrySet()
            .stream()
            .map(e -> new BasicNameValuePair(e.getKey(), e.getValue())
            .collect(Collectors.toList());