20170830-mapTobean-wy是什么意思?

2026-04-15 09:1710阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计521个文字,预计阅读时间需要3分钟。

20170830-mapTobean-wy是什么意思?

将map封装成JavaBean,并使用List合并-map-bean,反过来使用beanToMap进行比较,简单来说主要是设置bean的field,以及与bean的注解和反射相关。可以将bean的set方法写成静态代码段。

20170830-mapTobean-wy是什么意思?

把map封装成javaBean

/** List合并-map-bean 反过来beanToMap相对比较简单些 主要是设置bean的filed, 与bean的注入和反射相关 可以把bean的set方法写成静态代码段 */ private BrandTop getBean(List > totalList, List > subList, List > totalCompList,List > subCompList) { if (check(totalList) || check(subList)|| check(totalCompList)|| check(subCompList)) { return null; }; List > totalBeanList = compTwoMap(totalList, totalCompList); List > subComBeanList = compTwoMap(subList, subCompList); BrandTop brandTop = createBrandTop(totalBeanList.get(0)); List brandTopList = new ArrayList<>(); for (Map map : subComBeanList) { brandTopList.add(createBrandTop(map)); } brandTop.setSubList(brandTopList); return brandTop; } private boolean check(List > checkList) { if (checkList == null || !checkList.isEmpty()) { return false; } return true; } private BrandTop createBrandTop(Map map) { Method[] methods = BrandTop.class.getMethods(); Map brandTopMap = new HashMap<>(); for (Method method : methods) { if (method.getName().contains("set")) { brandTopMap.put(method.getName(), method); } } BrandTop brandTop = new BrandTop(); for (Entry entry : map.entrySet()) { String key = "set" +toUpCase(entry.getKey()); //没有此字段 if ("setCompBrandCate".equals(key)) { continue; } Method method = brandTopMap.get(key); try { setFileds(brandTop, entry, method); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); } } return brandTop; } private void setFileds(BrandTop brandTop, Entry entry, Method method) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { String fieldType = method.getParameters()[0].getParameterizedType().getTypeName(); String value = entry.getValue().toString(); if ("java.lang.String".equals(fieldType)) { method.invoke(brandTop, value); } else if ("Date".equals(fieldType)) { Date temp = parseDate(value); method.invoke(brandTop, temp); } else if ("Integer".equals(fieldType) || "int".equals(fieldType)) { Integer intval = (int) (Double.parseDouble(value)); method.invoke(brandTop, intval); } else if ("Long".equalsIgnoreCase(fieldType)) { Long temp = Long.parseLong(value); method.invoke(brandTop, temp); } else if ("Double".equalsIgnoreCase(fieldType)) { Double temp = Double.parseDouble(value); method.invoke(brandTop, temp); } else if ("Boolean".equalsIgnoreCase(fieldType)) { Boolean temp = Boolean.parseBoolean(value); method.invoke(brandTop, temp); } else { System.out.println("not supper type" + fieldType); } } private List > compTwoMap(List > totalList, List > totalCompList) { for (int index = 0; index < totalCompList.size()&& index < totalList.size(); index++) { for (Entry entry : totalCompList.get(index).entrySet()) { String key = toUpCase(entry.getKey()); totalList.get(index).put("comp" + key, entry.getValue()); } } return totalList; } private String toUpCase(String key) { return String.format("%s%s", key.substring(0, 1).toUpperCase(), key.substring(1)); } /** * 格式化string为Date * * @param datestr * @return */ public static Date parseDate(String datestr) { if (null == datestr || "".equals(datestr)) { return null; } try { String fmtstr; if (datestr.contains(":")) { fmtstr = "yyyy-MM-dd HH:mm:ss"; } else { fmtstr = "yyyy-MM-dd"; } SimpleDateFormat sdf = new SimpleDateFormat(fmtstr, Locale.UK); return sdf.parse(datestr); } catch (Exception e) { return null; } }

本文共计521个文字,预计阅读时间需要3分钟。

20170830-mapTobean-wy是什么意思?

将map封装成JavaBean,并使用List合并-map-bean,反过来使用beanToMap进行比较,简单来说主要是设置bean的field,以及与bean的注解和反射相关。可以将bean的set方法写成静态代码段。

20170830-mapTobean-wy是什么意思?

把map封装成javaBean

/** List合并-map-bean 反过来beanToMap相对比较简单些 主要是设置bean的filed, 与bean的注入和反射相关 可以把bean的set方法写成静态代码段 */ private BrandTop getBean(List > totalList, List > subList, List > totalCompList,List > subCompList) { if (check(totalList) || check(subList)|| check(totalCompList)|| check(subCompList)) { return null; }; List > totalBeanList = compTwoMap(totalList, totalCompList); List > subComBeanList = compTwoMap(subList, subCompList); BrandTop brandTop = createBrandTop(totalBeanList.get(0)); List brandTopList = new ArrayList<>(); for (Map map : subComBeanList) { brandTopList.add(createBrandTop(map)); } brandTop.setSubList(brandTopList); return brandTop; } private boolean check(List > checkList) { if (checkList == null || !checkList.isEmpty()) { return false; } return true; } private BrandTop createBrandTop(Map map) { Method[] methods = BrandTop.class.getMethods(); Map brandTopMap = new HashMap<>(); for (Method method : methods) { if (method.getName().contains("set")) { brandTopMap.put(method.getName(), method); } } BrandTop brandTop = new BrandTop(); for (Entry entry : map.entrySet()) { String key = "set" +toUpCase(entry.getKey()); //没有此字段 if ("setCompBrandCate".equals(key)) { continue; } Method method = brandTopMap.get(key); try { setFileds(brandTop, entry, method); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); } } return brandTop; } private void setFileds(BrandTop brandTop, Entry entry, Method method) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { String fieldType = method.getParameters()[0].getParameterizedType().getTypeName(); String value = entry.getValue().toString(); if ("java.lang.String".equals(fieldType)) { method.invoke(brandTop, value); } else if ("Date".equals(fieldType)) { Date temp = parseDate(value); method.invoke(brandTop, temp); } else if ("Integer".equals(fieldType) || "int".equals(fieldType)) { Integer intval = (int) (Double.parseDouble(value)); method.invoke(brandTop, intval); } else if ("Long".equalsIgnoreCase(fieldType)) { Long temp = Long.parseLong(value); method.invoke(brandTop, temp); } else if ("Double".equalsIgnoreCase(fieldType)) { Double temp = Double.parseDouble(value); method.invoke(brandTop, temp); } else if ("Boolean".equalsIgnoreCase(fieldType)) { Boolean temp = Boolean.parseBoolean(value); method.invoke(brandTop, temp); } else { System.out.println("not supper type" + fieldType); } } private List > compTwoMap(List > totalList, List > totalCompList) { for (int index = 0; index < totalCompList.size()&& index < totalList.size(); index++) { for (Entry entry : totalCompList.get(index).entrySet()) { String key = toUpCase(entry.getKey()); totalList.get(index).put("comp" + key, entry.getValue()); } } return totalList; } private String toUpCase(String key) { return String.format("%s%s", key.substring(0, 1).toUpperCase(), key.substring(1)); } /** * 格式化string为Date * * @param datestr * @return */ public static Date parseDate(String datestr) { if (null == datestr || "".equals(datestr)) { return null; } try { String fmtstr; if (datestr.contains(":")) { fmtstr = "yyyy-MM-dd HH:mm:ss"; } else { fmtstr = "yyyy-MM-dd"; } SimpleDateFormat sdf = new SimpleDateFormat(fmtstr, Locale.UK); return sdf.parse(datestr); } catch (Exception e) { return null; } }