当前位置: 首页 > news >正文

泛微 企业网站建设计划网站出现 503怎么了

泛微 企业网站建设计划,网站出现 503怎么了,版面设计软件,免费一键建站官网大全return userList.stream().filter(user -> {String tagsStr user.getTags(); 使用 Stream API 来过滤 userList 中的用户 解析 tagsStr 并根据标签进行过滤 假设 tagsStr 是一个 JSON 格式的字符串,存储了一个标签集合。你希望过滤出包含所有指定标签的用户。…
return userList.stream().filter(user -> {String tagsStr = user.getTags();

使用 Stream API 来过滤 userList 中的用户

解析 tagsStr 并根据标签进行过滤

假设 tagsStr 是一个 JSON 格式的字符串,存储了一个标签集合。你希望过滤出包含所有指定标签的用户。

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.*;
import java.util.stream.Collectors;public List<SafetyUser> filterUsersByTags(List<String> tagNameList) {// 查询所有用户QueryWrapper<User> queryWrapper = new QueryWrapper<>();List<User> userList = userMapper.selectList(queryWrapper);// 使用 Gson 解析 JSON 字符串Gson gson = new Gson();Type setType = new TypeToken<Set<String>>() {}.getType();// 过滤用户return userList.stream().filter(user -> {String tagsStr = user.getTags();if (StringUtils.isBlank(tagsStr)) {return false;}Set<String> tempTagNameSet = gson.fromJson(tagsStr, setType);tempTagNameSet = Optional.ofNullable(tempTagNameSet).orElse(new HashSet<>());return tagNameList.stream().allMatch(tempTagNameSet::contains);}).map(this::getSafetyUser).collect(Collectors.toList());
}
Set<String> tempTagNameSet = gson.fromJson(tagsStr, new TypeToken<Set<String>>() {}.getType());

使用了 Gson 来将一个 JSON 格式的字符串解析为一个 Set<String> 类型

假设 tagsStr 是一个 JSON 格式的字符串,存储了一个标签集合。你希望将这个字符串解析为一个 Set<String>

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.Set;public class Example {public static void main(String[] args) {// 示例 JSON 字符串String tagsStr = "[\"tag1\", \"tag2\", \"tag3\"]";// 创建 Gson 实例Gson gson = new Gson();// 使用 TypeToken 获取 Set<String> 的类型Type setType = new TypeToken<Set<String>>() {}.getType();// 解析 JSON 字符串为 Set<String>Set<String> tempTagNameSet = gson.fromJson(tagsStr, setType);// 输出结果System.out.println(tempTagNameSet);}
}
Set<String> tempTagNameSet = gson.fromJson(tagsStr, new TypeToken<Set<String>>() {}.getType());

你的代码片段中使用了 `Gson` 来将一个 JSON 格式的字符串解析为一个 `Set<String>` 类型。

### 完整代码示例

假设 `tagsStr` 是一个 JSON 格式的字符串,存储了一个标签集合。你希望将这个字符串解析为一个 `Set<String>`。

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.Set;public class Example {public static void main(String[] args) {// 示例 JSON 字符串String tagsStr = "[\"tag1\", \"tag2\", \"tag3\"]";// 创建 Gson 实例Gson gson = new Gson();// 使用 TypeToken 获取 Set<String> 的类型Type setType = new TypeToken<Set<String>>() {}.getType();// 解析 JSON 字符串为 Set<String>Set<String> tempTagNameSet = gson.fromJson(tagsStr, setType);// 输出结果System.out.println(tempTagNameSet);}
}

### 代码解释

1. **创建 Gson 实例**:

   Gson gson = new Gson();

   这里创建了一个 `Gson` 实例,用于后续的 JSON 解析。

2. **获取 `Set<String>` 的类型**:

   Type setType = new TypeToken<Set<String>>() {}.getType();

   - `TypeToken` 是一个辅助类,用于获取泛型类型的 `Type`。
   - `new TypeToken<Set<String>>() {}.getType()` 会返回一个 `Type` 对象,表示 `Set<String>` 类型。
   - 这是因为 Java 的泛型在运行时会被擦除,所以需要通过 `TypeToken` 来保留泛型信息。

3. **解析 JSON 字符串**:

   Set<String> tempTagNameSet = gson.fromJson(tagsStr, setType);

   - `gson.fromJson(tagsStr, setType)` 方法将 JSON 字符串 `tagsStr` 解析为 `Set<String>` 类型。
   - `tagsStr` 应该是一个 JSON 数组格式的字符串,例如 `["tag1", "tag2", "tag3"]`。

4. **处理空值或异常**:
   在实际应用中,你可能需要处理 `tagsStr` 为空或格式不正确的情况。可以添加一些额外的检查和异常处理:

 try {if (StringUtils.isBlank(tagsStr)) {tempTagNameSet = new HashSet<>();} else {tempTagNameSet = gson.fromJson(tagsStr, setType);}} catch (Exception e) {// 处理解析异常tempTagNameSet = new HashSet<>();System.err.println("Error parsing tags: " + e.getMessage());}

tempTagNameSet = Optional.ofNullable(tempTagNameSet).orElse(new HashSet<>());

这行代码使用了 `Optional` 类来处理可能为 `null` 的 `tempTagNameSet`。`Optional` 是 Java 8 引入的一个类,用于避免显式的 `null` 检查,使代码更加简洁和安全。

### 代码解释

- **`Optional.ofNullable(tempTagNameSet)`**:
  - 创建一个 `Optional` 对象,如果 `tempTagNameSet` 为 `null`,则返回一个空的 `Optional`;否则,返回一个包含 `tempTagNameSet` 的 `Optional`。

- **`.orElse(new HashSet<>())`**:
  - 如果 `Optional` 是空的(即 `tempTagNameSet` 为 `null`),则返回一个新的空 `HashSet`;否则,返回 `tempTagNameSet`。

### 作用

这行代码的作用是确保 `tempTagNameSet` 不会为 `null`。如果 `tempTagNameSet` 为 `null`,则用一个空的 `HashSet` 替代,避免后续代码中出现 `NullPointerException`。

### 示例

假设你从 JSON 字符串解析出的 `Set<String>` 可能为 `null`,你希望确保它不会为 `null`:

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.*;public class Example {public static void main(String[] args) {// 示例 JSON 字符串String tagsStr = "[\"tag1\", \"tag2\", \"tag3\"]";// 创建 Gson 实例Gson gson = new Gson();// 使用 TypeToken 获取 Set<String> 的类型Type setType = new TypeToken<Set<String>>() {}.getType();// 解析 JSON 字符串为 Set<String>Set<String> tempTagNameSet = null; // 假设解析失败,tempTagNameSet 为 nulltry {tempTagNameSet = gson.fromJson(tagsStr, setType);} catch (Exception e) {e.printStackTrace();}// 确保 tempTagNameSet 不为 nulltempTagNameSet = Optional.ofNullable(tempTagNameSet).orElse(new HashSet<>());// 输出结果System.out.println(tempTagNameSet);}
}

### 优化和注意事项

1. **异常处理**:
   在实际应用中,应该处理可能的异常,例如 JSON 格式错误或解析失败。


   try {
       tempTagNameSet = gson.fromJson(tagsStr, setType);
   } catch (Exception e) {
       log.error("Error parsing tags: {}", tagsStr, e);
       tempTagNameSet = new HashSet<>();
   }
 

2. **空值处理**:
   如果 `tagsStr` 为空或 `null`,应该提供一个默认值。


   if (StringUtils.isBlank(tagsStr)) {
       tempTagNameSet = new HashSet<>();
   } else {
       try {
           tempTagNameSet = gson.fromJson(tagsStr, setType);
       } catch (Exception e) {
           log.error("Error parsing tags: {}", tagsStr, e);
           tempTagNameSet = new HashSet<>();
       }
   }
 

3. **性能优化**:
   如果你需要频繁解析类似的 JSON 字符串,可以考虑缓存 `Gson` 实例,避免每次创建新的实例。

### 完整的过滤方法

结合你的需求,以下是一个完整的过滤方法示例:


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.*;
import java.util.stream.Collectors;public List<SafetyUser> filterUsersByTags(List<String> tagNameList) {// 查询所有用户QueryWrapper<User> queryWrapper = new QueryWrapper<>();List<User> userList = userMapper.selectList(queryWrapper);// 使用 Gson 解析 JSON 字符串Gson gson = new Gson();Type setType = new TypeToken<Set<String>>() {}.getType();// 过滤用户return userList.stream().filter(user -> {String tagsStr = user.getTags();Set<String> tempTagNameSet = new HashSet<>();try {if (StringUtils.isNotBlank(tagsStr)) {tempTagNameSet = gson.fromJson(tagsStr, setType);}} catch (Exception e) {log.error("Error parsing tags for user {}: {}", user.getId(), tagsStr, e);}tempTagNameSet = Optional.ofNullable(tempTagNameSet).orElse(new HashSet<>());return tagNameList.stream().allMatch(tempTagNameSet::contains);}).map(this::getSafetyUser).collect(Collectors.toList());
}

 

 

http://www.dtcms.com/a/488115.html

相关文章:

  • 兰州移动端网站建设如何做好网站站内优化
  • PS基本教学(二)——认识PS软件各个基础模块以及PS基本设置
  • 大模型Agent智能体:开启人工智能新时代
  • 常备资料查询
  • 20251015给荣品的PRO-RK3566开发板在buildroot下打开ov5645【只配置编译了】
  • 淄博网站排名公司苏州网页关键词优化
  • 网站设置密码访问一建二建报考条件及专业要求
  • 青岛市住房城乡建设厅网站php网站建设个人总结
  • 濮阳网站关键词网站做下载wordpress
  • 上海站优云网络科技有限公司简单的网站怎么做
  • django网站开发实例pdfseo交流群
  • 重庆建网站搜索快忻科技html代码注释
  • 如何做公司o2o网站网站制作杭州
  • Python自定义容器完全指南:从基础实现到高级模式
  • 小程序做网站济南网站建设方案咨询
  • 介绍近期github上有名的开源项目
  • 相应式手机网站建设网站可不可以做自己的专利
  • 网站建设菜鸟教程模板建站合同
  • PyQt5 串口上位机开发笔记:如何给界面更换图标
  • 响应式购物网站模板不想让网站保存密码怎么做
  • C#:函数默认参数
  • 比较指令 CMP 解析
  • 做设计接私活的网站优化近义词
  • 苏州知名网站制作设计保障性租赁住房管理平台
  • 今夕窗口批量启动排序以及窗口大小调整工具软件
  • 共建智能视觉生态,Deepano(嘀拍科技)授权世强硬创平台代理
  • HarmonyOS 用 attributeModifier 修改按钮背景但按压态不生效
  • 漳州城乡建设管理局网站贵州建设厅网站二建
  • 数字化科技简化移民流程的 5 种方式
  • DrvBsp_I2C驱动_EEPROM(二)