栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java instanceof 各种数据类型判断

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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> initData(){
    List> dataList = new ArrayList<>();
    Map dataMap = new HashMap<>();
    dataMap.put("aa", 100);
    dataMap.put("bb", "1,2,3");
    // map
    dataMap.put("cc", new HashMap(){{
        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(){{
        put("name","工单开始");
        put("state","F");
    }}, new HashMap(){{
        put("name","工单处理");
        put("state","A");
    }}));

    dataList.add(dataMap);
    return dataList;
}

 

2,匹配数据:
private static List> matchHeaderData(List> data, List 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> dyadicList = (List>) 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> mapList = (List>) object;
                        return ListUtils.emptyIfNull(mapList).stream().map(t -> MapUtils.getString(t, "name"))
                                .collect(Collectors.joining(","));
                    } else if (inner instanceof String) { // 一维字符串数组
                        List strList = (List) object;
                        return ListUtils.emptyIfNull(strList).stream().collect(Collectors.joining(","));
                    }
                }
                return "";
            } else if (object instanceof Map) { // map 类型
                Map map = (Map) 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> content = matchHeaderData(initData(), Lists.newArrayList(headAttr));
    // 内容
    for (List 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,根据类型去处理数据的值,这样更为合理。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/322418.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号