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

网站的关于我们怎么做seo专员招聘

网站的关于我们怎么做,seo专员招聘,闲置服务器做网站挣钱,怎样建设淘宝网站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/wzjs/273190.html

相关文章:

  • 做网站宣传有用吗百度的主页
  • 权威的南通网站建设seo有名气的优化公司
  • 推进政府门户网站建设的意义网络优化工程师简历
  • 中国室内设计任务网长春seo排名扣费
  • 公司名高端大气不重名安徽seo网络推广
  • 电脑免费安装wordpress深圳网站优化
  • 网站正在建设中 html源码短视频关键词seo优化
  • 网站标题关键词描述手机优化大师哪个好
  • 杭州 高端网站建设 推荐百度收录接口
  • 宜州设计公司长沙百家号seo
  • 专业网站建设方案优化排名推广技术网站
  • 设计师需要了解的网站企业seo关键字优化
  • 苹果14pro max价格推荐一个seo优化软件
  • 五合一建站百度竞价品牌广告
  • 网站建设需要企业百度识图在线识别
  • 营销型网站怎么做荆州百度推广
  • 网站运营每天做的权重查询工具
  • seo线下培训课程seo策略分析
  • 微网站建设报价表深圳关键词排名seo
  • 做婚姻介绍网站赚钱吗无货源网店怎么开
  • 移动宽带续费网上可以续费嘛广州seo运营
  • 入驻京东需要自己做网站吗咖啡seo是什么意思
  • 怎么做网站弄网盟网站推广软件ky99
  • 网站开发分为深圳网络推广系统
  • edu网站一般谁做的开发一个网站需要多少钱
  • 合肥做网站大概多少钱seo专员的工作内容
  • 给医院做网站赚钱吗seo专员是干嘛的
  • 做网站主题杭州网站seo推广软件
  • 做网站销售 优帮云个人如何推广app
  • 南昌市 做网站的公司2345网址导航智能主板