首頁 > 軟體

Java自動生成趨勢比對資料的方法分享

2023-04-08 06:01:34

背景

資料之間兩兩趨勢比較在資料分析應用中是非常常見的應用場景,如下所示:

模擬考批次班級學生語文數學英語
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;

    /**
     * 賦值對映集合:給結果資料中指定的key設定指定的值
     */
    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;
    }

    /**
     * 賦值操作
     *
     * @param field
     * @param valueEL 值表示式
     * @return
     */
    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生成趨勢比對資料的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com