徐州网站建设工作室,中石建基础设施建设有限公司网站,推广引流公司,政务公开网站建设要求前言介绍#xff08;如果对你有帮助#xff0c;请给我点点赞#xff09;
当我们在做Java项目时#xff0c;经常需要对集合进行操作。而对于List集合来说#xff0c;Stream流提供了一系列便捷的方法#xff0c;可以帮助我们快速地完成集合的筛选、排序、聚合等操作。但是…前言介绍如果对你有帮助请给我点点赞
当我们在做Java项目时经常需要对集合进行操作。而对于List集合来说Stream流提供了一系列便捷的方法可以帮助我们快速地完成集合的筛选、排序、聚合等操作。但是由于Stream流的语法比较复杂有时候会给我们带来一定的困扰。
为了解决这个问题我在做项目的过程中通过学习和总结封装了一些常用的Stream流操作方法。这些方法可以让我们更加方便地对List集合进行处理提高开发效率并减少出错的风险。
在下面的文档中我将分享这些方法的实现思路和使用方法希望能够对大家在日常开发中遇到的类似问题有所帮助
1、方法示例
【 批量修改集合对象的某个值 】
public static void main(String[] args) {//创建假数据, 对象属性为 id , nameTUser u1 new TUser(1,张三);TUser u2 new TUser(2,李四);TUser u3 new TUser(3,王五);TUser u4 new TUser(4,赵六);ListTUser userList new ArrayList();//将对象添加到集合中userList.add(u1);userList.add(u2);userList.add(u3);userList.add(u4);//TODO 引用【批量修改集合对象的某个值】实现将集合中所有对象的name修改为 坤哥//参数1要处理的集合//参数2要修改的属性//参数3修改的值setListObjectByProperty(userList,TUser::setName,坤哥);for (TUser tUser : userList) {System.out.println(修改后的对象 tUser);}
}输出结果如下
修改后的对象TUser{id1, name坤哥}
修改后的对象TUser{id2, name坤哥}
修改后的对象TUser{id3, name坤哥}
修改后的对象TUser{id4, name坤哥}
2、方法封装
import org.springframework.beans.BeanUtils;import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;public class StreamUtils {/*** 批量修改集合对象的某个值* param list: 待修改集合* param consumer: 修改属性 泛型属性对象的set方法 Object::set方法* param val: 新值* param T: 目标对象泛型* param V: 目标值泛型*/public static T,V void setListObjectByProperty(ListT list, BiConsumerT,V consumer, V val){list.forEach(bean - consumer.accept(bean,val));}/*** 修改集合中满足条件的对象的指定属性的值* param list: 目标集合* param conditionProperty: 条件属性 Objecg::getFiled();* param conditionValue: 条件值* param consumer: 修改的属性 Objecg::setFiled();* param newValue: 修改值* param T: 目标对象泛型* param V: 目标值泛型* param U: 修改值泛型*/public static T, V, U void updateListObjByField(ListT list, FunctionT, V conditionProperty,V conditionValue, BiConsumerT, U consumer, U newValue) {list.stream().filter(element - conditionProperty.apply(element).equals(conditionValue)).forEach(element - consumer.accept(element, newValue));}/*** 泛型对象的属性setter接口** param T 泛型类型*/public interface PropertySetterT {void set(T target, Object value);}/*** ListSysUser - ListUser* 集合复制 【泛型可以不一样但是泛型中的属性一样】* param sourceList: 待赋值的集合* param targetType: 泛型.class* param T* param R* return*/public static T, R ListR copyList(ListT sourceList, ClassR targetType){return sourceList.stream().map( bean - {try {R target targetType.getDeclaredConstructor().newInstance();BeanUtils.copyProperties(bean,target);return target;} catch (Exception e) {e.printStackTrace();return null;}}).collect(Collectors.toList());}/*** 根据指定条件过滤List集合中指定的元素* param inputList: 源list集合* param propertyExtractor: 指定对象属性 Object::getVal* param values: 过滤的集合值* param T* param R* return*/public static T, R ListT filterListByProperty(ListT inputList, FunctionT, R propertyExtractor, ListR values) {PredicateT condition item - !values.contains(propertyExtractor.apply(item));return filterList(inputList, condition);}public static T ListT filterList(ListT inputList, PredicateT condition) {return inputList.stream().filter(condition).distinct().collect(Collectors.toList());}/*** 提取List集合泛型对象中指定属性为map对象* param inputList: 目标list* param keyExtractor: key属性 Object:getObj* param valueExtractor: val属性 Object:getObj* param T* param K* param V* return*/public static T, K, V MapK, V extractPropertyToMap(ListT inputList, FunctionT, K keyExtractor, FunctionT, V valueExtractor) {return inputList.stream().collect(Collectors.toMap(keyExtractor, valueExtractor, (v1, v2) - v2));}/*** 获取集合中指定元素重复的数据,并返回这个元素的值* param list源集合* param propertyExtractor: 指定元素 Object::getVal* param T* param R* return*/public static T, R ListR findDuplicates(ListT list, FunctionT, R propertyExtractor) {MapR, Long propertyCountMap list.stream().collect(Collectors.groupingBy(propertyExtractor, Collectors.counting()));return propertyCountMap.entrySet().stream().filter(entry - entry.getValue() 1).map(Map.Entry::getKey).collect(Collectors.toList());}/*** 提取List集合中对象的某个属性并返回集合* param inputList: 目标集合* param propertyExtractor: 泛型对象的属性 Object::getVal* param T* param R* return*/public static T, R ListR extractPropertyToList(ListT inputList, FunctionT, R propertyExtractor) {return inputList.stream().map(propertyExtractor).collect(Collectors.toList());}/*** 提取List集合中对象的某个属性* param inputList: 目标集合* param propertyExtractor: 泛型对象的属性 Object::getVal* param returnType: 返回集合指定泛型 String.class* param T* param R* return*/public static T, R ListR extractPropertyToListWithType(ListT inputList, FunctionT, R propertyExtractor, ClassR returnType) {return inputList.stream().map(propertyExtractor).collect(Collectors.toList());}/*** 提取List集合中指定条件的属性值* param list源集合* param filter指定条件属性 泛型属性对象的get()方法* param mapper返回属性 泛型属性对象的get()方法* param cls返回类型* param value条件值* param T* param R* return*/public static T, R ListR extractPropertyToListWithTypeAndFilter(ListT list, FunctionT, String filter, FunctionT, R mapper, ClassR cls, String value) {return list.stream().filter(obj - filter.apply(obj).equals(value)).map(mapper).collect(Collectors.toList());}/*** 校验集合中对象指定属性的值是否都满足传入条件值* param collection: 源集合* param propertyAccessor: 指定属性 get方法* param expectedValue: 条件值* param T* param E* return*/public static T, E boolean validatePropertyAll(CollectionT collection, FunctionT, E propertyAccessor, E expectedValue) {return collection.stream().allMatch(item - expectedValue.equals(propertyAccessor.apply(item)));}/*** 校验集合中是否有一个对象的属性值满足传入条件值* param collection: 源集合* param propertyAccessor: 指定属性: 指定属性的 get方法* param expectedValue: 条件值* param T* param E* return*/public static T, E boolean validatePropertyAny(CollectionT collection, FunctionT, E propertyAccessor, E expectedValue) {return collection.stream().anyMatch(item - expectedValue.equals(propertyAccessor.apply(item)));}}