1 package com.example.springstudy.test.functions; 2 3 import cn.hutool.core.util.ReflectUtil; 4 import com.example.springstudy.test.User; 5 import com.google.common.collect.Lists; 6 7 import java.lang.reflect.Field; 8 import java.util.List; 9 import java.util.Optional; 10 import java.util.UUID; 11 import java.util.function.UnaryOperator; 12 13 /** 14 * @Author: guodong 15 * @CreateTime: 2023-05-05 16:57 16 * @Description: TODO 17 * @Version: 1.0 18 */ 19 public class UnaryOperatorTest { 20 21 22 // 提供数据 23 public <T> void supplyData(Class<T> tClass, UnaryOperator<List<T>> unaryOperator) throws Exception { 24 List<T> userList = Lists.newArrayList(); 25 for (int i = 1; i <= 10; i++) { 26 T entityNew = (T) ReflectUtil.newInstance(tClass); 27 Field[] entityNewFields = ReflectUtil.getFields(entityNew.getClass()); 28 for (Field field : entityNewFields) { 29 field.setAccessible(true); 30 if (field.getName().equals("id")) { 31 field.set(entityNew, Long.valueOf(i)); 32 } else if (field.getName().equals("age")) { 33 field.set(entityNew, i); 34 } else if (field.getName().equals("name")) { 35 field.set(entityNew, UUID.randomUUID().toString()); 36 } 37 } 38 userList.add(entityNew); 39 } 40 unaryOperator.apply(userList); 41 } 42 43 44 // 处理逻辑 45 public void handleData() throws Exception { 46 Optional<User> optionalUser = Optional.of(new User()); 47 supplyData(User.class, list -> { 48 saveUserInfo(list); 49 return null; 50 }); 51 } 52 53 // 保存数据 54 public void saveUserInfo(List<User> userList) { 55 userList.stream().forEach(data -> System.out.println(data)); 56 } 57 58 59 public static void main(String[] args) throws Exception { 60 UnaryOperatorTest unaryOperatorTest = new UnaryOperatorTest(); 61 unaryOperatorTest.handleData(); 62 } 63 64 65 }
标签:java,void,汇总,util,UnaryOperator,import,public From: https://www.cnblogs.com/jelly12345/p/17374946.html