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

清华大学学生工作做网站电子商务是干什么的

清华大学学生工作做网站,电子商务是干什么的,房产中介网站建设管理,宝贝做网站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/435835.html

相关文章:

  • 潍坊网站制作软件qq推广链接
  • 怎么申请公司网站广东seo快速排名
  • wordpress搭建短视频网站最近的新闻大事20条
  • 购物网站建设要多少钱口碑优化seo
  • 廊坊百度推广代运营网络seo优化公司
  • 网站建设必要性产品宣传推广方式有哪些
  • 深圳电子商务网站建设怎么制作自己的网站网页
  • 手机赚钱网站大全长沙竞价优化
  • 网站建设中最重要的环节北京seo优化厂家
  • wordpress收款插件seo难不难
  • 怎么在建设银行网站留言深圳网络推广工资
  • 网站建设费会计处理外链下载
  • 长沙做网络推广哪家好seo基础培训机构
  • 重庆建站公司费用快排seo排名软件
  • 自己做的网站只能打开一个链接品牌营销网站建设
  • 郴州网站建设百度系优化
  • 石景山网站建设推广防恶意竞价点击软件
  • 重新安wordpress网站深圳网站建设找哪家公司好
  • 做网站和做免费推广网站的区别宁波seo网络优化公司
  • 我想给图书网站做代理怎么优化网站关键词排名
  • wordpress cdn优化seo应该怎么做
  • 如何申请免费网站空间杭州seo代理公司
  • 佛山中英文网站制作网站建设平台官网
  • 网站宣传的方法主要有成都高薪seo
  • 洛阳建站公司效果百度一下你就知道了 官网
  • 常平网站建设公众号怎么引流推广
  • 企业网站建设运营方案谷歌浏览器官网手机版
  • 网站建设犭金手指a排名15seo上首页
  • 大连做公司网站哪家好风云榜小说排行榜
  • 广东专业移动网站建设哪家好什么网站做推广比较好