Java instanceof 各种数据类型判断

 Java  instanceof 各种数据类型判断

在导出的时候,要匹配数据,而数据有各种类型,只能用instanceof一一进行判断了。

目标:

表头: {"aa","bb","cc","dd","ee","ff","gg"}

 

 数据

[{"aa":100,"bb":"1,2,3","cc":{"num":"99","name":"统计数值"},"dd":["10505","10508"],"ee":[["业务运营保障","批量数据"],["系统运行维护","问题故障通知"],["系统运行维护","运维优化"]],"ff":[{"name":"工单开始","state":"F"},{"name":"工单处理","state":"A"}]}]

导出:

 

如何进行匹配数据呢? 再进行导出到word表格内呢?

处理:

1,模拟数据:

public static List<Map<String, Object>> initData(){
    List<Map<String, Object>> dataList = new ArrayList<>();
    Map<String, Object> dataMap = new HashMap<>();
    dataMap.put("aa", 100);
    dataMap.put("bb", "1,2,3");
    // map
    dataMap.put("cc", new HashMap<String, String>(){{
        put("name","统计数值");
        put("num","99");
    }});
    // 一维字符数组
    dataMap.put("dd", Lists.newArrayList("10505", "10508"));
    // 二维字符数组
    dataMap.put("ee", Lists.newArrayList( Lists.newArrayList("业务运营保障", "批量数据"),
            Lists.newArrayList("系统运行维护", "问题故障通知"),
            Lists.newArrayList("系统运行维护", "运维优化")));
    // list map
    dataMap.put("ff", Lists.newArrayList(new HashMap<String, String>(){{
        put("name","工单开始");
        put("state","F");
    }}, new HashMap<String, String>(){{
        put("name","工单处理");
        put("state","A");
    }}));

    dataList.add(dataMap);
    return dataList;
}

 

2,匹配数据:

/**
 *  匹配字段对应的值
 */
private static List<List<String>> matchHeaderData(List<Map<String, Object>> data, List<String> matchField) {
    try {
        return ListUtils.emptyIfNull(data).stream().map(e -> ListUtils.emptyIfNull(matchField).stream().map(f -> {

            Object object = e.get(f);
            if (object instanceof List) {
                List dataList = (List) object;
                if (CollectionUtils.isNotEmpty(dataList)) {
                    Object inner = dataList.get(0);
                    if (inner instanceof List) { // 二维字符数组 获取最后一个字符
                        List<List<String>> dyadicList = (List<List<String>>) object;
                        return ListUtils.emptyIfNull(dyadicList).stream().map(
                                dy -> ListUtils.emptyIfNull(dy).stream().reduce((first, last) -> last).orElse(""))
                                .collect(Collectors.joining(","));
                    } else if (inner instanceof Map) { // list map 格式
                        List<Map<String, Object>> mapList = (List<Map<String, Object>>) object;
                        return ListUtils.emptyIfNull(mapList).stream().map(t -> MapUtils.getString(t, "name"))
                                .collect(Collectors.joining(","));
                    } else if (inner instanceof String) { // 一维字符串数组
                        List<String> strList = (List<String>) object;
                        return ListUtils.emptyIfNull(strList).stream().collect(Collectors.joining(","));
                    }
                }
                return "";
            } else if (object instanceof Map) { // map 类型
                Map<String, Object> map = (Map<String, Object>) object;
                return MapUtils.getString(map, "num");
            }else  { // 数字,字符串
                String label = MapUtils.getString(e, f);
                return label == null ? "" : label;
            }
        }).collect(Collectors.toList())).collect(Collectors.toList());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new ArrayList<>();
}

 

 

匹配数据的时候,要对各种类型进行判断,map里面属性要相对固定,不然不好处理。最好数据有类型,先根据类型去处理,就不用一个一个 instanceof 判断了。

3,导出数据

public static void main(String[] args) throws Exception {
    OutputStream out = new FileOutputStream("d://exportFile//table" + System.currentTimeMillis() + ".doc");
    Document document = new Document(PageSize.A4);
    RtfWriter2.getInstance(document, out);
    document.open();

    Color lightGray = new Color(232, 232, 232);
    com.lowagie.text.Font fontChinese = new com.lowagie.text.Font(null, 12, com.lowagie.text.Font.BOLD,
            Color.black);
    com.lowagie.text.Font titleChinese = new com.lowagie.text.Font(null, 17, com.lowagie.text.Font.BOLD,
            Color.black);


    String[] headAttr = {"aa","bb","cc","dd","ee","ff","gg"};

    int size = headAttr.length;

    Table table = new Table(size);
    int widths = 100 / size;
    int widths1[] = setWordWith(size, widths);// 设置每列宽度比例
    table.setWidths(widths1);
    table.setWidth(100);// 占页面宽度比例
    table.setAlignment(Element.ALIGN_CENTER);//居中
    table.setAlignment(Element.ALIGN_MIDDLE);//垂直居中
    table.setAutoFillEmptyCells(true);//自动填满
    table.setBorderWidth(1);//边框宽度
    table.setPadding(8);

    // 表格表题
    Paragraph p = new Paragraph("导出内容", titleChinese);
    p.setSpacingAfter(1);
    Cell cell = new Cell(p);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setBorder(0);
    cell.setColspan(size);
    table.addCell(cell);

    // 表头
    for (String column : headAttr) {
        cell = new Cell(new Paragraph(column, fontChinese));
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setBackgroundColor(lightGray);
        table.addCell(cell);
    }

    List<List<String>> content = matchHeaderData(initData(), Lists.newArrayList(headAttr));
    // 内容
    for (List<String> row : content) {
        for (String column : row) {
            cell = new Cell(new Paragraph(column, fontChinese));
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            table.addCell(cell);
        }

    }
    
    document.add(table);
    document.close();
    System.out.println("ok");
}

private static int[] setWordWith(int size, int with) {
    int[] intWidth = new int[size];
    for (int i = 0; i < size; i++) {
        intWidth[i] = with;
    }
    return intWidth;
}

  这边导出用的是Lowagie.text ,pdf导出参考《Lowagie 导出html的内容到 pdf》

总结:

  进行数据类型匹配,因为可能存在各种嵌套,会比较复杂。最好数据本身有一个类型type,根据类型去处理数据的值,这样更为合理。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天狼1222

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值