Table of Contents
Background
Home Java javaTutorial What is the method for Java to automatically generate trend comparison data?

What is the method for Java to automatically generate trend comparison data?

May 03, 2023 am 09:37 AM
java

Background

Pairwise trend comparison between data is a very common application scenario in data analysis applications, as shown below:

##202302 Class Three Years张小明130145133 202302三级一王二小128138140202302Class Three Three YearsXie Chunhua136142139202301Three-Year ClassZhang Xiaoming132140128202301Class Three Three Years王二小125146142202301Class Three Three YearsXie Chunhua138143140202212Three-Year ClassZhang Xiaoming135138120 202212三级一级王二小123145138202212Class Three Three YearsXie Chunhua136140142
Mock test Batch Class Student Chinese Mathematics English
Now there is a need: the progress and decline of each student's performance in each subject in different exam batches for each student in each class. The data obtained are as follows

Mock test batchClassStudentChineseMathematicsEnglish##Comparison between 202302 and 202301Comparison between 202302 and 202301Comparison between 202302 and 202301##Comparison between 202301 and 202212Three-year class 张小明-328Comparison between 202301 and 202212三一级王二小214202301 and 202212 ComparisonClass Three YearsXie Chunhua23-2Detailed design and implementation
Three-Year Class Zhang Xiaoming -2 5 5
三级一级 王二小 3 -8 #-2
Class Three Three Years Xie Chunhua -2 -1 -1
Trend comparison definition class 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;
    }
}
Copy after login

This class defines the following attributes:

The subject's Field list
  • Preposition field
  • Compared field list
  • For example:
  • The progress and decline of each student
in each class in different

examination batcheseach subjectThe results of mapping the above requirements to the defined class are as follows :

Subject field list (
    Class, student
  • )

    Preposition field (
  • Exam batch
  • )

    Comparison field list (various subjects:
  • Chinese, mathematics, English
  • )

    Trend ratio For the execution class
, this class provides a method for external calls as follows

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;
}
Copy after login

As you can see, this method requires the incoming

data collection
  • Trend comparison defines
  • two parameters, and finally returns the result set after trend comparison.
The internal logic of this method can be divided into the following 2 steps:

Group by subject
  • After grouping, within the group The data is compared pairwise and the comparison result is finally returned.
  • Use case
Suppose there is a set of data as follows

Define a student class:What is the method for Java to automatically generate trend comparison data?

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;
    }
}
Copy after login

We write a method to return the above data:

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;
}
Copy after login

Trend comparison definition and execution comparison:

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

The results are as follows:

{"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}
Copy after login

The above is the detailed content of What is the method for Java to automatically generate trend comparison data?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

See all articles