List集合格式转换

2023-12-27 13:09:06

最近遇到一个任务:

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

A集合:

B集合:

代码:

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);
        }
    }
}

打印结果:

文章来源:https://blog.csdn.net/XikYu/article/details/135241329
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。