0
我有一個數據值列表,我將其插入到HashMap中供請求讀取,我將這些數據輸入到JSP中的表中。我在這裏遇到的問題是,我會成功將數據添加到相關列表中,然後將其添加到散列映射中,但是一旦我清除列表以檢索新年的新數據,它也會清除記錄的值哈希映射。如何阻止這個動態調整我的散列表?感謝您的時間。清除列表還會清除Java中以前的列表記錄
這是我的代碼,雖然我不認爲這是問題,因爲我缺乏知識。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
List<MyDataType> dataList = null;
try {
dataList = ***;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HashMap<String, List<BigDecimal>> recordYearlyData = new HashMap<>();
String nwC = "New Customers ";
String alC = "All Customers ";
String lsC = "Lost Customers ";
String rtC = "Rate ";
List<BigDecimal> startCus = new ArrayList<>();
List<BigDecimal> newCus = new ArrayList<>();
List<BigDecimal> endCus = new ArrayList<>();
List<BigDecimal> thisYearCus = new ArrayList<>();
List<BigDecimal> lostCus = new ArrayList<>();
List<BigDecimal> rateL = new ArrayList<>();
for (MyDataType dataType : dataList) {
String createDate = dataType.getOrderDate().toString();
String createDateN = createDate.substring(5, 7);
Integer getDateKey = Integer.parseInt(createDateN) - 1;
Integer getYear = Integer.parseInt(createDate.substring(0,4));
BigDecimal tenant_id = dataType.getTenantID();
createDateLabel = monthNames[getDateKey];
if (getYear != yearChecker) {
String yearLabel = String.valueOf(getYear - 1);
for (BigDecimal tenant : thisYearCus) {
if (newCus.contains(tenant) || startCus.contains(tenant)) {
endCus.add(tenant);
}
}
for (BigDecimal tenant : startCus) {
if (!thisYearCus.contains(tenant)) {
lostCus.add(tenant);
}
}
double rate = 1.0;
if (startCus.size() == 0) {
rate=1.0;
} else if (startCus.size() > 0){
rate = ((double) endCus.size() - (double) newCus.size())/(double) startCus.size();
}
rateL.add(BigDecimal.valueOf(rate));
recordYearlyData.put(alC+yearLabel, thisYearCus);
recordYearlyData.put(nwC+yearLabel, newCus);
recordYearlyData.put(lsC+yearLabel, lostCus);
recordYearlyData.put(rtC+yearLabel, rateL);
rateL.clear();
startCus.clear();
startCus.addAll(thisYearCus);
thisYearCus.clear();
endCus.clear();
newCus.clear();
lostCus.clear();
yearChecker = getYear;
}
if (!thisYearCus.contains(tenant_id)) {
thisYearCus.add(tenant_id);
}
if (!startCus.contains(tenant_id)) {
if(!newCus.contains(tenant_id)) {
newCus.add(tenant_id);
}
}
}
request.setAttribute("newCusTable", recordYearlyData.get("New Customers 2015"));
request.setAttribute("allCusTable", recordYearlyData.get("All Customers 2015"));
request.setAttribute("lostCusTable", recordYearlyData.get("Lost Customers 2015"));
request.setAttribute("retRate", recordYearlyData.get("Rate 2015"));
RequestDispatcher view = request.getRequestDispatcher("/Sales.jsp");
view.forward(request, response);
}
不需要調用'clear'這種解決方案 –
也不太工作,但使用這種嚇人袋熊的建議,我得到它的工作。我在for循環中創建了新列表,然後將以前的列表添加到克隆中以生成結果。 –