【发布时间】:2019-12-28 17:59:16
【问题描述】:
我想使用方法参数中的键从另一个 TreeMap 创建一个过滤后的 TreeMap,但我还想测试这些值?
我试过这个:
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,我想抛出异常
【问题讨论】:
-
你的意思是,你只需要
idsMap ? -
stream.filter(i -> i < 808).boxed()...? -
如果 id 为 > 807 是否可以抛出异常
-
if (Instream.of(ids).anyMatch(id -> id > 807)) { throw new IllegalArgumentException("..."); }docs.oracle.com/javase/8/docs/api/java/util/stream/…
标签: java dictionary lambda filter java-stream