I have the following Map<String, Set<String>>
:
{upload=[ADMIN], configure.accounts=[ADMIN, CONSULT], consult=[ADMIN, CONSULT], configure.merchants=[MANAGER, ADMIN], configure.invoices=[MANAGER, ADMIN], graphics=[MANAGER, ADMIN]}
and I want to get a structure Set<String>
like the following:
[ADMIN, CONSULT, MANAGER]
where the data is filtered obtaining all the possible values of the Set inside the Map. I can do this by going through the map and comparing the values so that it adds those that are not inside the Set
, is there a function to do this?
We can get a collection of sets from the map using the
values()
Map interface method. After this, we can iterate through the returned collection and add all the elements from each of the collection's sets to the set we're interested in getting as a result. The code would look more or less like the following:In java 8 it can be solved as follows: