list<String>生成Supplier<list<String>>
时间: 2023-03-30 10:03:25 浏览: 131
可以使用Stream的map方法将List<String>转换为Supplier<List<String>>,示例代码如下:
List<String> list = Arrays.asList("a", "b", "c");
Supplier<List<String>> supplier = () -> new ArrayList<>(list);
List<Supplier<List<String>>> supplierList = Collections.singletonList(supplier);
```java
相关问题
List<SchemaRowRule> userRowRuleList = schemaRowRuleList.stream() .filter(schemaRowRuleEo -> { List<String> userList = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionUserList(), String.class); List<String> orgList = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionOrgList(), String.class); return userList.contains(userUid) || orgList.contains(orgCode); }) .map(schemaRowRuleEo -> { SchemaRowRule schemaRowRule = SchemaRowRule.builder().build(); BeanUtils.copyProperties(schemaRowRuleEo, schemaRowRule); return schemaRowRule; }) .collect(Collectors.toList()); List<SchemaColumnRule> userColumnRuleList = schemaColumnRuleList.stream() .filter(rule -> { if (StringUtils.isNotEmpty(rule.getColumnPermissionUserList())) { List<String> userList = JSONArray.parseArray(rule.getColumnPermissionUserList(), String.class); return userList.contains(userUid); } else if (StringUtils.isNotEmpty(rule.getColumnPermissionOrgList())) { List<String> orgList = JSONArray.parseArray(rule.getColumnPermissionOrgList(), String.class); return orgList.contains(orgCode); } return false; }) .map(rule -> { SchemaColumnRule columnRule = new SchemaColumnRule(); BeanUtils.copyProperties(rule, columnRule); return columnRule; }) .collect(Collectors.toList());代码优化
可以将一些重复的代码提前抽象出来,例如解析字符串为列表的部分可以提取为一个方法,同时可以使用方法引用的方式替换Lambda表达式,代码如下:
```
private <T> List<T> copyProperties(List<T> sourceList, Supplier<T> targetSupplier) {
return sourceList.stream()
.map(source -> {
T target = targetSupplier.get();
BeanUtils.copyProperties(source, target);
return target;
})
.collect(Collectors.toList());
}
...
List<String> userList = parseJsonArray(schemaRowRuleEo.getRowPermissionUserList());
List<String> orgList = parseJsonArray(schemaRowRuleEo.getRowPermissionOrgList());
List<SchemaRowRule> userRowRuleList = schemaRowRuleList.stream()
.filter(schemaRowRuleEo -> userList.contains(userUid) || orgList.contains(orgCode))
.collect(Collectors.toList());
List<SchemaColumnRule> userColumnRuleList = schemaColumnRuleList.stream()
.filter(rule -> {
List<String> userList = parseJsonArray(rule.getColumnPermissionUserList());
List<String> orgList = parseJsonArray(rule.getColumnPermissionOrgList());
return userList.contains(userUid) || orgList.contains(orgCode);
})
.collect(Collectors.toList());
List<SchemaRowRule> copiedRowRuleList = copyProperties(userRowRuleList, SchemaRowRule::new);
List<SchemaColumnRule> copiedColumnRuleList = copyProperties(userColumnRuleList, SchemaColumnRule::new);
...
private List<String> parseJsonArray(String jsonArray) {
if (StringUtils.isNotEmpty(jsonArray)) {
return JSONArray.parseArray(jsonArray, String.class);
}
return Collections.emptyList();
}
```
public class PageHelperUtil { public static <T> Page<T> startPage(PageSortInterface pageSort, Supplier<List<T>> supplier) { PageHelper.startPage(pageSort.getPageNum(), pageSort.getPageSize(), pageSort.getSort() + " " + pageSort.getOrder()); List<T> list = supplier.get(); PageInfo<T> pageInfo = new PageInfo<>(list); Page<T> page = new Page<>(); page.setContent(list); page.setTotal(pageInfo.getTotal()); page.setPageNum(pageInfo.getPageNum()); page.setPageSize(pageInfo.getPageSize()); page.setSort(pageSort.getSort()); page.setOrder(pageSort.getOrder()); return page; } }如果是有条件的查询分页,该如何修改?
如果是有条件的查询分页,可以在 `startPage` 方法中增加一个参数,用于传递查询条件,然后在 `supplier` 中根据条件进行查询,例如:
```
public static <T> Page<T> startPage(PageSortInterface pageSort, Supplier<List<T>> supplier, Object condition) {
PageHelper.startPage(pageSort.getPageNum(), pageSort.getPageSize(), pageSort.getSort() + " " + pageSort.getOrder());
List<T> list = supplier.get(condition);
PageInfo<T> pageInfo = new PageInfo<>(list);
Page<T> page = new Page<>();
page.setContent(list);
page.setTotal(pageInfo.getTotal());
page.setPageNum(pageInfo.getPageNum());
page.setPageSize(pageInfo.getPageSize());
page.setSort(pageSort.getSort());
page.setOrder(pageSort.getOrder());
return page;
}
```
在 `supplier` 中接收条件参数,例如:
```
public List<User> getUsersByCondition(Map<String, Object> condition) {
return userDao.getUsersByCondition(condition);
}
```
这样就可以根据条件进行分页查询了。
阅读全文