首页 > 解决方案 > 如何为两个或三个属性或属性数量制作下面的代码泛型

问题描述

"pmStudent": [
        {
            "id": 31506,
            "name": "sushant",
            "collageId": 12,
            "collageName": "PVP Baramati",
            "hodId": 114,
            "hodName": "Yogesh"
        },
         {
            "id": 31506,
            "name": "sushant",
            "collageId": 12,
            "collageName": "SCSV murti",
            "hodId": 114,
            "hodName": "Yogesh"
        },
        {
            "id": 41507,
            "name": "mahesh",
            "collageId": 12,
            "collageName": "SCSV murti",
            "hodId": 114,
            "hodName": "Yogesh"
        }
    ]

我有以下代码,它根据三个字段检查重复项,如何使此代码通用?所以可以用于另一个对象,如教师。重复检查可以基于对象学生三个字段,对象教师两个字段,其他对象多个字段?

protected List<Student> extractStudentDuplicates(final List<Student> studentList) {

        return studentList.stream().flatMap(i -> {
            final AtomicInteger count = new AtomicInteger();
            final List<Student> duplicatedStu = new ArrayList<>();
            studentList.forEach(p -> {
                if (null != p.getId() && null != p.getCollageId() && null != p.getHodId()) {
                    if (p.getId().equals(i.p.getId()) && p.getCollageId().equals(i.getCollageId())
                            && p.getCollageId().equals(i.getCollageId())) {
                        count.getAndIncrement();
                    }
                    if (count.get() == 2) {
                        duplicatedStu.add(i);
                    }
                }
            });
            return duplicatedStu.stream();
        }).distinct().collect(Collectors.toList());
    }

标签: generics

解决方案


推荐阅读