Stream filter过滤案例
生活随笔
收集整理的這篇文章主要介紹了
Stream filter过滤案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package com.gblfy.gxts;import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.Before;
import org.junit.Test;import java.util.List;/*** 案例2:* 標簽管理功能模塊。允許用戶批量添加標簽,后臺需要對標簽去重,* 并且需要防止數據庫中存在同名的標簽。*/
public class CaseOne {/*** 用戶請求的創建標簽模型*/@Data@AllArgsConstructorstatic class TagReqDTO {/*** 標簽名字*/private String name;/*** 標簽值:年齡*/private Integer age;}//從DB中查詢出來已經存在的標簽名List<String> tagListFromDB;//用戶請求的標簽列表List<TagReqDTO> tagListFromrEeq;//初始化數據@Beforepublic void init() {//數據庫中存在的標簽名列表tagListFromDB = Lists.newArrayList("李四", "王五", "趙六");//用戶提交的tagListFromrEeq = Lists.newArrayList(new TagReqDTO("張三", 10),new TagReqDTO("李四", 20),new TagReqDTO("張三", 10));}/*** 過濾去重后需要提交的標簽列表*/@Testpublic void distinctTag() {tagListFromrEeq.stream()//TODO true:通過測試,數據不過濾 false:未通過測試數據過濾.filter(tagReqDTO -> !tagListFromDB.contains(tagReqDTO.getName()))//TODO 通過equals對元素去重.distinct().forEach(tagReqDTO -> System.out.println(tagReqDTO));}
}
總結
以上是生活随笔為你收集整理的Stream filter过滤案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring整合rabbitMQ最新版
- 下一篇: Stream anyMatch查找案例