I have a map like below:-
HashMap<String, Set<String>> mapList;
I'm retrieving the data like below:-
mapList.forEach((k, v) -> {
System.out.println("URL" + k);
Set<String> s = mapList.get(k);
s.forEach(e -> {
System.out.print(e);
});
});
Is there a better way to do this?
You can use a method reference for the second forEach, and you are doing an unnecessary mapList.get - you already have the value.
forEach((k, v) -> {
System.out.println("URL" + k);
v.forEach(System.out::print);
});
I think that you are looking for:
mapList.forEach((k, v) -> System.out.println("URL " + k + ", values : " + v)));
which will output the following:
URL http://url1, values: [a, b]
URL http://url2, values: [c, d]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With