博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
集合处理工具
阅读量:7213 次
发布时间:2019-06-29

本文共 1622 字,大约阅读时间需要 5 分钟。

1 /** 2      * 按给定对象属性为参数ls分组整理 3      * @param ls 集合 4      * @param propertyName 对象中的某个属性 5      * @return HashMap(key=propertyValue,Value=ArrayList) 6      * @throws IllegalAccessException 7      * @throws InvocationTargetException 8      * @throws NoSuchMethodException 9      */10     @SuppressWarnings("unchecked")11     public static 
HashMap
> groupByProperty(List
ls,String propertyName) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{12 HashMap
> result=new HashMap
>();13 List
list=null;14 for (Iterator
iter = ls.iterator(); iter.hasNext();) {15 E element = iter.next();16 T proValue=(T)PropertyUtils.getProperty(element, propertyName);17 if(proValue==null)18 throw new NullPointerException("propertyValue is null"); 19 if(result.containsKey(proValue)){20 list=(List
) result.get(proValue);21 }else{22 list=new ArrayList
();23 result.put(proValue, list);24 }25 list.add(element);26 }27 return result;28 }

 

1     /** 2      * 提取集合中的对象的属性,组合成List. 3      *  4      * @param collection 来源集合. 5      * @param propertyName 要提取的属性名. 6      */ 7     @SuppressWarnings({ "unchecked", "rawtypes" }) 8     public static List fetchElementPropertyToList(final Collection collection, final String propertyName) throws Exception {10         List list = new ArrayList();11         for (Object obj : collection) {12             list.add(PropertyUtils.getProperty(obj, propertyName));13         }14 15         return list;16     }

 

转载于:https://www.cnblogs.com/sun-space/p/5562017.html

你可能感兴趣的文章
微服务,我们如何与你相处
查看>>
中国人工智能学会通讯——KS-Studio:一个知识计算引擎 1.2 知识图谱构建
查看>>
“Redirect to SMB”漏洞影响所有版本的Windows
查看>>
东方通没有创造中间件 却在定义中间件的“化蝶”
查看>>
物联网其实只是一个话题
查看>>
CloudCC:2017年下半年企业移动CRM市场风向窥测
查看>>
《并行计算的编程模型》一3.8.3 原子交换和条件交换
查看>>
备份不等于归档,在智能归档中备份资产!
查看>>
首届渣打科营编程马拉松赛初赛圆满结束
查看>>
三大超算军团加速布局 中科曙光E级超算预研项目正式启动
查看>>
高计算密度+低功耗!浪潮新一代高密度服务器SA5248M4横空出世
查看>>
GPU驱动“后摩尔定律时代” 为HPC和深度学习提供强大加速动力
查看>>
工信部副部长怀进鹏:信息产业呈现四大发展特点
查看>>
专访uPlane陈宏强:手机遥控固定翼飞机还是蓝海
查看>>
读懂Android中的代码混淆
查看>>
新IT运维模式下,全栈溯源助你解应用性能监控难题
查看>>
云存储标准应运而生
查看>>
物联网创新领域的三大驱动性趋势
查看>>
X光扫描揭示芯片密码卡入侵手段
查看>>
如何重新定义云数据中心的资源利用率
查看>>