检查地图值是否包含特定字符串和限制列表中的元素
我有一些这样的值的地图:
Map<String, String> roleMap = new HashMap<>();
map.put(1,"A");
map.put(2,"B;C;D");
map.put(3,"A;C");
以及一些限制值的列表:
List<String> restrictedList= List.of("B", "C");
我需要检查值是否在地图的每个键中显示了“ a” a“ a”
,而不是与其中一个一起显示从限制列表中的值,然后引发一些错误。
我成功地做到了这一点,但认为这可以通过流完全而不是一半的工作来做得更好。
这是我的解决方案:
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))
};
});
上述方案的结果应输出该密钥编号3
无效,因为仅密钥编号3
包含“ a
以及一些从限制性列表中进行值,
但我想如何
用filter()
在anymatch()
之前尝试过它。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在地图的值上创建流,并在其上应用
filter()
操作两次:“ a” a“ a“ a“ a“ a“ a“ a“ a“ a” a”)包含在地图值中;
两项检查都是
AnyMatch()
操作的制作。为了提高对受限制列表的检查的性能,它是用
HashSet
包裹的。正在通过
split()
从每个值中创建一个字符串数组,以减少开销。目标值(<代码>“ a” ),错误消息和定界符(
“;”
)被动态地传递给该方法,而不是硬编码。该代码可能看起来像这样:
main()
- emo输出
You can create a stream over the values of the map and apply
filter()
operation on it twice:"A"
) in your code is contained in the map value;Both checks are making of
anyMatch()
operation.In order to improve performance of checking against the restricted lists, it's being wrapped with a
HashSet
.A string array is being created via
split()
operation from each value only once to reduce the overheads.Target value (
"A"
), error message and delimiter (";"
) are being passed to the method dynamically and not hard-coded.The code might look like this:
main()
- demoOutput
您可以执行下面的操作来过滤根据申请的无效值的键,并将这些键收集到列表/集合:
如果要避免两个过滤器并调用
arrays.aslist
两次,您可以提取下面的谓词:You could do something like below to filter the keys which have invalid values according to your requierments and collect those keys to a list/set:
If you want to avoid two filters and calling
Arrays.asList
twice, you could extract a predicate like below:我建议将您的状况检查到
isInrestrictallist
方法。然后,您可以使用流。
过滤
地图
以非常简单和干净的方式:errorslist的输出
is键:3 ,A; C组合不能与'a'
一起使用I recommend extracting your condition checking to
isInRestrictedList
method.Then you can use stream.
filter
map
in a very simple and clean way like this:The output of
errorsList
iskey: 3, A;C combination can't be used with 'A'