按list对象的某个方法返回值分组
来源:互联网 发布:web软件开发方式 编辑:程序博客网 时间:2024/05/17 01:01
通过list里对象的某个方法返回值,对list进行分组。
/** * 按list对象的某个方法返回值分组 * * @param list 待分组的list集合 * @param methodName 对象方法名称 * @param methodArgs 对象方法的参数 * @param <T> 对象 * @param <MapKey> 分组key的数据类型 * @return Map 分组的map * @throws NoSuchMethodException * @throws InvocationTargetException * @throws IllegalAccessException */ @SuppressWarnings("unchecked") public static <T, MapKey> Map<MapKey, List<T>> groupByKey(List<T> list, String methodName, Object... methodArgs) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Map<MapKey, List<T>> map = new HashMap<>(); if (list != null) { for (T t : list) { Class<?> clz = t.getClass(); Class[] argsClass = new Class[methodArgs.length]; for (int i = 0; i < methodArgs.length; i++) { argsClass[i] = methodArgs[i].getClass(); } Method mth = clz.getMethod(methodName, argsClass); MapKey key = (MapKey) mth.invoke(t, methodArgs); if (map.containsKey(key)) { map.get(key).add(t); } else { List<T> tempList = new ArrayList<>(); tempList.add(t); map.put(key, tempList); } } } return map; }
就是不写注释,略略略略略略略。。。
阅读全文
0 0
- 按list对象的某个方法返回值分组
- 关于List<object>中按对象的某个属性值排序的方法
- C# List对象的分组
- c# List find()方法返回值的问题(返回结果为对象的指针)
- 【代码分享】关于List<V>按V的某个属性分组的通用代码实现
- 按照List中Object的某个属性进行分组
- JS 使用List<Object> 对象中的Object对象的某个属性的临时处理方法
- 直接比较两个对象是否相等返回false和List中判断是否包含某个对象的问题
- 多个List合并去除list中对象的某个属性值相同的对象
- sql count某个group by出来的分组后,能按条件取出该分组的某个值
- 如何判断某个方法的返回值是不是void
- 数组对象根据某个值相同合并分组
- 返回到某个VC 的方法
- Access中查询某个分组中多行字段值的拼接字串的方法
- 返回List的分页方法
- Java 将一个list按照某个字段分组后放入到一个新的list里面
- Java 将一个list按照某个字段分组后放入到一个新的list里面
- java list<自定义对象>按对象的某个属性进行排序,和判断相等
- 326. Power of Three
- apache ant的使用与介绍
- File类的判断功能
- 那些seo技巧会导致网站排名下降?
- mysql设计索引的原则与索引的选择
- 按list对象的某个方法返回值分组
- Python高级编程技巧
- springMVC 中几种获取request和response的方式
- RabbitMQ学习笔记(例子)
- Leetcode 378 Kth Smallest Element in a Sorted Matrix
- CAP原则(CAP定理)、BASE理论
- [bigdata-103] spring-cloud-01 服务注册 eureka server 单机版
- File获取功能
- OpenJudge 2989 糖果