赞
踩
list模糊查询工具
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Optional;
- import java.util.function.Function;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
-
- /**
- * @author :
- * @description: list模糊查询工具类
- * @date :2023/6/23 14:42
- */
- public class FilterUtil {
-
-
- /**
- * @method: listFilter
- * @Description: 模糊查找
- * @Author:
- * @date: 2023/6/23 14:42
- */
- public static <T> List<T> listFilter(boolean condition, String target, List<T> list, Function<T, ?> function) {
- List<T> result = null;
- if (condition) {
- result = listFilter(target, list, function);
- }
- // 不满足条件直接返回原数据集合
- return Optional.ofNullable(result).orElse(list);
- }
-
-
-
-
- public static <T> List<T> listFilter(String target, List<T> list, Function<T, ?> function) {
- List<T> result = new LinkedList<>();
- //该处决定大小写是否敏感
- Pattern pattern = Pattern.compile(target, Pattern.CASE_INSENSITIVE);
- for (T t : list) {
- Matcher matcher = pattern.matcher(function.apply(t).toString());
- if (matcher.find()) {
- result.add(t);
- }
- }
- return result;
- }
-
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。