List集合格式转换

最近遇到一个任务:

需要把A集合数据转成 B集合的形式:

A集合:

List集合格式转换_第1张图片

B集合:

List集合格式转换_第2张图片

代码:

package com.example.juc.test;

import com.example.juc.entity.Ld;
import com.example.juc.entity.Student;

import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author 
 * @Date Created in  2023/12/26 14:43
 * @DESCRIPTION:
 * @Version V1.0
 */

public class 测试行转列 {
    private static final Map SFID_MAPPING = new HashMap<>();

    // 初始化映射关系
    static {
        SFID_MAPPING.put("ywjbrmc", "1");
        SFID_MAPPING.put("ywfgld", "2");
        SFID_MAPPING.put("ywzzld", "3");
        SFID_MAPPING.put("fgxld", "4");
        SFID_MAPPING.put("zyxld", "5");
    }

    public static void main(String[] args) {
   





        List list = new CopyOnWriteArrayList<>();

        list.add(new Ld( "龙德(longyd3)", "夏天(longyd3)", "黄美(hyanm)", "王雄(wgx)", "黄美(hyanm)"));
        list.add(new Ld( "", "", "王国雄(wgx),黄美(hanm),龙永德(longyd3)", "", ""));
        list.add(new Ld( "", "", "", "黄美(hanm)", ""));
        list.add(new Ld("", "", "", "", "黄美(hanm)"));
        list.add(new Ld("", "", "", "", "黄燕美(hanm)"));
        List okList = new ArrayList<>();
        //遍历第一个集合
        for (Ld ld : list) {
            // 在这里处理 ld 对象
            // 使用反射获取所有字段
            Field[] fields = Ld.class.getDeclaredFields();
            String id  = UUID.randomUUID().toString();
            for (Field field : fields) {
                // 设置可访问,因为字段可能是 private 的
                field.setAccessible(true);
                try {
                    Object value = field.get(ld);
                    HashMap map = new HashMap<>(16);

                    if (value != null && !value.toString().isEmpty()) {
                        Student student = new Student();
                        String name = field.getName();
                        map.put(name,value);

                        student.setPzryId(id);
                        student.setNetId(map.get(name).toString());
                        // 使用映射设置 sfid
                        String sFid = SFID_MAPPING.get(name);
                        if (sFid != null) {
                            student.setSfid(sFid);
                        }
                        okList.add(student);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }

        }
        List updateList = new CopyOnWriteArrayList<>();
        List errorList = new CopyOnWriteArrayList<>();
        System.out.println("====================原本转好的集合============================");
        for (Student stu : okList){
//

            String netId = stu.getNetId();

            String regex = "^[^()]+\\([^()]+\\)$";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(netId);
            // 如果不符合格式,则打印该 Student 对象
            if (!matcher.matches()) {
                errorList.add(stu);
                String[] split = stu.getNetId().split(",");

                for (int i = 0; i < split.length; i++) {
                    Student student = new Student();
                    student.setPzryId(stu.getPzryId());
                    student.setNetId(split[i]);
                    student.setSfid(stu.getSfid());
                    updateList.add(student);
                }
            }
            System.out.println(stu);
        }
        System.out.println("--------------------------修改后的数据-------------------------------------");
        for (Student s : updateList){
            System.out.println(s);
        }
        okList.addAll(updateList);
        System.out.println("--------------------------不符合规则的数据-------------------------------------");
        for (Student s : errorList){
            System.out.println(s);
        }
        okList.removeAll(errorList);
        System.out.println("--------------------最后的数据------------------------------------");

        for (Student s : okList){
            System.out.println(s);
        }
    }
}

打印结果:

List集合格式转换_第3张图片

你可能感兴趣的:(list,python,windows)