我可以获取地图的一部分并测试我使用的密钥吗?



我想使用方法参数中的键从另一个树状图创建过滤树状图,但我也想测试这些值?

我试过这个:

public TreeMap<Integer, List<String>> getInfoById(int... ids) {
try (IntStream stream = Arrays.stream(ids)) {
return stream.boxed().collect(
Collectors.toMap(
i -> i,
info::get, //Info is a TreeMap<Integer, List<String>>
(i1, i2) -> {
throw new IllegalStateException("Two keys are similar");
},
TreeMap::new
)
);
}
}

但我想确保在ids不会有超过 807 的数字(因为总是有 807 键,所以我不希望有人寻找 808 键(

如果我的流不包含超过 807 的 int,我可以添加什么来查看

编辑:更准确地说,如果其中一个 id 超过 807,我想抛出一个异常

注释中给出的答案,但是: 如果我想只是过滤而不回溯它,我应该使用

stream.filter(i -> i < 808).boxed()...

但是如果发生这种情况,我想抛出一个错误,所以我用了

if (Instream.of(ids).anyMatch(id -> id > 807)) { throw new IllegalArgumentException("..."); }

最新更新