list转map工具类,根据指定的字段分组
import org.apache.log4j.Logger; import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map; /**
* list转map工具类,根据指定的字段分组
*
*/
public class EntryUtil { private static Logger logger = Logger.getLogger(EntryUtil.class);
/**
*
* 将list中的元素放到Map<K, List<V>> 以建立 key - List<value> 索引<p>
*
* @param list List<V> 元素列表
* @param keyFieldName String 元素的属性名称, 该属性的值作为索引key
* @param <K> key类型
* @param <V> value类型
* @return Map<K, V> key - value 索引
*
*
*/
public static <K, V> Map<K, List<V>> makeEntityListMap(List<V> list, String keyFieldName) {
Map<K, List<V>> map = new LinkedHashMap<K, List<V>>();
if(list == null || list.size() == 0) {
return map;
}
try {
Method getter = getMethod(list.get(0).getClass(), keyFieldName, "get");
for (V item : list) {
@SuppressWarnings("unchecked")
K key = (K) getter.invoke(item);
List<V> groupList = map.get(key);
if (groupList == null) {
groupList = new ArrayList<V>();
map.put(key, groupList);
}
groupList.add(item);
}
} catch (Exception e) {
logger.error("makeEntityListMap error list is " + list, e);
return map;
}
return map;
} /**
* 获取getter或setter
*/
@SuppressWarnings("unchecked")
private static Method getMethod(@SuppressWarnings("rawtypes") Class clazz, String fieldName,
String methodPrefix) throws NoSuchMethodException {
String first = fieldName.substring(0, 1);
String getterName = methodPrefix + fieldName.replaceFirst(first, first.toUpperCase());
return clazz.getMethod(getterName);
}
}
list转map工具类,根据指定的字段分组的更多相关文章
- Xml格式数据转map工具类
前言[ 最近在写一个业务,由于调UPS物流的接口返回的是一个xml格式的数据结果,我现在要拿到xml中某个节点的值,而这个xml数据是多节点多层级的,并且这某个节点的值有可能只有一条值,有可能有多条, ...
- js 实现 map 工具类
/* * MAP对象,实现MAP功能 * * 接口: * size() 获取MAP元素个数 * isEmpty() 判断MAP是否为空 * clear() 删除MAP所有元素 * put(key, v ...
- Json格式String类型字符串转为Map工具类
package agriculture_implement.util; import com.google.gson.Gson; import com.google.gson.JsonSyntaxEx ...
- Map转url ,url转Map工具类
/** * 将url参数转换成map * @param param aa=11&bb=22&cc=33 * @return */ public static Map<String ...
- xml和map互转工具类
/** * xml转map工具类 * @author zhangyao * */ public class XmlToMapUtil { /** * xml转map 不带属性 * @param xml ...
- HttpUtil工具类
HttpUtil工具类 /** * 向指定URL发送GET方法的请求 * * @param url * 发送请求的URL * @param params * 请求参数,请求参数应该是name1=val ...
- (11)连个工具类之间的比较4.Collections与Arrays
集合框架中的工具类:特点:该工具类中的方法都是静态的. Collections:常见方法: 1, 对list进行二分查找: 前提该集合一定要有序. int binarySearch(list,key) ...
- java 日期工具类
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; imp ...
- java工具类
1.HttpUtilsHttp网络工具类,主要包括httpGet.httpPost以及http参数相关方法,以httpGet为例:static HttpResponse httpGet(HttpReq ...
随机推荐
- bzoj1699
st表 我还不会st表 f[i][j]表示[i,i+2^j)区间的最值 构造就像lca一样f[i][j]=f[i][j-1] f[i][j]=max(f[i][j-1],f[i+(1<<( ...
- 插入1000万条数据到mysql数据库表
转自:https://www.cnblogs.com/fanwencong/p/5765136.html 我用到的数据库为,mysql数据库5.7版本的 1.首先自己准备好数据库表 其实我在插入100 ...
- Appium + python - automator定位操作
# coding:utf-8from appium import webdriverfrom time import sleep desired_caps = { 'platformName': 'A ...
- alipay.trade.refund (统一收单交易退款接口)[支付宝退款]
首页官网退款的api: https://doc.open.alipay.com/docs/api.htm?spm=a219a.7395905.0.0.UTBitT&docType=4& ...
- bootstrap表格样式
一:表格基本格式 <table> <tr> <th>标题一</th> <th>标题二</th> </tr> < ...
- Gradle 自定义Task 打Jar包
可以作为Jar包内容的有两个地方: : build/intermediates/bundles/release/ 下的classes.jar : build/intermediates/classes ...
- 使用Sql Server Management Studio 2008将数据导出到Sql文件中
最近需要将一个Sql Server 2005数据库中的数据导出,为了方便,就希望能导出成Sql文件,里面包含的数据是由Insert 语句组成的. 在Sql Server Management St ...
- c++ 枚举与字符串 比较
读取字符串,然后将这个字符转换为对应的枚举. 如:从屏幕上输入'a',则转换为set枚举中对应的a,源代码如下: //关键函数为char2enum(str,temp); #include using ...
- .NET 在序列化时使用全小写的属性名
基于某些奇怪的需求,需要将一些对象序列化后输出,而且属性名又必须为小写形式. 解决过程 说到在 .NET 平台上序列化操作,那么第一个想到的应该就是 Json.NET 家的 Newtonsoft.Js ...
- Arduino LM35温度检测
一. 接线原理图 二.实物图 三.代码例子