The keys of the map may be values 1 - 5 where each key is not required, but if absent must concatenate something that looks like " - [] ".
I've used Java 8 a bit before and I feel like this can be done more efficiently with stream() and collect() but I feel like the external list requirement is throwing me off. The below code works but feels incorrect.
List<String> ids = Arrays.asList("1", "2", "3", "4", "5");
StringBuilder location = new StringBuilder();
ids.forEach(id -> {
List<String> addresses = map.get(id);
if (addresses != null) {
addresses.forEach(addr -> location.append(addr + " - "));
} else {
location.append(" [] - ");
}
});
Generates something that looks like this for a Colorado location:
US - CO - [] - CENTENNIAL -
In answer to Nicholas K an example of map would be
{1=[US], 2=[CO], 5=[METROPOLITAN FOOTBALL STADIUM DISTRICT, REGIONAL TRANSPORTATION DISTRICT]}
and would be expected to receive output
US - CO - [] - [] - METROPOLITAN FOOTBALL STADIUM DISTRICT - REGIONAL TRANSPORTATION DISTRICT
map?stream()andcollect()will be more efficient, write the code in the way you're most comfortable with.