stream.map 排除null
时间: 2023-08-24 19:08:16 浏览: 185
你可以使用 `filter` 方法来排除流中的 `null` 值。下面是一个示例:
```java
stream.filter(Objects::nonNull)
.map(/* 在这里进行你的映射操作 */);
```
这段代码会首先过滤掉流中的 `null` 值,然后再执行 `map` 操作。你可以根据需要在 `map` 中进行具体的映射操作。
相关问题
stream流list转map排除null
将Stream流中的元素转换为Map时可以使用如下代码:
```
List<String> list = Arrays.asList("one", "two", null, "three", null);
Map<String, Integer> map = list.stream()
.filter(Objects::nonNull)
.collect(Collectors.toMap(Function.identity(), String::length));
```
这段代码将会过滤掉List中的null元素,然后将列表中的元素作为Key,长度作为Value存储到Map中。
for (DatabaseType d : dataTypes) { DatabaseContextHolder.setDatabaseType(d); List<String> bankPaidOutRefNbrs = unionLoanMapper.getCustPaidOutRefNbrsUnionLoan(); if(!CollectionUtils.isEmpty(bankPaidOutRefNbrs)){ DatabaseContextHolder.setDatabaseType(d); List<PaidOutRemainBalVo> unionBankBals = unionLoanMapper.getUnionBalByRefNbrs(bankPaidOutRefNbrs); List<String> refNbrs = unionBankBals.stream().map(p -> p.getRefNbr()).collect(Collectors.toList()); DatabaseContextHolder.setDatabaseType(d); List<String> waiveRefNbrs = new ArrayList<>(); if (!CollectionUtils.isEmpty(refNbrs)){ waiveRefNbrs = unionLoanMapper.getUnionWaiveRefNbr(refNbrs); } Iterator<PaidOutRemainBalVo> iterator = unionBankBals.iterator(); while (iterator.hasNext()) { PaidOutRemainBalVo vo = iterator.next(); //排除豁免未入账 if (!CollectionUtils.isEmpty(waiveRefNbrs)) { if (waiveRefNbrs.contains(vo.getRefNbr())) { iterator.remove(); continue; } } //去除三峡本金为负数的告警 if (vo.getCoopId() != null && vo.getCoopId().contains("SXBANK") && vo.getPrincipalBal().compareTo(BigDecimal.ZERO) < 0) { iterator.remove(); } } if (!CollectionUtils.isEmpty(unionBankBals)){ unionBalsAll.addAll(unionBankBals); } } } 用多线程实现该循环
可以使用Python内置的`concurrent.futures`模块中的`ThreadPoolExecutor`来实现多线程循环。具体代码如下:
```python
from concurrent.futures import ThreadPoolExecutor
from functools import partial
def process_data(d, data):
DatabaseContextHolder.setDatabaseType(d)
bankPaidOutRefNbrs = unionLoanMapper.getCustPaidOutRefNbrsUnionLoan()
if not bankPaidOutRefNbrs:
return []
DatabaseContextHolder.setDatabaseType(d)
unionBankBals = unionLoanMapper.getUnionBalByRefNbrs(bankPaidOutRefNbrs)
refNbrs = [p.getRefNbr() for p in unionBankBals]
DatabaseContextHolder.setDatabaseType(d)
waiveRefNbrs = unionLoanMapper.getUnionWaiveRefNbr(refNbrs) if refNbrs else []
result = []
for vo in unionBankBals:
if waiveRefNbrs and vo.getRefNbr() in waiveRefNbrs:
continue
if vo.getCoopId() and "SXBANK" in vo.getCoopId() and vo.getPrincipalBal() < 0:
continue
result.append(vo)
return result
dataTypes = [...]
with ThreadPoolExecutor(max_workers=2) as executor:
func = partial(process_data, data=dataTypes)
results = executor.map(func, dataTypes)
unionBalsAll = []
for result in results:
if result:
unionBalsAll.extend(result)
```
以上代码中,我们将原来的循环拆分成了一个`process_data`函数,并使用`partial`函数将`dataTypes`作为参数传递给该函数。接着,创建一个`ThreadPoolExecutor`实例,设置最大工作线程数为2。使用`map`方法将`process_data`函数提交给线程池,并传入`dataTypes`参数,得到一个生成器对象`results`。最后,遍历`results`并将结果存入`unionBalsAll`中。这样就实现了多线程循环。
阅读全文