请问 java 中有没有 stream 的方法或者使用别的算法之类的去完成这个逻辑?
我有: List<String> list; Map<String, Integer> keyMap;
如果 list 的值命中 keyMap 里的 key 了,就返回一个 List<Integer> valueList 这样
1
Nooooobycat 2023 年 1 月 16 日
|
2
superedlimited 2023 年 1 月 16 日 map.keyset().filter(el -> list.include(el)).map(el -> keyMap.get(el))
|
3
Leviathann 2023 年 1 月 16 日
list
filter get from map not null map |
4
issakchill 2023 年 1 月 16 日
public <T> List<T> getByKeyList(Map<Integer, T> map, List<Integer> idList) {
if (CollectionUtil.isEmpty(map) || CollectionUtil.isEmpty(idList)) { return new ArrayList<>(); } return idList.stream() .map(map::get) .filter(Objects::nonNull) .collect(Collectors.toList()); } |
5
KamL OP |