public static List<SysDictData> getDictCache(String key) { JSONArray arrayCache = SpringUtils.getBean(RedisService.class).getCacheObject(getCacheKey(key)); if (StringUtils.isNotNull(arrayCache)) { return arrayCache.toList(SysDictData.class); } return null; }
时间: 2024-04-21 08:27:21 浏览: 19
这段代码是获取缓存中的字典数据。方法接收一个参数 key,表示要获取的字典数据的类型。首先通过 SpringUtils.getBean() 方法获取 RedisService 的实例,然后调用该实例的 getCacheObject() 方法从缓存中获取数据。如果获取到了缓存数据,则将其转换为 SysDictData 类型的 List,并返回该 List。如果没有获取到缓存数据,则返回 null。需要注意的是,该方法中调用了 StringUtils.isNotNull() 方法,该方法的作用是判断字符串是否不为空。
相关问题
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表达式,同时避免多次解析JSON字符串,代码如下:
```
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 = new SchemaRowRule();
BeanUtils.copyProperties(schemaRowRuleEo, schemaRowRule);
return schemaRowRule;
})
.collect(Collectors.toList());
List<SchemaColumnRule> userColumnRuleList = schemaColumnRuleList.stream()
.filter(rule -> {
List<String> userList = Optional.ofNullable(rule.getColumnPermissionUserList())
.map(jsonArray -> JSONArray.parseArray(jsonArray, String.class))
.orElse(Collections.emptyList());
List<String> orgList = Optional.ofNullable(rule.getColumnPermissionOrgList())
.map(jsonArray -> JSONArray.parseArray(jsonArray, String.class))
.orElse(Collections.emptyList());
return userList.contains(userUid) || orgList.contains(orgCode);
})
.map(BeanUtils::instantiate)
.collect(Collectors.toList());
```
其中,使用了`Optional`来避免`null`值的判断和处理,使用`BeanUtils::instantiate`方法引用来创建对象并复制属性。
更改代码错误List<Map<String,String>> mapList = JSONArray.parseArray(list.toString(), Map.class)
对不起,我的回答有误。在Java中,如果要将JSON字符串转换为List<Map<String, String>>类型,可以使用以下代码:
```java
String jsonStr = "[{'name':'Alice', 'age':18}, {'name':'Bob', 'age':20}]";
List<Map<String, String>> mapList = new ArrayList<>();
JSONArray jsonArray = JSONArray.parseArray(jsonStr);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Map<String, String> map = new HashMap<>();
for (String key : jsonObject.keySet()) {
map.put(key, jsonObject.getString(key));
}
mapList.add(map);
}
```
其中,首先使用JSONArray.parseArray()方法将JSON字符串转换为JSONArray对象,然后遍历JSONArray对象的每个元素,将其转换为Map<String, String>类型,并添加到List中。请注意,上述代码中使用了FastJson库来解析JSON字符串。如果你使用的是其他JSON库,代码可能会有所不同。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)