I have a map of some values like this:
Map<String, String> roleMap = new HashMap<>();
map.put(1,"A");
map.put(2,"B;C;D");
map.put(3,"A;C");
And the list of some restricted values:
List<String> restrictedList= List.of("B", "C");
I need to check if the value "A" is presented in each key of the map and not together with one of the values from restricted list, then throw some error.
I succeeded to do this but think it can be done better with streams fully and not a half job.
This is my solution:
map.forEach((key, value) -> {
if (Arrays.stream(value.toString().split(";"))
.anyMatch(role -> "A".contains(role))
&& CollectionUtils.containsAny(Arrays.asList(value.toString().split(";")), restrictedList)) {
errorsList.add(String.format("%s combination can't be used with 'A'", value))
};
});
The result of above scenario should output that key number 3 is invalid because only key number 3 contains "A and also some value from restricted list.
But I wonder how to implement it with streams?
I tried with filter() before anyMatch() but it doesn't work.