Question:
I'm capturing parameters from a request url using
com.apache.http.NameValuePair
which basically store those params in List<NameValuePair>
. To do certain checks and verifications on those params, I need to convert that list into a HashMap<String, String>
. Is there a way to do this conversion?Answer:
You can use it for Java 8
public static <K, V, T extends V> Map<K, V> toMapBy(List<T> list,
Function<? super T, ? extends K> mapper) {
return list.stream().collect(Collectors.toMap(mapper, Function.identity()));
}
And here's how you would use it on a List:
Map<Long, Product> productsById = toMapBy(products, Product::getId);
Follow the link:
No comments:
Post a Comment