应用场景:对于考试试卷选择题选项的乱序对应问题,防止考生作弊,每个人的题目都是不一样的选项顺序。
package com.muyuan.platform.elearning.util;
import com.alibaba.fastjson.JSONObject;
import com.muyuan.platform.elearning.vo.exam.QuestionVo;
import java.util.*;
/**
* 乱序工具类
*/
public class QuestionSelectRevertUtils {
/**
* 打乱Map key-value顺序,重新组合key-value
*
* @param questionJson
* @return
*/
public static Map<String, String> toDisOrder(String questionJson) {
QuestionVo questionVo = JSONObject.parseObject(questionJson, QuestionVo.class);
Map<String, String> map = questionVo.getOptionsMap();
String correctValue = map.get(questionVo.getCorrectOptions());
List<String> valueList = new ArrayList<>();
List<String> keyList = new ArrayList<>();
for (Map.Entry<String, String> entry : map.entrySet()) {
keyList.add(entry.getKey());
valueList.add(entry.getValue());
}
Collections.shuffle(valueList);
HashMap<String, String> revertMap = new HashMap<>();
for (int i = 0; i < keyList.size(); i++) {
revertMap.put(keyList.get(i), valueList.get(i));
}
QuestionVo vo = new QuestionVo();
for (Map.Entry<String, String> entry : revertMap.entrySet()) {
if (entry.getValue().equals(correctValue)) {
vo.setCorrectOptions(entry.getKey());
vo.setCorrectValue(correctValue);
}
}
vo.setOptionsMap(revertMap);
vo.setQuestionContent(questionVo.getQuestionContent());
System.out.println("revertMap: " + JSONObject.toJSON(vo));
return revertMap;
}
public static String setOptions() {
QuestionVo questionVo = new QuestionVo();
questionVo.setQuestionContent("中华人民共和国的首都是哪里?");
Map<String, String> map = new HashMap<>();
map.put("A", "北京");
map.put("B", "天津");
map.put("C", "上海");
map.put("D", "重庆");
questionVo.setOptionsMap(map);
questionVo.setCorrectOptions("A");
questionVo.setCorrectValue("北京");
System.out.println("originMap: " + JSONObject.toJSON(questionVo));
return JSONObject.toJSONString(questionVo);
}
public static void main(String[] args) {
String s = setOptions();
System.out.println("\r\n");
int i = 0;
while (i < 10) {
toDisOrder(s);
i++;
}
}
}
输出结果:
originMap: {"questionContent":"中华人民共和国的首都是哪里?","correctValue":"北京","optionsMap":{"A":"北京","B":"天津","C":"上海","D":"重庆"},"correctOptions":"A"}
revertMap: {"questionContent":"中华人民共和国的首都是哪里?","correctValue":"北京","optionsMap":{"A":"重庆","B":"上海","C":"天津","D":"北京"},"correctOptions":"D"}
revertMap: {"questionContent":"中华人民共和国的首都是哪里?","correctValue":"北京","optionsMap":{"A":"北京","B":"上海","C":"重庆","D":"天津"},"correctOptions":"A"}
revertMap: {"questionContent":"中华人民共和国的首都是哪里?","correctValue":"北京","optionsMap":{"A":"上海","B":"重庆","C":"天津","D":"北京"},"correctOptions":"D"}
revertMap: {"questionContent":"中华人民共和国的首都是哪里?","correctValue":"北京","optionsMap":{"A":"重庆","B":"北京","C":"天津","D":"上海"},"correctOptions":"B"}
revertMap: {"questionContent":"中华人民共和国的首都是哪里?","correctValue":"北京","optionsMap":{"A":"北京","B":"天津","C":"上海","D":"重庆"},"correctOptions":"A"}
revertMap: {"questionContent":"中华人民共和国的首都是哪里?","correctValue":"北京","optionsMap":{"A":"天津","B":"北京","C":"上海","D":"重庆"},"correctOptions":"B"}
revertMap: {"questionContent":"中华人民共和国的首都是哪里?","correctValue":"北京","optionsMap":{"A":"上海","B":"重庆","C":"天津","D":"北京"},"correctOptions":"D"}
revertMap: {"questionContent":"中华人民共和国的首都是哪里?","correctValue":"北京","optionsMap":{"A":"北京","B":"重庆","C":"天津","D":"上海"},"correctOptions":"A"}
revertMap: {"questionContent":"中华人民共和国的首都是哪里?","correctValue":"北京","optionsMap":{"A":"上海","B":"重庆","C":"北京","D":"天津"},"correctOptions":"C"}
revertMap: {"questionContent":"中华人民共和国的首都是哪里?","correctValue":"北京","optionsMap":{"A":"上海","B":"重庆","C":"北京","D":"天津"},"correctOptions":"C"}