百木园-与人分享,
就是让自己快乐。

10轮伪匹配

  26名学生,每个人可以填写10个交谈对象:

10轮匹配结果:

1、pom.xml

 <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.3.RELEASE</version>
        </dependency>


        <!-- EasyExcel -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.6</version>
        </dependency>

        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version> 1.18.12</version>
            <scope>provided</scope>
        </dependency>

2、MatchingExcel 、 StudentExcel

@Data                //生成getter,setter ,toString等函数
@NoArgsConstructor   //生成无参构造函数
@AllArgsConstructor //生成全参数构造函数
public class MatchingExcel {

    @ExcelProperty(value = \"学生\")
    private String st;

    @ExcelProperty(value = \"回合1\")
    private String round1;

    @ExcelProperty(value = \"回合2\")
    private String round2;


    @ExcelProperty(value = \"回合3\")
    private String round3;

    @ExcelProperty(value = \"心动4\")
    private String round4;


    @ExcelProperty(value = \"回合5\")
    private String round5;


    @ExcelProperty(value = \"回合6\")
    private String round6;


    @ExcelProperty(value = \"回合7\")
    private String round7;

    @ExcelProperty(value = \"回合8\")
    private String round8;

    @ExcelProperty(value = \"回合9\")
    private String round9;

    @ExcelProperty(value = \"回合10\")
    private String round10;

    public boolean equals(Object anObject) {
        return (this.st == ((MatchingExcel)anObject).st);
    }

    public int hashCode() {
        return this.st.hashCode();
    }
}
@Data                //生成getter,setter ,toString等函数
@NoArgsConstructor   //生成无参构造函数
@AllArgsConstructor //生成全参数构造函数
public class StudentExcel {

    @ExcelProperty(value = \"学生\")
    private String st;

    @ExcelProperty(value = \"心动1\")
    private String like1;

    @ExcelProperty(value = \"心动2\")
    private String like2;


    @ExcelProperty(value = \"心动3\")
    private String like3;

    @ExcelProperty(value = \"心动4\")
    private String like4;


    @ExcelProperty(value = \"心动5\")
    private String like5;


    @ExcelProperty(value = \"心动6\")
    private String like6;


    @ExcelProperty(value = \"心动7\")
    private String like7;

    @ExcelProperty(value = \"心动8\")
    private String like8;

    @ExcelProperty(value = \"心动9\")
    private String like9;

    @ExcelProperty(value = \"心动10\")
    private String like10;

}

3、Student 

public class Student {

    private String id;
    private List likeIds;
    private boolean matched;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List getLikeIds() {
        return likeIds;
    }

    public void setLikeIds(List likeIds) {
        this.likeIds = likeIds;
    }

    public boolean isMatched() {
        return matched;
    }

    public void setMatched(boolean matched) {
        this.matched = matched;
    }

    @Override
    public String toString() {
        return \"Student{\" +
                \"id=\" + id +
                \", likeIds=\" + likeIds +
                \", matched=\" + matched +
                \'}\';
    }

}

4、StudentUtils

public class StudentUtils {

    public void dealData(List<MatchingExcel> matchingExcels, String from, String to, int round, Map<String, MatchingExcel> matchingExcelMap) {
        MatchingExcel matchingExcel = new MatchingExcel();
        if (round == 0 && !matchingExcelMap.containsKey(from)) { //新增数据
            matchingExcel.setSt(from);
            dealRound(matchingExcel, to, round);
            matchingExcels.add(matchingExcel);
            matchingExcelMap.put(from, matchingExcel);
        } else { //改数据
            matchingExcel = matchingExcelMap.get(from);
            dealRound(matchingExcel, to, round);
        }
    }

    public void addToMatchingExcels(List<MatchingExcel> matchingExcels, String from, String to, int round, Map<String, MatchingExcel> matchingExcelMap, List<String> matchingResultList) {
        dealData(matchingExcels, from, to, round, matchingExcelMap);
        dealData(matchingExcels, to, from, round, matchingExcelMap);
        if (from.compareTo(to) > 0) {
            matchingResultList.add(to + \"_\" + from);
        } else {
            matchingResultList.add(from + \"_\" + to);
        }
    }

    private void dealRound(MatchingExcel matchingExcel, String to, int round) {
        switch (round) {
            case 0:
                matchingExcel.setRound1(to);
                break;
            case 1:
                matchingExcel.setRound2(to);
                break;
            case 2:
                matchingExcel.setRound3(to);
                break;
            case 3:
                matchingExcel.setRound4(to);
                break;
            case 4:
                matchingExcel.setRound5(to);
                break;
            case 5:
                matchingExcel.setRound6(to);
                break;
            case 6:
                matchingExcel.setRound7(to);
                break;
            case 7:
                matchingExcel.setRound8(to);
                break;
            case 8:
                matchingExcel.setRound9(to);
                break;
            case 9:
                matchingExcel.setRound10(to);
                break;
            default:
                System.out.println(\"非法输入\");
        }
    }


    public List<MatchingExcel> getMatchingResults(List<Student> students, int maxRound) {
        if (students == null || students.size() ==0) {
            return null;
        }
        int round = 0; //第几轮
        Map<String, MatchingExcel> matchingExcelMap = new HashMap<>(students.size());
        MatchingExcel bestMatchingNum = new MatchingExcel(); //记录每轮最佳的匹配次数
        bestMatchingNum.setSt(\"每轮最佳的匹配次数\");
        int studentNum = students.size();
        List<String> matchingResultList = new ArrayList<>(maxRound); //maxRound次匹配结果 用于判断没有重复结果
        List<MatchingExcel> matchingExcels = new ArrayList<>(students.size() + 1);
        Map<String, Student> studentMap = new HashMap<>(studentNum);

        for (; round < maxRound; round++) {
            //根据id放进map
            for (Student s :  students) {
                studentMap.put(s.getId(), s);
            }
            List<String> matchingList = new ArrayList<>(students.size());
            int matchingNum = 0; //当前轮的最佳匹配次数
            Student currentStudent = null; //当前学生
            Student likePerson = null; //与当前学生匹配的学生
            for (int i = 0; i < studentNum; i++) {
                currentStudent = students.get(i);
                if (currentStudent.isMatched()) {
                    continue;
                }
                List likes = currentStudent.getLikeIds();
                if (likes != null && likes.size() > 0) {
                    for (int j = 0; j < likes.size(); j++) {
                        likePerson = studentMap.get(likes.get(j));
                        if (likePerson != null) {
                            if (new Random().nextInt(8) > 5 && !likePerson.isMatched() && likePerson.getLikeIds().contains(currentStudent.getId())) { //二个都喜欢
                                addToMatchingExcels(matchingExcels, currentStudent.getId(), likePerson.getId(), round, matchingExcelMap, matchingList);
                                studentMap.remove(currentStudent.getId());
                                studentMap.remove(likePerson.getId());
                                currentStudent.setMatched(true);
                                likePerson.setMatched(true);
                                matchingNum++;
                                break;
                            }
                        }
                    }
                }
            }

            //对studentMap再匹配
            Iterator iterator = studentMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry entry = (Map.Entry) iterator.next();
                Student student = (Student)entry.getValue();
                if (student.isMatched()) {
                    continue;
                }
                List likes = student.getLikeIds();
                if (likes != null && likes.size() > 0) {
                    for (int j = 0; j < likes.size(); j++) {
                        likePerson = studentMap.get(likes.get(j));
                        if (likePerson != null) {
                            if (!likePerson.isMatched() && likePerson.getLikeIds().contains(student.getId())) { //二个都喜欢
                                addToMatchingExcels(matchingExcels, student.getId(), likePerson.getId(), round, matchingExcelMap, matchingList);
                                student.setMatched(true);
                                likePerson.setMatched(true);
                                matchingNum++;
                                break;
                            }
                        }
                    }
                }
            }

            //移除再匹配成功的学生
            for (Iterator<Map.Entry<String, Student>> iter = studentMap.entrySet().iterator(); iter.hasNext();){
                Map.Entry<String, Student> item = iter.next();
                if (item.getValue().isMatched()) {
                    iter.remove();
                }
            }

            //剩下对studentMap 打乱匹配
            int mapNum = studentMap.size();
            if (mapNum != 0) {
                String from = null;
                String to = null;
                for (int l = 0; l < mapNum; l++) {
                    String randomValue = getRandomFromMap(studentMap);
                    if (randomValue == null) {
                        break;
                    }
                    if (l % 2 == 0) {
                        from = randomValue;
                    } else {
                        to = randomValue;
                        addToMatchingExcels(matchingExcels, from, to, round, matchingExcelMap, matchingList);
                    }

                }
            }

            dealRound(bestMatchingNum, String.valueOf(matchingNum), round);

            matchingList.sort(Comparator.naturalOrder()); //排序

            String matchingListString = matchingList.toString();

            if (matchingResultList.contains(matchingListString)) {
                System.out.println(\"重复结果了\");
                round --; //这轮不算
                continue;
            } else  {
                matchingResultList.add(matchingListString);
                matchingResultList.contains(matchingListString);
            }

            for (Student p :  students) {
                p.setMatched(false);
            }
        }
        matchingExcels.sort(Comparator.comparing(MatchingExcel::getSt));
        matchingExcels.add(bestMatchingNum);
        return matchingExcels;
    }

    public String getRandomFromMap(Map map) {
        Random generator = new Random();
        Object[] values = map.keySet().toArray();
        if (values == null || values.length ==0) {
            return null;
        }
        String id = (String) values[generator.nextInt(values.length)];
        map.remove(id);
        return id;
    }

    public static void main(String[] args) throws FileNotFoundException {
        String filePath =  ResourceUtils.getURL(\"classpath:\").getPath().toString() +\"交友匹配模板.xlsx\";
        List<StudentExcel> studentExcels = EasyExcel.read(filePath).head(StudentExcel.class).sheet().doReadSync();
        if (studentExcels == null && studentExcels.size() == 0) {
            return;
        }

        List<Student> students = new ArrayList<>(studentExcels.size());
        for (int i = 0; i < studentExcels.size(); i++) {
            Student student = new Student();
            StudentExcel studentExcel = studentExcels.get(i);
            student.setId(studentExcel.getSt());
            List<String> likes = new ArrayList<>(10);
            likes.add(studentExcel.getLike1());
            likes.add(studentExcel.getLike2());
            likes.add(studentExcel.getLike3());
            likes.add(studentExcel.getLike4());
            likes.add(studentExcel.getLike5());
            likes.add(studentExcel.getLike6());
            likes.add(studentExcel.getLike7());
            likes.add(studentExcel.getLike9());
            likes.add(studentExcel.getLike8());
            likes.add(studentExcel.getLike10());
            student.setLikeIds(likes);
            students.add(student);
        }

        StudentUtils studentUtils = new StudentUtils();
        List<MatchingExcel> matchingResults = studentUtils.getMatchingResults(students, 10);

        String destPath =  ResourceUtils.getURL(\"classpath:\").getPath().toString() + \"交友匹配.xlsx\";

        // 生成工作簿对象
        ExcelWriterBuilder workBookWriter = EasyExcel.write(new File(destPath)).withTemplate(filePath);
        // 创建工作表对象
        ExcelWriterSheetBuilder sheet = workBookWriter.sheet(\"输出\");
        sheet.doFill(matchingResults);
    }

}

5、Excel

https://gitee.com/weiguodong2017/like/blob/master/src/main/resource/%E4%BA%A4%E5%8F%8B%E5%8C%B9%E9%85%8D%E6%A8%A1%E6%9D%BF.xlsx


来源:https://www.cnblogs.com/weiapro/p/16200739.html
本站部分图文来源于网络,如有侵权请联系删除。

未经允许不得转载:百木园 » 10轮伪匹配

相关推荐

  • 暂无文章