我正试图从列表中绘制出一张地图,并面临一些困难。我有一个名为"surveyAnsList“的列表,从这个列表中我只想得到”类型C“数据,并将这些数据放到称为"mapC”的地图中,并将这些映射放到JSONArray中。--当我这样做的时候,我不想在地图上放一些空的"answerValue“。
我的数据库就像

'CompanyRegistrationNumber 987-65-43210、RowNum 2、ColNum 3‘行中的空AnswerValues应包括在地图中,而'CompanyRegistrationNumber 123-45-56778、RowNum 2、ColNum 2-6’行中的空AnswerValues不应包括在地图中,因为RowNum 2的整个AnswerValues是空的。
我试过的代码是这样的。
for(int i=0; i<surveyAnsList.size(); i++){
String answerType = surveyAnsList.get(i).getAnswerType();
String rowNum = surveyAnsList.get(i).getRowNum();
String colNum = surveyAnsList.get(i).getColNum();
String order1 = surveyAnsList.get(i).getQuestionOrder1();
String order2 = surveyAnsList.get(i).getQuestionOrder2();
String answerValue = surveyAnsList.get(i).getAnswerValue();
String companyRegistrationNumber = surveyAnsList.get(i).getCompanyRegistrationNumber();
String companyName = surveyAnsList.get(i).getCompanyName();
String[] answerTypeArr = answerType.split("\\|");
String typeCAnswer = "";
String newValue = "";
String cNum = "";
Map<String, Object> mapC = new HashMap<>();
mapC.put("questionOrder1", order1);
mapC.put("questionOrder2", order2);
mapC.put("rowNum", rowNum);
mapC.put("colNum", colNum);
mapC.put("answerValue", answerValue);
mapC.put("companyRegistrationNumber", companyRegistrationNumber);
mapC.put("companyName", companyName);
if(!jsonArrayC_TN3.contains(mapC)){
jsonArrayC_TN3.add(mapC);
}
}当然,在这种情况下,映射包含了所有空的AnswerValue。我怎样才能实现我想要的?
发布于 2019-09-27 10:32:14
也许下面的代码可以帮助你
Map<String, List<Integer>> nameMap = new HashMap<>(); // tore the name and index
Set<String> values = new HashSet<>(); // judge if the whole AnswerValues is null
for(int i=0; i<surveyAnsList.size(); i++){
String answerType = surveyAnsList.get(i).getAnswerType();
String rowNum = surveyAnsList.get(i).getRowNum();
String colNum = surveyAnsList.get(i).getColNum();
String order1 = surveyAnsList.get(i).getQuestionOrder1();
String order2 = surveyAnsList.get(i).getQuestionOrder2();
String answerValue = surveyAnsList.get(i).getAnswerValue();
String companyRegistrationNumber = surveyAnsList.get(i).getCompanyRegistrationNumber();
String companyName = surveyAnsList.get(i).getCompanyName();
if (!nameMap.keySet().contains(companyRegistrationNumber)) {
if(values.size() == 0 && i != 0){ // remove the null value map, stored in jsonArrayC_TN3
nameMap.get("companyRegistrationNumber").forEach(jsonArrayC_TN3::remove);
}
nameMap.put(companyRegistrationNumber, new ArrayList<>());
values = new HashSet<>();
}
if (answerValue != null) {
values.add(answerValue);
nameMap.get("companyRegistrationNumber").add(jsonArrayC_TN3.size());
}
String[] answerTypeArr = answerType.split("\\|");
String typeCAnswer = "";
String newValue = "";
String cNum = "";
Map<String, Object> mapC = new HashMap<>();
mapC.put("questionOrder1", order1);
mapC.put("questionOrder2", order2);
mapC.put("rowNum", rowNum);
mapC.put("colNum", colNum);
mapC.put("answerValue", answerValue);
mapC.put("companyRegistrationNumber", companyRegistrationNumber);
mapC.put("companyName", companyName);
if(!jsonArrayC_TN3.contains(mapC)){
jsonArrayC_TN3.add(mapC);
}
}https://stackoverflow.com/questions/58131102
复制相似问题