List<对象>转换Map<String,List<对象>>方案

前提:List<对象>转换成Map<String,List<对象>>,对象中存在type字段,依据type为Map的key,进行数据的拆分

public class File {

    private static final long serialVersionUID=1L;

    private String id= new ObjectId().toHexString();
    private String size;
    private String name;
    private String desc;

    private String originName;
    private String userId;
    private String businessId;

    /**
     * 类型包括:educationCertificates、kpiReports、license、medicalTest、meetingEvaluationReports、photo、staffContract、passportAttachment、identityCardAttachment、householdRegister、competitionCertificate、bonetest几种类别,和userId配合使用
     */
    private String businessType;

    private String url;
    
    private String snapshot;


    @Override
    protected Serializable pkVal() {
        return this.id;
    }

}
public class UserExt {
    private List<File> identityCardAttachment;
    private List<File> passportAttachment;
    private List<File> photo;
    private List<File> bonetest;
    private List<File> medicalTest;
    private List<File> schoolRegistration;
    private List<File> socialInsurance;
    private List<File> householdRegister;
    private List<File> competitionCertificate;
    private List<File> transferCertificate;
    private List<File> meetingEvaluationReports;   
    private List<File> staffContract;               
    private List<File> kpiReports;                  
    private List<File> license;                   
    private List<File> educationCertificates;      
}

方案1:

public enum FileTypeEnum {

    /**
     * workitem
     */
    Workitem(1, "workitem"),
    /**
     * exercise
     */
    Exercise(2, "exercise"),

    /**
     * photo
     */
    Photo(3,"photo"),

    /**
     * IdentityCardAttachment
     */
    IdentityCardAttachment(4,"identityCardAttachment"),

    /**
     * MedicalTest
     */
    MedicalTest(5,"medicalTest"),

    /**
     * MeetingEvaluationReports
     */
    MeetingEvaluationReports(6,"meetingEvaluationReports"),

    /**
     * StaffContract
     */
    StaffContract(7,"staffContract"),

    /**
     * KpiReports
     */
    KpiReports(8,"kpiReports"),

    /**
     * EducationCertificates
     */
    EducationCertificates(9,"educationCertificates"),

    /**
     * License
     */
    License(10,"license"),

    /**
     * PassportAttachment
     */
    PassportAttachment(11,"passportAttachment"),

    /**
     * HouseholdRegister
     */
    HouseholdRegister(12,"householdRegister"),

    /**
     * CompetitionCertificate
     */
    CompetitionCertificate(13,"competitionCertificate"),

    /**
     * Bonetest
     */
    Bonetest(14,"bonetest"),

    /**
     * schoolRegistrationFiles
     */
    SchoolRegistrationFiles(15, "schoolRegistrationFiles"),

    /**
     * socialInsuranceFiles
     */
    SocialInsuranceFiles(16,"socialInsuranceFiles"),

    /**
     * transferCertificateFiles
     */
    TransferCertificateFiles(17,"transferCertificateFiles");



    private Integer value;

    private String name;


    FileTypeEnum(Integer value, String name) {
        this.value = value;
        this.name = name;
    }

    public Integer getValue() {
        return value;
    }


    public void setValue(Integer value) {
        this.value = value;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }
}

public Map<String, List<File>> getFileList(List<File> fileList) {
        Map<String, List<File>> map = new HashMap<>(20);
        List<File> bonetestFiles = new ArrayList<>();
        List<File> meetingEvaluationReportsFiles = new ArrayList<>();
        List<File> staffContractFiles = new ArrayList<>();
        List<File> kpiReportsFiles = new ArrayList<>();
        List<File> licenseFiles = new ArrayList<>();
        List<File> educationCertificatesFiles = new ArrayList<>();
        List<File> identityCardAttachmentFiles = new ArrayList<>();
        List<File> passportAttachmentFiles = new ArrayList<>();
        List<File> photoFiles = new ArrayList<>();
        List<File> medicalTestFiles = new ArrayList<>();
        List<File> schoolRegistrationFiles = new ArrayList<>();
        List<File> socialInsuranceFiles = new ArrayList<>();
        List<File> householdRegisterFiles = new ArrayList<>();
        List<File> competionCertificateFiles = new ArrayList<>();
        List<File> transferCertificateFiles = new ArrayList<>();
        fileList.forEach(e -> {
            if (e.getBusinessType().equals(FileTypeEnum.Bonetest.getName())) {
                bonetestFiles.add(e);
            }
            if (e.getBusinessType().equals(FileTypeEnum.CompetitionCertificate.getName())) {
                competionCertificateFiles.add(e);
            }
            if (e.getBusinessType().equals(FileTypeEnum.EducationCertificates.getName())) {
                educationCertificatesFiles.add(e);
            }
            if (e.getBusinessType().equals(FileTypeEnum.HouseholdRegister.getName())) {
                householdRegisterFiles.add(e);
            }
            if (e.getBusinessType().equals(FileTypeEnum.IdentityCardAttachment.getName())) {
                identityCardAttachmentFiles.add(e);
            }
            if (e.getBusinessType().equals(FileTypeEnum.KpiReports.getName())) {
                kpiReportsFiles.add(e);
            }
            if (e.getBusinessType().equals(FileTypeEnum.License.getName())) {
                licenseFiles.add(e);
            }
            if (e.getBusinessType().equals(FileTypeEnum.MedicalTest.getName())) {
                medicalTestFiles.add(e);
            }
            if (e.getBusinessType().equals(FileTypeEnum.MeetingEvaluationReports.getName())) {
                meetingEvaluationReportsFiles.add(e);
            }
            if (e.getBusinessType().equals(FileTypeEnum.PassportAttachment.getName())) {
                passportAttachmentFiles.add(e);
            }
            if (e.getBusinessType().equals(FileTypeEnum.Photo.getName())) {
                photoFiles.add(e);
            }
            if (e.getBusinessType().equals(FileTypeEnum.StaffContract.getName())) {
                staffContractFiles.add(e);
            }
            if (e.getBusinessType().equals(FileTypeEnum.SchoolRegistrationFiles.getName())) {
                schoolRegistrationFiles.add(e);
            }
            if (e.getBusinessType().equals(FileTypeEnum.SocialInsuranceFiles.getName())) {
                socialInsuranceFiles.add(e);
            }
            if (e.getBusinessType().equals(FileTypeEnum.TransferCertificateFiles.getName())) {
                transferCertificateFiles.add(e);
            }
        });
        map.put("bonetest", bonetestFiles);
        map.put("competionCertificate", competionCertificateFiles);
        map.put("educationCertificates", educationCertificatesFiles);
        map.put("householdRegister", householdRegisterFiles);
        map.put("identityCardAttachment", identityCardAttachmentFiles);
        map.put("kpiReports", kpiReportsFiles);
        map.put("license", licenseFiles);
        map.put("medicalTest", medicalTestFiles);
        map.put("meetingEvaluationReports", meetingEvaluationReportsFiles);
        map.put("passportAttachment", passportAttachmentFiles);
        map.put("photo", photoFiles);
        map.put("staffContract", staffContractFiles);
        map.put("schoolRegistration", schoolRegistrationFiles);
        map.put("socialInsurance", socialInsuranceFiles);
        map.put("transferCertificate", transferCertificateFiles);
        return map;
    }

上述方案缺点大家应该一眼就能看出来,代码过于长,已经超过了阿里规约所提出的80行,所以我们可以忽略这种方案
方案2:
流式操作

Map<String, List<File>> fileMap = fileList.stream().collect(Collectors.groupingBy(File::getBusinessType));

反射方式将值赋给对象

Class clazz = UserExt.class;
UserExt userExt=new UserExt();
Field[] fields = clazz.getDeclaredFields();
for (Field f : fields) {
    if (fileMap.containsKey(f.getName())) {
        f.setAccessible(true);// 注意一定要加,因为实体类的属性是私有的,而且没有get和set方法(除非你重写了)
        f.set(userExt,fileMap.get(f.getName()));
    }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

new_repo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值