文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Java自动生成趋势比对数据的方法分享

2023-05-14 21:02

关注

背景

数据之间两两趋势比较在数据分析应用中是非常常见的应用场景,如下所示:

模拟考批次班级学生语文数学英语
202302三年一班张小明130145133
202302三年一班王二小128138140
202302三年一班谢春花136142139
202301三年一班张小明132140128
202301三年一班王二小125146142
202301三年一班谢春花138143140
202212三年一班张小明135138120
202212三年一班王二小123145138
202212三年一班谢春花136140142

现在有一个需求:各班级的每个学生在不同考试批次的各学科成绩的进退步情况,得出数据如下

模拟考批次班级学生语文数学英语
202302与202301对比三年一班张小明-255
202302与202301对比三年一班王二小3-8-2
202302与202301对比三年一班谢春花-2-1-1
202301与202212对比三年一班张小明-328
202301与202212对比三年一班王二小214
202301与202212对比三年一班谢春花23-2

详细设计及实现

趋势比对定义类 TrendCompare

public class TrendCompare {
    
    private String[] subjectFields;
    
    private String atField;

    
    private String[] compareFields;

    
    private Map<String, String> assignValMap;

    public String[] subjectFields() {
        return this.subjectFields;
    }

    public TrendCompare subjectFields(String... fields) {
        this.subjectFields = fields;
        return this;
    }

    public String atField() {
        return this.atField;
    }

    public TrendCompare atField(String field) {
        this.atField = field;
        return this;
    }

    public String[] compareFields() {
        return this.compareFields;
    }

    public TrendCompare compareFields(String... fields) {
        this.compareFields = fields;
        return this;
    }

    
    public TrendCompare assignVal(String field, String valueEL) {
        if (assignValMap == null) {
            assignValMap = new HashMap<>();
        }
        assignValMap.put(field, valueEL);
        return this;
    }

    public Map<String, String> assignValMap() {
        return this.assignValMap;
    }
}

该类定义了如下属性:

如:各班级的每个学生在不同考试批次各学科成绩的进退步情况

上面的需求映射到定义类的结果如下:

趋势比对执行类

该类提供了一个供外部调用的方法如下

public static <T> List<T> compare(List<T> dataList, TrendCompare trendCompare) {
    Map<String, List<T>> groupMap = group(dataList, null, trendCompare.subjectFields());
    List<T> resultList = new ArrayList<>();
    for (List<T> groupDataList : groupMap.values()) {
        List<T> diffValueList = new ArrayList<>();
        int size = groupDataList.size();
        if (size > 1) {
            for (int i = 0; i < size - 1; i++) {
                //数据之间两两比较 diffValue = minuend - subtrahend
                T minuend = groupDataList.get(i);
                T subtrahend = groupDataList.get(i + 1);
                T diffValue = minus(trendCompare.compareFields(), minuend, subtrahend);
                //设置主体信息
                if (trendCompare.subjectFields() != null) {
                    for (String subjectField : trendCompare.subjectFields()) {
                        setFieldValue(diffValue, subjectField, getFieldValue(minuend, subjectField));
                    }
                }
                //设置介词字段信息
                String atField = trendCompare.atField();
                if (StringUtils.isNotEmpty(atField)) {
                    setFieldValue(diffValue, atField, getFieldValue(minuend, atField) + "与" + getFieldValue(subtrahend, atField) + "对比增减");
                }
                diffValueList.add(diffValue);
            }
        }
        if (diffValueList.size() > 0) {
            T firstData = groupDataList.get(0);
            Map<String, Object> valMap = new HashMap<>();
            //指定的赋值集合进行赋值
            if (trendCompare.assignValMap() != null) {
                for (Map.Entry<String, String> stringStringEntry : trendCompare.assignValMap().entrySet()) {
                    String field = stringStringEntry.getKey();
                    if (!StringUtils.equalsAny(field, trendCompare.compareFields())) {
                        String valueEL = stringStringEntry.getValue();
                        valMap.put(field, executeSpEL(valueEL, firstData));
                    }
                }
            }
            for (Map.Entry<String, Object> entry : valMap.entrySet()) {
                for (T diffValue : diffValueList) {
                    setFieldValue(diffValue, entry.getKey(), entry.getValue());
                }
            }
        }
        resultList.addAll(diffValueList);
    }
    return resultList;
}

可以看到,该方法要求传入

两个参数,并最终返回趋势比对后的结果集合。

该方法的内部逻辑可分为如下2个步骤:

使用案例

假设有如下这样一组数据

定义一个学生类:

public class Student {
    private String batch;
    private String banji;
    private String studentNo;
    private String name;
    private String sex;
    private Double yuwen;
    private Double math;
    private Double english;
    private Double physics;
    //extra
    private String maxScoreName1;
    public Student(String batch, String banji, String studentNo, String name, String sex, Double yuwen, Double math, Double english, Double physics) {
        this.batch = batch;
        this.banji = banji;
        this.studentNo = studentNo;
        this.name = name;
        this.sex = sex;
        this.yuwen = yuwen;
        this.math = math;
        this.english = english;
        this.physics = physics;
    }
}

我们写一个方法,返回如上数据:

public List<Student> getDataList() {
    List<Student> dataList = new ArrayList<>();
    dataList.add(new Student("202302", "三年一班", "20001001", "张小明", "男", 130.0, 145.0, 133.0, 92.0));
    dataList.add(new Student("202302", "三年一班", "20001002", "王二小", "男", 128.0, 138.0, 140.0, 98.0));
    dataList.add(new Student("202302", "三年一班", "20001003", "谢春花", "女", 136.0, 142.0, 139.0, 95.0));
    dataList.add(new Student("202302", "三年二班", "20002001", "冯世杰", "男", 129.0, 144.0, 138.0, 96.0));
    dataList.add(new Student("202302", "三年二班", "20002002", "马功成", "男", 130.0, 132.0, 133.0, 98.0));
    dataList.add(new Student("202302", "三年二班", "20002003", "魏翩翩", "女", 136.0, 142.0, 137.0, 92.0));
    dataList.add(new Student("202301", "三年一班", "20001001", "张小明", "男", 132.0, 142.0, 134.0, 92.0));
    dataList.add(new Student("202301", "三年一班", "20001002", "王二小", "男", 126.0, 136.0, 135.0, 94.0));
    dataList.add(new Student("202301", "三年一班", "20001003", "谢春花", "女", 136.0, 145.0, 139.0, 95.0));
    dataList.add(new Student("202301", "三年二班", "20002001", "冯世杰", "男", 124.0, 143.0, 148.0, 90.0));
    dataList.add(new Student("202301", "三年二班", "20002002", "马功成", "男", 140.0, 133.0, 138.0, 90.0));
    dataList.add(new Student("202301", "三年二班", "20002003", "魏翩翩", "女", 126.0, 136.0, 135.0, 92.0));
    return dataList;
}

趋势比对定义并执行比对:

List<Student> dataList = getDataList();
TrendCompare trendCompare = new TrendCompare()
        .subjectFields("banji", "name")
        .atField("batch")
        .compareFields("yuwen", "math", "english")
        //.assignVal("batch", "'环比增减'")
        ;
List<Student> resultList = DataProcessUtil.compare(dataList, trendCompare);
for (Student result : resultList) {
    System.out.println(JSON.toJSONString(result));
}

结果如下:

{"banji":"三年一班","batch":"202302与202301对比增减","english":-1.0,"math":3.0,"name":"张小明","yuwen":-2.0}
{"banji":"三年一班","batch":"202302与202301对比增减","english":5.0,"math":2.0,"name":"王二小","yuwen":2.0}
{"banji":"三年一班","batch":"202302与202301对比增减","english":0.0,"math":-3.0,"name":"谢春花","yuwen":0.0}
{"banji":"三年二班","batch":"202302与202301对比增减","english":-10.0,"math":1.0,"name":"冯世杰","yuwen":5.0}
{"banji":"三年二班","batch":"202302与202301对比增减","english":-5.0,"math":-1.0,"name":"马功成","yuwen":-10.0}
{"banji":"三年二班","batch":"202302与202301对比增减","english":2.0,"math":6.0,"name":"魏翩翩","yuwen":10.0}

以上就是Java自动生成趋势比对数据的方法分享的详细内容,更多关于Java生成趋势比对数据的资料请关注编程网其它相关文章!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯