Arunava Paul
Arunava Paul

Reputation: 111

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?

Upvotes: 3

Views: 94

Answers (2)

dbl
dbl

Reputation: 1109

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]

Upvotes: 2

Michael
Michael

Reputation: 44250

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);
});

Upvotes: 8

Related Questions