JAVA代码优化?

有没有代码写的漂亮的大佬,看看这个代码怎么优化,一直写前端的,突然被叫去搞java,发现很多技术都不太相同,例如动态的key去调用之类,导致写出这样的恶心代码,自己都看不下去了

List<String> questionCreateBySingleIdList = (List<String>) examinationCreatePojo.getQuestionCreateBySingleIdList();

List<String> questionCreateByMultipleIdList = (List<String>) examinationCreatePojo.getQuestionCreateByMultipleIdList();

List<String> questionCreateByJudgmentIdList = (List<String>) examinationCreatePojo.getQuestionCreateByJudgmentIdList();

List<String> questionCreateBySubjectiveIdList = (List<String>) examinationCreatePojo.getQuestionCreateBySubjectiveIdList();

if (questionCreateBySingleIdList.size() > 0) {

Map map1 = questionCreateBySingleService.postBatchAdd(questionCreateBySingleIdList);

Integer i1 = (Integer) map1.get("model");

if (i1 == 0) {

throw new Error("单选题题目生成错误");

} else {

List<QuestionCreateBySinglePojo> list1 = (List<QuestionCreateBySinglePojo>) map1.get("list");

List<String> idList1 = new ArrayList<>();

for (QuestionCreateBySinglePojo item1 : list1) {

idList1.add(item1.getId());

}

examinationCreatePojo.setQuestionCreateBySingleIdList(idList1);

}

}

if (questionCreateByMultipleIdList.size() > 0) {

Map map2 = questionCreateByMultipleService.postBatchAdd(questionCreateByMultipleIdList);

Integer i2 = (Integer) map2.get("model");

if (i2 == 0) {

throw new Error("多选题题目生成错误");

} else {

List<QuestionCreateByMultiplePojo> list2 = (List<QuestionCreateByMultiplePojo>) map2.get("list");

List<String> idList2 = new ArrayList<>();

for (QuestionCreateByMultiplePojo item2 : list2) {

idList2.add(item2.getId());

}

examinationCreatePojo.setQuestionCreateByMultipleIdList(idList2);

}

}

if (questionCreateByJudgmentIdList.size() > 0) {

Map map3 = questionCreateByJudgmentService.postBatchAdd(questionCreateByJudgmentIdList);

Integer i3 = (Integer) map3.get("model");

if (i3 == 0) {

throw new Error("判断题题目生成错误");

} else {

List<QuestionCreateByJudgmentPojo> list3 = (List<QuestionCreateByJudgmentPojo>) map3.get("list");

List<String> idList3 = new ArrayList<>();

for (QuestionCreateByJudgmentPojo item3 : list3) {

idList3.add(item3.getId());

}

examinationCreatePojo.setQuestionCreateByJudgmentIdList(idList3);

}

}

if (questionCreateBySubjectiveIdList.size() > 0) {

Map map4 = questionCreateBySubjectiveService.postBatchAdd(questionCreateBySubjectiveIdList);

Integer i4 = (Integer) map4.get("model");

if (i4 == 0) {

throw new Error("主观题题目生成错误");

} else {

List<QuestionCreateBySubjectivePojo> list4 = (List<QuestionCreateBySubjectivePojo>) map4.get("list");

List<String> idList4 = new ArrayList<>();

for (QuestionCreateBySubjectivePojo item4 : list4) {

idList4.add(item4.getId());

}

examinationCreatePojo.setQuestionCreateBySubjectiveIdList(idList4);

}

}

明明js可以写的这么短小优雅,java有没有办法做到这样子的呢

function getKeyName(key) {

let v0 = key.split("questionCreateBy")[1];

let v1 = v0.split("IdList")[0];

return v1;

}

function main() {

let listMap = {

questionCreateBySingleIdList:examinationCreatePojo.questionCreateBySingleIdList,

questionCreateByMultipleIdList:examinationCreatePojo.questionCreateByMultipleIdList,

questionCreateByJudgmentIdList:examinationCreatePojo.questionCreateByJudgmentIdList,

questionCreateBySubjectiveIdList:examinationCreatePojo.questionCreateBySubjectiveIdList,

};

let errMap = {

'Single': '单选题题目生成错误',

'Multiple': '多选题题目生成错误',

'Judgment': '判断题题目生成错误',

'Subjective': '主观题题目生成错误',

}

let keyName = "";

for (const key in listMap) {

const item = listMap[key];

if (item.length > 0) {

keyName = getKeyName(key);

let map = httpMap[`questionCreateBy${keyName}Service`].postBatchAdd(item);

let i = map.model;

if (i === 0) {

throw new Error(errMap[keyName]);

} else {

let list = map.list;

let idList = [];

list.map((v) => idList.push(v.id));

examinationCreatePojo[`questionCreateBy${keyName}IdList`] = idList;

}

}

}

}


回答:

private List<String> postQuestionIds(List<String> idList, BaseService<?, String> service, String error) {

List<String> result = new ArrayList<>();

if (idList.size() > 0) {

Map map = service.postBatchAdd(idList);

Integer model = (Integer) map.get("model");

if (model == 0) {

throw new Error(error + "题目生成错误");

} else {

List<?> list = (List<?>) map.get("list");

for (Object item : list) {

result.add((String) ((Map<?, ?>) item).get("id"));

}

}

}

return result;

}

List<String> idList1 = postQuestionIds(questionCreateBySingleIdList, questionCreateBySingleService, "单选");

examinationCreatePojo.setQuestionCreateBySingleIdList(idList1);

List<String> idList2 = postQuestionIds(questionCreateByMultipleIdList, questionCreateByMultipleService, "多选");

examinationCreatePojo.setQuestionCreateByMultipleIdList(idList2);

List<String> idList3 = postQuestionIds(questionCreateByJudgmentIdList, questionCreateByJudgmentService, "判断");

examinationCreatePojo.setQuestionCreateByJudgmentIdList(idList3);

List<String> idList4 = postQuestionIds(questionCreateBySubjectiveIdList, questionCreateBySubjectiveService, "主观");

examinationCreatePojo.setQuestionCreateBySubjectiveIdList(idList4);


回答:

没按照楼上大佬用泛型改写的思路,直接用你的JS代码思路改写的

import java.util.HashMap;

import java.util.List;

import java.util.Map;

public class MyClass {

public static String getKeyName(String key) {

String[] v0 = key.split("questionCreateBy");

String v1 = v0[1].split("IdList")[0];

return v1;

}

public static void main(String[] args) {

Map<String, List<String>> listMap = new HashMap<>();

listMap.put("questionCreateBySingleIdList", examinationCreatePojo.questionCreateBySingleIdList);

listMap.put("questionCreateByMultipleIdList", examinationCreatePojo.questionCreateByMultipleIdList);

listMap.put("questionCreateByJudgmentIdList", examinationCreatePojo.questionCreateByJudgmentIdList);

listMap.put("questionCreateBySubjectiveIdList", examinationCreatePojo.questionCreateBySubjectiveIdList);

Map<String, String> errMap = new HashMap<>();

errMap.put("Single", "单选题题目生成错误");

errMap.put("Multiple", "多选题题目生成错误");

errMap.put("Judgment", "判断题题目生成错误");

errMap.put("Subjective", "主观题题目生成错误");

String keyName = "";

for (Map.Entry<String, List<String>> entry : listMap.entrySet()) {

String key = entry.getKey();

List<String> item = entry.getValue();

if (item.size() > 0) {

keyName = getKeyName(key);

Map<String, Object> map = httpMap.get("questionCreateBy" + keyName + "Service").postBatchAdd(item);//按照你题目写的,httpMap是哪里来的?

int i = (int) map.get("model");

if (i == 0) {

throw new Error(errMap.get(keyName));

} else {

List<Map<String, Object>> list = (List<Map<String, Object>>) map.get("list");

List<Integer> idList = examinationCreatePojo.get("questionCreateBy" + keyName + "IdList");

for (Map<String, Object> v : list) {

idList.add((int) v.get("id"));

}

examinationCreatePojo.put("questionCreateBy" + keyName + "IdList", idList);

}

}

}

}

}

以上是 JAVA代码优化? 的全部内容, 来源链接: utcz.com/p/945124.html

回到顶部