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

Java工具类,对象List提取某个属性为List,对象List转为对象Map其中某个属性作为Key值

Java工具类

package org.common;import lombok.extern.slf4j.Slf4j;import java.util.*;
import java.util.stream.Collectors;@Slf4j
public final class CollectorHelper {/*** @param element* @param propertyName* @param <E>* @return*/public static <E> List toList(Collection<E> element, String propertyName) {if (element == null) {return Collections.emptyList();}return element.stream().map(item -> {return ReflectCallField.getForInstance(item, propertyName);}).collect(Collectors.toList());}/*** @param element* @param propertyName* @param <E>* @return*/public static <E> List extractList(Collection<E> element, String propertyName) {if (element == null) {return Collections.emptyList();}List dataList = new ArrayList();for (E item : element) {Object obj = ReflectCallField.getForInstance(item, propertyName);if (obj != null) {dataList.add(obj);}}return dataList;}/*** @param element* @param propertyName* @param <K>* @param <V>* @return*/public static <K, V> Map<K, V> toMap(Collection<V> element, String propertyName) {if (element == null) {return Collections.emptyMap();}try {return (Map<K, V>) element.stream().collect(Collectors.toMap(item -> {return ReflectCallField.getForInstance(item, propertyName);}, item -> item));} catch (Exception e) {log.error(e.getMessage());}return Collections.emptyMap();}/*** @param element* @param propertyName* @param <K>* @param <V>* @return*/public static <K, V> Map<K, V> extractMap(Collection<V> element, String propertyName) {if (element == null) {return Collections.emptyMap();}Map<K, V> dataMap = new HashMap();for (V item : element) {Object key = ReflectCallField.getForInstance(item, propertyName);if (key != null) {dataMap.put((K) key, item);}}return dataMap;}}
package org.common;import java.lang.reflect.Field;public class ReflectCallField {/*** @param entryClass* @param fieldName* @param <T>* @return*/public static <T> T getForStatic(Class entryClass, String fieldName) {if (entryClass == null) {return null;}T result = null;try {// 反射获取Field callField = entryClass.getDeclaredField(fieldName);callField.setAccessible(true);return (T) callField.get(entryClass);} catch (Exception e) {e.printStackTrace();}return result;}/*** @param entryClass* @param fieldName* @param value*/public static void setForStatic(Class entryClass, String fieldName, Object value) {if (entryClass == null) {return;}try {Field callField = entryClass.getDeclaredField(fieldName);callField.setAccessible(true);callField.set(fieldName, value);} catch (Exception e) {e.printStackTrace();}}/*** @param entryObject* @param fieldName* @param <T>* @return*/public static <T> T getForInstance(Object entryObject, String fieldName) {if (entryObject == null) {return null;}T result = null;try {// 反射获取Field callField = entryObject.getClass().getDeclaredField(fieldName);callField.setAccessible(true);return (T) callField.get(entryObject);} catch (Exception e) {e.printStackTrace();}return result;}/*** @param entryObject* @param fieldName* @param value*/public static void setForInstance(Object entryObject, String fieldName, Object value) {if (entryObject == null) {return;}try {Field callField = entryObject.getClass().getDeclaredField(fieldName);callField.setAccessible(true);callField.set(entryObject, value);} catch (Exception e) {e.printStackTrace();}}}

测试

package org.common;import lombok.Data;@Data
public class Franchisee {private Integer id;private String name;}
package org.common;import java.util.ArrayList;
import java.util.List;
import java.util.Map;public class CollectorHelperTest {public static void main(String[] args) {List<Franchisee> dataList = new ArrayList<>();Franchisee franchisee1 = new Franchisee();franchisee1.setId(1);franchisee1.setName("Franchisee1");dataList.add(franchisee1);Franchisee franchisee2 = new Franchisee();franchisee2.setId(2);franchisee2.setName("Franchisee2");dataList.add(franchisee2);Franchisee franchisee3 = new Franchisee();franchisee3.setName("Franchisee3");dataList.add(franchisee3);Franchisee franchisee4 = new Franchisee();franchisee4.setName("Franchisee4");dataList.add(franchisee4);//Map<Integer, Franchisee> dataMap = CollectorHelper.toMap(dataList, "id");System.out.println("dataMap = " + dataMap);Map<Integer, Franchisee> dataMap2 = CollectorHelper.extractMap(dataList, "id");System.out.println("dataMap2 = " + dataMap2);//List<Integer> idList = CollectorHelper.toList(dataList, "id");System.out.println("idList = " + idList);idList.forEach(id -> {System.out.println("id = " + id);});List<Integer> idList2 = CollectorHelper.extractList(dataList, "id");System.out.println("idList2 = " + idList2);idList2.forEach(id -> {System.out.println("id = " + id);});}}

http://www.lryc.cn/news/582100.html

相关文章:

  • RAG实战指南 Day 8:PDF、Word和HTML文档解析实战
  • UI自动化常见面试题
  • day08-Elasticsearch
  • 云计算领域“XaaS”是什么?
  • Python编译器(Pycharm Jupyter)
  • 第4.2节 Android App生成追溯关系
  • 【Mac 从 0 到 1 保姆级配置教程 19】- 英语学习篇-我的英语工作流分享(AI 辅助学习)
  • JavaWeb笔记07
  • 比亚迪6月销量38.25万辆,同比增长11.9%
  • window显示驱动开发—BGRA 扫描输出支持
  • 特伦斯T1节拍器,突出综合优势与用户体验
  • Python 包管理工具 uv
  • 【C语言进阶】数据是如何存储的?
  • Web后端开发-请求响应
  • 国产CAD皇冠CAD(CrownCAD)建模教程:哈雷摩托车发动机零件
  • [论文阅读] 人工智能 | 读懂Meta-Fair:让LLM摆脱偏见的自动化测试新方法
  • 【mini-spring】【更新中】第一章 IOC与Bean源码及思路解析
  • IT 与动环一体化运维的技术融合实践
  • MySQL Galera Cluster企业级部署
  • 力扣_链表(前后指针)_python版本
  • verilog中timescale指令的使用
  • 零知开源——STM32F4结合BMP581气压传感器实现ST7789中文显示教程
  • centos stream 10设置本地网络
  • 沙箱逃逸漏洞
  • 音频信号的预加重:提升语音清晰度
  • OpenCV 人脸分析------面部关键点检测类cv::face::FacemarkLBF
  • 使用ansible的角色实现批量安装nginx服务
  • 图像处理基础:镜像、缩放与矫正
  • 《声音的变形记:Web Audio API的实时特效法则》
  • 【论文撰写】如何把AI生成的文本公式复制在word中,完整的复制公式,拷贝豆包生成的公式