java把map集合转成对象集合

//用hibernate查询后的结果集直接转已有的对象
List list = session.createSQLQuery(sql.toString())
              .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();

Class[] cla = {对象A.class,对象B.class};//其中对象A extends 对象B
List result =  ClassHelper.mapListToEntityList(list,cla);

ClassHelp类


public class ClassHelper{

	/*map集合转对象集合 是否格式化NULL数据*/
	public static List mapListToEntityList(List<Map<String, Object>> sourceList, Class[] classes,Boolean isFormat) throws InstantiationException, IllegalAccessException {
		if(sourceList==null || sourceList.size()==0){
			return null;
		}
		Map<String, Field> usedFields = getUsedFields(sourceList.get(0).keySet(), classes);
		long l  = System.currentTimeMillis();
		Class clazz = classes[0];
		List result = new ArrayList(sourceList.size());
		Object entity;
		Field field;
		for (Map<String,Object> map : sourceList) {
			entity = clazz.newInstance();
			
			for (Map.Entry<String, Object> entry : map.entrySet()) {
				if((field = usedFields.get(entry.getKey()))==null){
					continue;
				}
//					System.out.println(entry.getKey()+"="+usedFields.get(entry.getKey())+"="+entry.getValue());
				if(isFormat){
					if(field.getType() == Double.class){
						field.set(entity, Arith.doubleFormat(entry.getValue()));
					}else if (field.getType() == Boolean.class) {
						field.set(entity, entry.getValue()!=null && entry.getValue().equals(1));
					}else if(field.getType() == String.class){
						field.set(entity,Arith.strFormat(entry.getValue()));
					}else {
						field.set(entity,entry.getValue());
					}
				}else{
					if(field.getType() == Double.class){
						field.set(entity,(Double)(entry.getValue()));
					}else if (field.getType() == Boolean.class) {
						field.set(entity, entry.getValue()!=null && entry.getValue().equals(1));
					}else if(field.getType() == String.class){
						field.set(entity,(String)entry.getValue());
					}else {
						field.set(entity,entry.getValue());
					}
				}
				
			}
			result.add(entity);
		}
		System.out.println(System.currentTimeMillis()-l+"ms result.len="+result.size());
		return result;
	}
	/**map集合转对象集合*/
	public static List mapListToEntityList(List<Map<String, Object>> sourceList, Class[] classes)      throws InstantiationException, IllegalAccessException {
		return mapListToEntityList(sourceList, classes, true);
	}
	private static Map<String,Field> getUsedFields( Set<String> keys,Class[] classes) {
		
		HashMap<String, Field> result = new HashMap<String, Field>();
		List<Field> fields = getAllDeclaredField(classes);
		for (String key : keys) {
			for (Field field : fields) {
				String lowerCase = field.getName().toLowerCase();
				if(key.toLowerCase().equals(lowerCase)){
					field.setAccessible(true);
					result.put(key, field);
					fields.remove(field);
					break;
				}
			}
		}
		return result;
	}
	private static List<Field> getAllDeclaredField(Class[] classes) {
		
		List<Field> result = new ArrayList<Field>();
		for (Class clazz : classes) {
			Field[] fields = clazz.getDeclaredFields();
			for (Field field : fields) {
				result.add(field);
			}
		}

		return result;
	}
}

 

转载于:https://my.oschina.net/return/blog/739233

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值