Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving from Map

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?

like image 815
Arunava Paul Avatar asked Dec 10 '25 17:12

Arunava Paul


2 Answers

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);
});
like image 83
Michael Avatar answered Dec 12 '25 06:12

Michael


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]

like image 39
dbl Avatar answered Dec 12 '25 06:12

dbl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!