请问 java 中有没有 stream 的方法或者使用别的算法之类的去完成这个逻辑?
我有: List<String> list; Map<String, Integer> keyMap;
如果 list 的值命中 keyMap 里的 key 了,就返回一个 List<Integer> valueList 这样
请问 java 中有没有 stream 的方法或者使用别的算法之类的去完成这个逻辑?
我有: List<String> list; Map<String, Integer> keyMap;
如果 list 的值命中 keyMap 里的 key 了,就返回一个 List<Integer> valueList 这样
|
NooooobycatJan 16, 2023
1
|
|
2
map.keyset().filter(el -> list.include(el)).map(el -> keyMap.get(el))
|
|
LeviathannJan 16, 2023
3
list
filter get from map not null map |
|
issakchillJan 16, 2023
4
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
|