List集合格式转换

最近遇到一个任务:

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

A集合:

B集合:

代码:

java 复制代码
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<String, String> 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<Ld> 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<Student> 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<String, Object> 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<Student> updateList = new CopyOnWriteArrayList<>();
        List<Student> 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);
        }
    }
}

打印结果:

相关推荐
江沉晚呤时4 小时前
在 C# 中调用 Python 脚本:实现跨语言功能集成
python·microsoft·c#·.net·.netcore·.net core
xchenhao5 小时前
基于 Flutter 的开源文本 TTS 朗读器(支持 Windows/macOS/Android)
android·windows·flutter·macos·openai·tts·朗读器
电脑能手5 小时前
如何远程访问在WSL运行的Jupyter Notebook
ide·python·jupyter
Edward-tan6 小时前
CCPD 车牌数据集提取标注,并转为标准 YOLO 格式
python
老胖闲聊6 小时前
Python I/O 库【输入输出】全面详解
开发语言·python
倔强青铜三6 小时前
苦练Python第18天:Python异常处理锦囊
人工智能·python·面试
倔强青铜三6 小时前
苦练Python第17天:你必须掌握的Python内置函数
人工智能·python·面试
迷路爸爸1807 小时前
让 VSCode 调试器像 PyCharm 一样显示 Tensor Shape、变量形状、变量长度、维度信息
ide·vscode·python·pycharm·debug·调试
帽儿山的枪手7 小时前
追踪网络流量就这么简单 | 进阶篇 | conntrack
linux·windows·网络协议
咸鱼鲸8 小时前
【PyTorch】PyTorch中的数据预处理操作
人工智能·pytorch·python